将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);
Run Code Online (Sandbox Code Playgroud)
三角类可以在http://pastebin.com/RtCB2kSZ找到
请记住,飞机可以处于跑道点周围的任何位置
两点之间的最短距离是一条线,因此只需x沿着连接两点的线移动该点单位.
编辑:如果这是作业,我不想泄露答案的细节,但这很简单,它可以说明而不是太扰乱.
我们假设你有两个点A=(x 1,y 1)和B=(x 2,y 2).包含这两个点的线具有等式
(x 1,y 1)+ t·(x 2 - x 1,y 2 - y 1)
哪个t是参数.请注意,当t = 1行指定的点是B,以及何时t = 0,该行指定的点是A.
现在,你想移动B到B',一个点,这是一个新的距离d距A:
A B' B
(+)---------------------(+)-----------(+)
<========={ d }=========>
Run Code Online (Sandbox Code Playgroud)
B'与该线上的任何其他点一样,该点也受我们之前显示的等式控制.但是t我们使用什么价值?好吧,当t为1时,等式指向B,即距离的|AB|单位A.所以t它指定的值B'是t= d/|AB|.
求解| AB | 并将其插入上述等式中作为练习留给读者.
您可以将两个轴上的差异最小化一个百分比(这取决于您想要移动多少点).
例如:
Point2D.Double p1, p2;
//p1 and p2 inits
// you don't use abs value and use the still point as the first one of the subtraction
double deltaX = p2.getX() - p1.getX();
double deltaY = p2.getY() - p1.getY();
// now you know how much far they are
double coeff = 0.5; //this coefficient can be tweaked to decice how much near the two points will be after the update.. 0.5 = 50% of the previous distance
p1.setLocation(p1.getX() + coeff*deltaX, p1.getY() + coeff*deltaY);
Run Code Online (Sandbox Code Playgroud)
所以你走了p1一半p2.避免的好处abs是,如果您选择移动哪个点以及哪个点将静止,您可以避免测试并使用原始系数.
矢量救援!
给定点A和B.创建从A到B的向量V(通过做BA).将矢量V归一化为单位矢量,然后将其乘以你想要的距离d,最后将得到的矢量加到A点.即:
A_moved = A + |(B-A)|*d
Run Code Online (Sandbox Code Playgroud)
爪哇(ISH)
Vector2D a_moved = a.add(b.subtract(a).norm().multiply(d));
Run Code Online (Sandbox Code Playgroud)
没有角度,不需要讨厌的三角形.
| 归档时间: |
|
| 查看次数: |
8759 次 |
| 最近记录: |