Java 2D:将点 P 移动到靠近另一个点一定距离?
将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以将两个轴上的差异最小化一个百分比(这取决于您想要移动点的程度)。
例如:
因此,您将
p1
向p2
移动了一半。避免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:
So you moved
p1
halfway towardp2
. The good thing avoidabs
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.两点之间的最短距离是一条线,因此只需沿着连接两点的线移动该点 x 个单位即可。
编辑:如果这是家庭作业,我不想透露答案的具体细节,但这很简单,可以在不剧透的情况下进行说明。
让我们假设您有两个点
A
= (x1, y1) 和B
= (x<子>2,y<子>2)。包含这两点的直线具有方程其中
t
是某个参数。请注意,当t = 1
时,直线指定的点为B
,当t = 0
时,直线指定的点为A
。现在,您希望将
B
移动到B'
,该点与A
相距新的距离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) andB
= (x2, y2). The line that includes these two points has the equationwhere
t
is some parameter. Notice that whent = 1
, the point specified by the line isB
, and whent = 0
, the point specified by the line isA
.Now, you would like to move
B
toB'
, a point which is a new distanced
away fromA
:The point
B'
, like any other point on the line, is also governed by the equation we showed earlier. But what value oft
do we use? Well, whent
is 1, the equation points toB
, which is|AB|
units away fromA
. So the value oft
that specifiesB'
ist
=d/|AB|
.Solving for |AB| and plugging this into the above equation is left as an exercise to the reader.
向量来救援!
给定点 A 和 B。创建一个从 A 到 B 的向量 V(通过执行 BA)。将向量 V 标准化为单位向量,然后将其与所需的距离 d 相乘,最后将所得向量添加到点 A。即:
Java(ish)
没有角度,不需要讨厌的三角函数。
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:
Java(ish)
No angles, no nasty trig needed.
这可能是伪代码的基本思想,但还有更多内容。您如何定义“最佳”方式?
需要考虑某些事情:
给我们更多细节,我们会看到我们得到的结果。
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:
Give us a little more detail and we'll see what we've got. :D
在这个游戏中,积分坐标用于表示网格中的正方形。方法
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.