6

将 Point2D.Double x 距离移近另一个 Point2D.Double 的最佳方法是什么?

编辑:试图编辑,但因维护而停机。不,这不是作业

我需要将飞机 (A) 移向跑道 (C) 的尽头,并将其指向正确的方向(角度 a)。

替代文字 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找到

记住飞机可以在跑道点周围的任何位置

4

6 回答 6

6

两点之间的最短距离是一条线,因此只需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

现在,您想移动BB',距离 新距离的dA

 A                       B'            B
(+)---------------------(+)-----------(+)

 <========={ d }=========>

与线上的任何其他点一样,该点B'也受我们之前显示的方程的支配。t但是我们使用什么值呢?好吧,当t为 1 时,等式指向B,与|AB|相距单位A。所以t指定的值B't= d/|AB|

求解 |AB| 并将其代入上述等式留给读者作为练习。

于 2010-03-01T00:45:15.683 回答
5

您可以将两个轴上的差异最小化一个百分比(这取决于您想要移动点的程度)。

例如:

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);

所以你p1p2. 避免的好处abs是,如果您选择哪个点将被移动,哪个点将静止,您可以避免进行测试并仅使用原始系数。

于 2010-03-01T00:56:24.480 回答
4

向量来救援!

给定点 A 和 B。创建一个从 A 到 B 的向量 V(通过做 BA)。将向量 V 归一化为单位向量,然后将其与所需的距离 d 相乘,最后将结果向量添加到点 A。即:

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

Java(什)

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

没有角度,不需要讨厌的三角。

于 2010-03-01T15:27:59.620 回答
3
double angle = Math.atan2(landingCoordinate.y-coordinate.y, landingCoordinate.x-coordinate.x);

coordinate.x += Math.cos(angle)*distance;
coordinate.y += Math.sin(angle)*distance;

//Add 90 degress to the plane angle
plane.setAngle(angle + 1.57079633);
于 2010-03-01T15:20:05.353 回答
0
(point A is to be moved closer to B)

if (A.x > B.x)
    //decrement A.x
if (A.x < B.x)
    //increment A.x

这可能是伪代码的基本思想,但远不止这些。您如何定义“最佳”方式?

有些事情需要考虑:

  • 是否有特定的度量/比率您希望这些点彼此分开,或者您只是希望它们更接近?
  • 是否应该始终修改两个坐标?(例如,如果您想将 (1,50) 移动到更靠近 (0,0) 的位置,您是使其成为 (.5, 25) 还是仅是 (1,25)?
  • 如果该点距离 1 个单位,水平方向应该是 1 个单位吗?直接对角线?1个单位在2点之间的线上?

给我们更多细节,我们会看到我们有什么。:D

于 2010-03-01T00:51:42.580 回答
0

在这个游戏中,积分坐标用于表示网格中的正方形。该方法move(int row, int col)通过在八个半基本方向之一上前进一个方格来向指定的行和列移动,如此处所示

于 2010-03-01T01:57:08.153 回答