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 ??
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:
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With