java返回p点到线段的距离,Java 2D:将点P移至距另一个点一定距离?

What is the best way to go about moving a Point2D.Double x distance closer to another Point2D.Double?

Edit: Tried to edit, but so went down for maintenance. No this is not homework

I need to move a plane (A) towards the end of a runway (C) and point it in the correct direction (angle a).

Here is what I have so far, but it seems messy, what is the usual way to go about doing something like this?

//coordinate = plane coordinate (Point2D.Double)

//Distance = max distance the plane can travel in this frame

Triangle triangle = new Triangle(coordinate, new Coordinate(coordinate.x, landingCoordinate.y), landingCoordinate);

double angle = 0;

//Above to the left

if (coordinate.x <= landingCoordinate.x && coordinate.y <= landingCoordinate.y)

{

angle = triangle.getAngleC();

coordinate.rotate(angle, distance);

angle = (Math.PI-angle);

}

//Above to the right

else if (coordinate.x >= landingCoordinate.x && coordinate.y <= landingCoordinate.y)

{

angle = triangle.getAngleC();

coordinate.rotate(Math.PI-angle, distance);

angle = (Math.PI*1.5-angle);

}

plane.setAngle(angle);

The triangle class can be found at http://pastebin.com/RtCB2kSZ

Bearing in mind the plane can be in in any position around the runway point

解决方案

Vectors to the rescue!

Given points A and B. Create a vector V from A to B (by doing B-A). Normalize vector V into a unit vector and then just multiply it with the distance, d, you want and finally add the resulting vector to point A. ie:

A_moved = A + |(B-A)|*d

Java(ish)

Vector2D a_moved = a.add(b.subtract(a).norm().multiply(d));

No angles, no nasty trig needed.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值