Skip to content

Instantly share code, notes, and snippets.

@u840903
Created January 14, 2017 12:04
Show Gist options
  • Save u840903/a51d2fed48c91a2541839899915a152a to your computer and use it in GitHub Desktop.
Save u840903/a51d2fed48c91a2541839899915a152a to your computer and use it in GitHub Desktop.
/*
Returns a random point of a sphere, evenly distributed over the sphere.
The sphere is centered at (x0,y0,z0) with the passed in radius.
The returned point is returned as a three element array [x,y,z].
*/
function randomSpherePoint(x0,y0,z0,radius){
var u = Math.random();
var v = Math.random();
var theta = 2 * Math.PI * u;
var phi = Math.acos(2 * v - 1);
var x = x0 + (radius * Math.sin(phi) * Math.cos(theta));
var y = y0 + (radius * Math.sin(phi) * Math.sin(theta));
var z = z0 + (radius * Math.cos(phi));
return [x,y,z];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment