Java2D:将点P移动到离另一点较近的某一距离?

Java2D:将点P移动到离另一点较近的某一距离?,java,java-2d,Java,Java 2d,移动一个点2D的最佳方式是什么?双倍x距离靠近另一个点2D。双倍 编辑:尝试编辑,但因维护原因而停机。不,这不是家庭作业 我需要将飞机(a)移向跑道(C)的末端,并将其指向正确的方向(角度a) 这是我到目前为止所做的,但看起来很混乱,做这样的事情通常的方法是什么 //coordinate = plane coordinate (Point2D.Double) //Distance = max distance the plane can travel in this frame

移动一个点2D的最佳方式是什么?双倍x距离靠近另一个点2D。双倍

编辑:尝试编辑,但因维护原因而停机。不,这不是家庭作业

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

这是我到目前为止所做的,但看起来很混乱,做这样的事情通常的方法是什么

    //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);
//坐标=平面坐标(Point2D.Double)
//距离=飞机在此帧中可以行驶的最大距离
三角形=新三角形(坐标,新坐标(坐标.x,着陆坐标.y),着陆坐标);
双角度=0;
//左上方

如果(坐标.x两点之间的最短距离是一条直线,那么只需沿着连接两点的直线移动该点
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 }=========>
ab'B
(+)---------------------(+)-----------(+)
B'
,就像直线上的任何其他点一样,也由我们前面展示的方程控制。但是我们使用
t
的值是什么?当
t
为1时,方程指向
B
,即
| AB
距离
A
的单位。因此
t
的值指定了
B'
is
t
=
d/|AB

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

(将点A移到B附近)
(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
如果(A.x>B.x) //减量A.x 如果(A.x
这可能是伪代码的基本思想,但它的意义远不止于此。您如何定义“最佳”方式

某些事情需要考虑:

  • 是否有一个特定的度量/比率,您希望这些点彼此分开,还是希望它们更接近
  • 是否应始终修改这两个坐标?(例如,如果要将(1,50)移近(0,0),是将其设置为(.5,25)还是仅设置为(1,25)
  • 如果该点距离为1个单位,那么在两个点之间的直线上,该点是否水平距离为1个单位?直接对角距离为1个单位

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

您可以将沿两个轴的差异最小化百分之一(这取决于您希望移动点的程度)

例如:

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
的好处是,如果您选择移动哪一点,哪一点静止不动,您可以避免进行测试,只需使用原始系数。

在这种情况下,积分坐标用于表示网格中的正方形。该方法将移向spe通过在八个半基数方向中的一个方向上前进一个正方形来指定行和列,如图所示。

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

  A_moved = A + |(B-A)|*d
爪哇语(ish)


不需要角度,也不需要急转弯。

@Jack:注意你的解是如何进行与我的解完全相同的计算的,但使用了另一种表示法。是的,因为向量总是处理问题的最佳方式:)
  A_moved = A + |(B-A)|*d
  Vector2D a_moved = a.add(b.subtract(a).norm().multiply(d));