Created
January 14, 2017 12:04
-
-
Save u840903/a51d2fed48c91a2541839899915a152a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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