将 Point2D.Double x 距离移近另一个 Point2D.Double 的最佳方法是什么?
编辑:尝试编辑,但因维护而停机。不,这不是作业
我需要将飞机 (A) 移向跑道末端 (C) 并将其指向正确的方向(角度 a)。
替代文本http://img246.imageshack.us/img246/9707/planec.png 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 http://pastebin.com/RtCB2kSZ
请记住,飞机可以处于跑道点周围的任何位置
您可以将两个轴上的差异最小化一个百分比(这取决于您想要移动点的程度)。
例如:
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);
所以你搬家了p1
一半走向p2
。好的事情要避免abs
也就是说,如果您选择哪个点将被移动,哪个点将保持静止,您可以避免 if 测试并仅使用原始系数。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)