Last active
June 25, 2017 18:19
-
-
Save kchapelier/a6c719fb0a2b5eccb79f58e744838072 to your computer and use it in GitHub Desktop.
Uniform distribution on a sphere
This file contains hidden or 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
//Return random directions, spherical angle pairs theta [0,PI) and phi [0,TWO_PI), distributed uniformly over a sphere | |
//Pairs generated with theta=random(PI) and phi=random(TWO_PI) have a higher density around the poles. | |
//See spherical coordinates: https://en.wikipedia.org/wiki/Spherical_coordinate_system | |
//Optimalization by Dan Gries | |
//https://pastebin.com/7uPYaA8G | |
function randomThetaPhi () { | |
"use strict"; | |
var eps = Math.random(), | |
z = 1 - 2 * eps, | |
t = Math.PI * 2 * Math.random(); | |
return [Math.acos(z), t]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment