Random Point Within a Sphere
Assuming that the sphere is at the origin, and the radius of the sphere is r, how would I go about generating a random x,y,z coordinate within it?
[edited by - Mulligan on May 19, 2002 7:34:00 PM]
Work in polar (spherical) coordinates. Pick theta with range [0, 2pi), phi with range [-pi/2, pi/2], and radius with range [0, 10] randomly, and use them to get cartesian coordinates:
x = r * cos(theta) * cos(phi)
y = r * sin(phi)
z = r * sin(theta) * cos(phi)
[edited by - IndirectX on May 19, 2002 7:36:43 PM]
x = r * cos(theta) * cos(phi)
y = r * sin(phi)
z = r * sin(theta) * cos(phi)
[edited by - IndirectX on May 19, 2002 7:36:43 PM]
---visit #directxdev on afternet <- not just for directx, despite the name
Spherical coordinates are probably not what you''re looking for.
First of all, you have to do several trig operations, which are slow. Secondly, the distribution will not be completely uniform, it will tend to generate more points in the centre and toward the poles.
Try this:
do {
x = random in [-r, r]
y = random in [-r, r]
z = random in [-r, r]
} while(x^2 + y^2 + z^2 > r^2)
It has a uniform distribution, and it''s faster.
"Math is hard" -Barbie
First of all, you have to do several trig operations, which are slow. Secondly, the distribution will not be completely uniform, it will tend to generate more points in the centre and toward the poles.
Try this:
do {
x = random in [-r, r]
y = random in [-r, r]
z = random in [-r, r]
} while(x^2 + y^2 + z^2 > r^2)
It has a uniform distribution, and it''s faster.
"Math is hard" -Barbie
"Math is hard" -Barbie
quote:
Original post by Pragma
Try this:
do {
x = random in [-r, r]
y = random in [-r, r]
z = random in [-r, r]
} while(x^2 + y^2 + z^2 > r^2)
It has a uniform distribution, and it''s faster.
I don''t get it. All I want is one point.
You are right about the first part, I thought my eyes were just playing tricks one me when I saw this happeneing. Thanks for the insight.
That code takes a random point in the surrounding cube, then tests if it''s in the sphere. The distribution will be as random as your rand() function.
I remember an old thread where I came up with a nice way to generate a random point in a circle. I''ll see what I can do for the 3d version...
I remember an old thread where I came up with a nice way to generate a random point in a circle. I''ll see what I can do for the 3d version...
both ideas are good IMHO
i want to know something though, why do you want a random point
many people ask for this
i want to know something though, why do you want a random point
many people ask for this
The reason I want them is because I''m making a galaxy simulator for a science project. Each star starts at a random point within the galaxy''s sphere of influence. As soon as I can get a good random point generator, I can refine it to fit to model true star distribution formulas. (if there are any). By the way, are there any for galaxy star placement like there are for atom electron placements?
quote:
Original post by Mulligan
The reason I want them is because I''m making a galaxy simulator for a science project.
This seems to be homework, which normally I do not allow in the forum. But since you''re working on a project and not specifically a math/physics homework question , I''ll allow the thread. Besides that, there are games that need to do what you''re trying to do.
In the future, when asking about homework related things, please state why you''re asking, and indicate what you''ve already done yourself prior to coming here for the answer. And keep in mind that these forums are generally reserved for game-development issues.
Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
With the spherical approach you can get a better distribution by generating a number between 0 and r^3 an then taking the cube root of that number. There is either something wrong with my generator or you still get a concentration at the poles.
Keys to success: Ability, ambition and opportunity.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement