How to randomize points on a sphere surface evenly

2019-07-31 04:28发布

Im trying to make stars on the sky, but the stars distribution isnt even.

This is what i tried:

rx = rand(0.0f, PI*2.0f);
ry = rand(0.0f, PI);
x = sin(ry)*sin(rx)*range;
y = sin(ry)*cos(rx)*range;
z = cos(ry)*range;

Which results to:

img http://img716.imageshack.us/img716/3320/sphererandom.jpg

And:

rx = rand(-1.0f, 1.0f);
ry = rand(-1.0f, 1.0f);
rz = rand(-1.0f, 1.0f);
x = rx*range;
y = ry*range;
z = rz*range;

Which results to:

img2 http://img710.imageshack.us/img710/5152/squarerandom.jpg

(doesnt make a sphere, but opengl will not tell a difference, though).

As you can see, there is always some "corner" where are more points in average. How can i create random points on a sphere where the points will be distributed evenly?

标签: math geometry
3条回答
We Are One
2楼-- · 2019-07-31 04:32

You don't need trigonometry if you can generate random gaussian variables, you can do (pseudocode)

x <- gauss()
y <- gauss()
z <- gauss()
norm <- sqrt(x^2 + y^2 + z^2)

result = (x / norm, y / norm, z / norm)

Or draw points inside the unit cube until one of them is inside the unit ball, then normalize:

double x, y, z;

do
{
    x = rand(-1, 1);
    y = rand(-1, 1);
    z = rand(-1, 1);
} while (x * x + y * y + z * z > 1);

double norm = sqrt(x * x + y * y + z * z);
x / norm; y /= norm; z /= norm;
查看更多
一纸荒年 Trace。
3楼-- · 2019-07-31 04:42

you can do

z = rand(-1, 1)
rxy = sqrt(1 - z*z)
phi = rand(0, 2*PI)
x = rxy * cos(phi)
y = rxy * sin(phi)

Here rand(u,v) draws a uniform random from interal [u,v]

查看更多
别忘想泡老子
4楼-- · 2019-07-31 04:54

It looks like you can see that it's the cartesian coordinates that are creating the concentrations.

Here is an explanation of one right (and wrong) way to get a proper distribution.

查看更多
登录 后发表回答