c# - 如何将点移动给定距离 d (并获取新坐标)

发布于 2024-10-10 01:35:12 字数 175 浏览 0 评论 0原文

你好 我想知道是否有任何有效的方法来计算点的坐标(从原始位置移动了距离 d)。

假设我有一个点 P(0.3,0.5),我需要将该点随机方向移动距离 d。

到目前为止,我通过随机选取新的 x 和 y 坐标来完成此操作,并且检查新旧点之间的距离是否等于 d。我确实意识到这并不是太有效的方法。 你会怎么做?

Hi
I was wondering if there is any efficent way to calculating coordinates of point (which was moved distance d from it's original location).

Let's say I have a point P(0.3,0.5) and I need to move that point random direction with distance d.

So far I did it by random picking new x and y coordinates and I was checking if distance between old and new point equals d. I do realize that is't too eficient way to do that.
How would You do it ??

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

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

发布评论

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

评论(4

桃扇骨 2024-10-17 01:35:12

给定一个点 (x1, y1),我们想要找到一个距离它 d 的“随机”点 (x2, y2)

选择一个随机角度theta。那么:

x2 = x1 + d * cos(theta)
y2 = y1 + d * sin(theta)

这将是一个以 (x1, y1) 为中心、半径为 d 的圆上的随机点

证明

Distance between (x1, y1) and (x2, y2)
= sqrt ( (x2 - x1) ^ 2 + (y2 - y1) ^ 2)
= sqrt ( d^2 * (sin^2 (theta) + cos^2 (theta) ) )
= d

您可能想看看:

Given a point (x1, y1), we want to find a "random" point (x2, y2) at a distance d from it.

Pick a random angle theta. Then:

x2 = x1 + d * cos(theta)
y2 = y1 + d * sin(theta)

This will be a random point on a circle of radius d centered at (x1, y1)

Proof:

Distance between (x1, y1) and (x2, y2)
= sqrt ( (x2 - x1) ^ 2 + (y2 - y1) ^ 2)
= sqrt ( d^2 * (sin^2 (theta) + cos^2 (theta) ) )
= d

You might want to look at:

热情消退 2024-10-17 01:35:12

其公式涉及基本的三角函数。

new_x = old_x + Math.cos(angle) * distance;
new_y = old_y + Math.sin(angle) * distance;

顺便说一句,角度应该以弧度为单位。

radians = degrees * Math.PI / 180.0;

The formula for that involves basic trig functions.

new_x = old_x + Math.cos(angle) * distance;
new_y = old_y + Math.sin(angle) * distance;

By the way, angle should be in radians.

radians = degrees * Math.PI / 180.0;
好倦 2024-10-17 01:35:12

您需要解简单的方程:

double dx_square = rand.NextDouble(d);
double dy_square = d - dx_square;
double dx = Math.Sqrt(dx_square);
double dy = Math.Sqrt(dy_square);

You need to resolve simple equation:

double dx_square = rand.NextDouble(d);
double dy_square = d - dx_square;
double dx = Math.Sqrt(dx_square);
double dy = Math.Sqrt(dy_square);
给我一枪 2024-10-17 01:35:12

如果点移动的方向没有限制,最简单的方法是仅沿一个轴移动。

因此,如果您必须将点移动 1 个单位的距离,则您的点 P(0.3,0.5) 可以简单地变为以下任一:
P(1.3,0.5),或
P(0.3,1.5)

If there is no constraint as to in which direction the point is to move, the easiest way is to move it along only one axis.

So, if you have to move the point by a distance of 1 unit, your point P(0.3,0.5), can simply become either of the following:
P(1.3,0.5), or
P(0.3,1.5)

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