views:

132

answers:

3

Hey,

I'm trying to figure out an algorithm for finding a random point a set distance away from a base point. So for example:

alt text

This could just be basic maths and my brain not working yet (forgive me, haven't had my coffee yet :) ), but I've been trying to work this out on paper and I'm not getting anywhere.

Thanks,

Niall.

+9  A: 

coordinate of point on circle with radius R and center (xc, yc):

x = xc + R*cos(a);
y = yc + R*sin(a);

changing value of angle a from 0 to 2*PI you can find any point on circumference.

Vladimir
+1  A: 

Basic Pythagoras.

Pick random number between 0 and 50 and solve h^2 = a^2 + b^2 Add a few random descisions on direction.

Martin
It should be pointed out that this will not give an answer that's uniformly distributed in the way most people would expect. Instead, this method (used, say, on the unit circle) will tend to slightly favour points near (1,0) and (-1,0) (or (0,1) and (0,-1) depending on how you implement it).
Peter Milley
@Peter - Good point. And the density, I believe, should be sin() or cos().
ysap
+5  A: 

Use the angle from the verticle as your random input.

Pseudocode:

angle = rand(0,1)
x = cos(angle * 2 * pi) * Radius + x_centre
y = sin(angle * 2 * pi) * Radius + y_centre
Visage