将 Point2D.Double x 距离移近另一个 Point2D.Double 的最佳方法是什么?
编辑:试图编辑,但因维护而停机。不,这不是作业
我需要将飞机 (A) 移向跑道 (C) 的尽头并将其指向正确的方向(角度 a)。
alt text http://img246.imageshack.us/img246/9707/planec.png
这是我目前所拥有的,但看起来很乱,做这样的事情通常的方法是什么?
//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);
可以在 http://pastebin.com/RtCB2kSZ 找到三角形类
记住飞机可以在跑道点周围的任何位置
最佳答案
两点之间的最短距离是一条直线,因此只需将该点沿连接两点的直线移动 x
个单位即可。
编辑:如果这是家庭作业,我不想透露答案的具体细节,但这很简单,可以在不剧透的情况下进行说明。
假设您有两个点 A
= (x1, y1) 和 B
= (x2, y2)。包括这两个点的线有等式
(x1, y1) + t · (x2 - x1, y2 - y1)
其中 t
是一些参数。注意当t = 1
时,直线指定的点是B
,当t = 0
时,直线指定的点是A
。
现在,您想将 B
移动到 B'
,一个距离 A 新距离
:d
的点
A B' B
(+)---------------------(+)-----------(+)
<========={ d }=========>
B'
点与直线上的任何其他点一样,也受我们之前显示的方程式的约束。但是我们使用什么值的 t
呢?那么,当 t
为 1 时,等式指向 B
,它与 A
相距 |AB|
个单位.因此,指定 B'
的 t
的值是 t
= d/|AB|
。
求解|AB|并将其代入上述等式作为练习留给读者。
关于Java 二维 : Moving a point P a certain distance closer to another point?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2353268/