• 12
• 9
• 9
• 13
• 10

# Vector for 1 radius sphere

This topic is 3646 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi folks, how can i make a vector with radius 1 point to a sphere ?? i know what it is in 2d, but my imagination is limited, in 3d it doesnt work as well i have these formula
//alpha is x,y angle
//beta is x,z angle
float alpha = ::getRandomMinMax( 0.0f, 360.0f );
float beta = ::getRandomMinMax(  0.0f, 360.0f );
float x = cos(alpha) * cos(beta);
float y = sin(alpha) * sin(beta);
float z = cos(alpha) * sin(beta);

and a picture you can click on it. and can someone explain what a vector should look like if alpha and beta are 90 degree... i think that there is the mistake cause sin(90) * cos(90) is always 0 the problem is at 45 and 0 degrees these vectors are just too small, but x = 0,7 * 1 = 0,7 y = 0,7 * 0 = 0 z = 0,7 * 0 = 0 would be (45,0) = (0.7,0,0) and that is ok isnt it ?? [Edited by - Ben091986 on March 23, 2008 6:33:05 PM]

##### Share on other sites
It seems like you would be trying to make a random point on the sphere?

If so, this link is for you:
http://mathworld.wolfram.com/SpherePointPicking.html

The main point of the page is that you cannot use a linear distribution for the angle. If you do that, the points will eventually bunch up at the poles.

To get a good distribution you use two random variables in the range [0, 1]:

theta = 2*pi*var1
phi = acos(2*var2 - 1)

From there you convert the spherical coordinates to Cartesian by:

x = r*cos(theta)*sin(phi)
y = r*sin(theta)*sin(phi)
z = r*sin(phi)

Since you're talking about a unit sphere, r = 1.

Is this something like what you're asking for?

##### Share on other sites
Quote:
 i think that there is the mistake cause sin(90) * cos(90) is always 0

cos 90 = 0 ;)

##### Share on other sites
Quote:
Original post by NerdInHisShoe
Quote:
 i think that there is the mistake cause sin(90) * cos(90) is always 0

cos 90 = 0 ;)

and someting multiplied with 0 is 0 ...

		float theta = 2 * 3.141f * ::getRandomMinMax( 0.0f, 1.0f );		float phi   = acos(2*::getRandomMinMax(0.0f, 1.0f) - 1);		float x = cos(theta) * sin(phi);		float y = sin(theta) * sin(phi);//::getRandomMinMax( -1.0f, 1.0f );		float z = sin(phi);

but with your formula it looks like

##### Share on other sites
I'm not even sure what you're doing, or if you're implying that things are "working" yet, or not.

If you are indeed attempting to generate a set of random points along the surface of a unit sphere, then the problem is not with the formula that I gave you. It works, but perhaps for not what you're needing?

The theta/phi generates a good distribution of radian angles, and the x/y/z code just simply converts those to Cartesian coordinates.

Can you post the code for the working 2D analogy so that I can extrapolate from that instead?

##### Share on other sites
This is a good link on how to generate random points on a sphere: http://mathworld.wolfram.com/SpherePointPicking.html

##### Share on other sites
If you ever get sick of hand-typing the (approximated) value of pi, it can be generated at runtime by pi = 4.0*atan(1.0).

Marcel Luttgens from sci.physics.relativity showed me this one. He's full of neat and useful math "tricks" such as this. jyk is also very good at these kinds of problems. It's like these guys were doing trigonometry in the womb!

##### Share on other sites
Unless you HAVE to use angles... there's an easier way.

Just generate a random vector (say between -1 to +1 on each axis)
Then Normalize it.
// without a vector classfloat x = ::getRandomMinMax( -1.0f, 1.0f );float y = ::getRandomMinMax( -1.0f, 1.0f );float z = ::getRandomMinMax( -1.0f, 1.0f );float l = sqrt(x*x+y*y+z*z)x = x / ly = y / lz = z / l

edit: just noticed similar is mentioned in the mathworld link.
HTH

##### Share on other sites
Quote:
 Original post by daftasbrushUnless you HAVE to use angles... there's an easier way.Just generate a random vector (say between -1 to +1 on each axis)Then Normalize it.*** Source Snippet Removed ***edit: just noticed similar is mentioned in the mathworld link.HTH

This will not give you a uniform distribution. The parts of the sphere that point at the corners of the [-1,+1]^3 box will be picked more often than the rest.

  double x,y,z,l2;  do {    x=random_double(-1.0,1.0);    y=random_double(-1.0,1.0);    z=random_double(-1.0,1.0);    l2=x*x+y*y+z*z;  } while(l2>1.0);  double l=sqrt(l2);  x/=l;  y/=l;  z/=l;