c# - how to move point a given distance d (and get a new coordinates)

孤街浪徒 提交于 2019-12-03 10:48:33

问题


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 ??


回答1:


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:

  • Polar coordinate system
  • Distance Formula
  • Pythagorean trigonometric identity



回答2:


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;



回答3:


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)




回答4:


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


来源:https://stackoverflow.com/questions/4598858/c-sharp-how-to-move-point-a-given-distance-d-and-get-a-new-coordinates

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!