Java 2D:将点 P 移动到靠近另一个点一定距离?

发布于 2024-08-23 10:54:06 字数 1314 浏览 6 评论 0原文

将 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 中找到,

请记住平面可以位于跑道点周围的任何位置

What is the best way to go about moving a Point2D.Double x distance closer to another Point2D.Double?

Edit: Tried to edit, but so went down for maintenance. No this is not homework

I need to move a plane (A) towards the end of a runway (C) and point it in the correct direction (angle a).

alt text http://img246.imageshack.us/img246/9707/planec.png

Here is what I have so far, but it seems messy, what is the usual way to go about doing something like this?

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

The triangle class can be found at http://pastebin.com/RtCB2kSZ

Bearing in mind the plane can be in in any position around the runway point

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

木格 2024-08-30 10:54:06

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

例如:

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 的好处是,如果您选择哪个点将被移动以及哪个点将保持静止,您可以避免 if 测试并仅使用原始系数。

You can minimize the difference along both axis by a percent (that depends on how much you want to move the points).

For example:

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

So you moved p1 halfway toward p2. The good thing avoid abs is that, if you choose which point will be moved and which one will stand still you can avoid if tests and just use the raw coefficient.

青朷 2024-08-30 10:54:06

两点之间的最短距离是一条线,因此只需沿着连接两点的线移动该点 x 个单位即可。


编辑:如果这是家庭作业,我不想透露答案的具体细节,但这很简单,可以在不剧透的情况下进行说明。

让我们假设您有两个点 A = (x1, y1) 和 B = (x<子>2,y<子>2)。包含这两点的直线具有方程

(x1, y1) + t · (x2 - x1, y<子>2 - y<子>1)

其中 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|并将其代入上述方程,作为练习留给读者。

The shortest distance between two points is a line, so simply move that point x units along the line that connects the two points.


Edit: I didn't want to give away the specifics of the answer if this is homework, but this is simple enough that it can be illustrated without being too spoiler-y.

Let us assume you have two points A = (x1, y1) and B = (x2, y2). The line that includes these two points has the equation

(x1, y1) + t · (x2 - x1, y2 - y1)

where t is some parameter. Notice that when t = 1, the point specified by the line is B, and when t = 0, the point specified by the line is A.

Now, you would like to move B to B', a point which is a new distance d away from A:

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

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

The point B', like any other point on the line, is also governed by the equation we showed earlier. But what value of t do we use? Well, when t is 1, the equation points to B, which is |AB| units away from A. So the value of t that specifies B' is t = d/|AB|.

Solving for |AB| and plugging this into the above equation is left as an exercise to the reader.

不气馁 2024-08-30 10:54:06

向量来救援!

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

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

Java(ish)

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

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

Vectors to the rescue!

Given points A and B. Create a vector V from A to B (by doing B-A). Normalize vector V into a unit vector and then just multiply it with the distance, d, you want and finally add the resulting vector to point A. ie:

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

Java(ish)

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

No angles, no nasty trig needed.

┾廆蒐ゝ 2024-08-30 10:54:06
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);
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);
爱要勇敢去追 2024-08-30 10:54:06
(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 个单位?直接在 2 个点之间的直线上 1 个单位?

给我们更多细节,我们会看到我们得到的结果。

(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

that might be the basic idea in pseudocode, but there's a lot more to it than that. How do you define 'best' way?

Certain things need to be considered:

  • Is there a specific measure/ratio you want the points to be apart from each other, or do you just want them closer?
  • Should both coordinates always be modified? (e.g. if you want to move (1,50) closer to (0,0), do you make it (.5, 25) or just (1,25)?
  • if the point is to be 1 unit away, should it be 1 unit horizontally? directly diagonally? 1 unit away on the line between the 2 points?

Give us a little more detail and we'll see what we've got. :D

著墨染雨君画夕 2024-08-30 10:54:06

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

In this game, integral coordinates are used to represent squares in a grid. The method move(int row, int col) moves toward the specified row and column by advancing one square in one of eight semi-cardinal directions, as seen here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文