Reputation: 235
I'm writing a simple 2d brownian motion simulator in Python. It's obviously easy to draw values for x displacement and y displacement from a distribution, but I have to set it up so that the 2d displacement (ie hypotenuse) is drawn from a distribution, and then translate this to new x and y coordinates. This is probably trivial and I'm just too far removed from trigonometry to remember how to do it correctly. Am I going to need to generate a value for the hypotenuse and then translate it into x and y displacements with sin and cos? (How do you do this correctly?)
Upvotes: 1
Views: 1098
Reputation: 69192
This is best done by using polar coordinates (r, theta)
for your distributions (where r
is your "hypotenuse")), and then converting the result to (x, y)
, using x = r cos(theta)
and y = r sin(theta)
. That is, select r
from whatever distribution you like, and then select a theta
, usually from a flat, 0 to 360 deg, distribution, and then convert these values to x
and y
.
Going the other way around (i.e., constructing correlated (x, y) distributions that gave a direction independent hypotenuse) would be very difficult.
Upvotes: 1
Reputation: 50971
If you have a hypotenuse in the form of a line segment, then you have two points. From two points in the form P0 = (x0, y0)
P1 = (x1, y1)
you can get the x and y displacements by subtracting x0
from x1
and y0
from y1
.
If your hypotenuse is actually a vector in a polar coordinate plane, then yes, you'll have to take the sin
of the angle and multiply it by the magnitude of the vector to get the y displacement and likewise with cos
for the x displacement.
Upvotes: 0