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

Hi I was wondering if there is any efficient way to calculate the coordinates of a point (which has been moved a distance d from the original location).

Say I have a point P (0,3,0,5) and I need to move a point random direction with distance d.

So far, I have done this by randomly choosing the new x and y coordinates, and I have checked whether the distance between the old and the new point is d. I really understand that this is not a very effective way to do this. How would you do that?

+6
c # geometry 2d point
source share
4 answers

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

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

Evidence

 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 can watch:

+17
source share

The formula for this includes basic trig functions.

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

By the way, the angle should be in radians.

 radians = degrees * Math.PI / 180.0; 
+8
source share

If there is no restriction as to which direction the point is moving, the easiest way is to move along one axis.

So, if you need to move a point to a distance of 1 unit, your point P (0,3,0,5) can simply become one of the following: P (1,3,0,5) or P (0.3,1.5)

0
source share

You need to solve a 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); 
0
source share

All Articles