Search code examples
javascriptrandom3deffectdemo

Randomly generated 3D points on sphere surface


I am generating 3D points and process them 3D rotation:

var Points = [] ;
for (var i=0 ; i < 20 ; i++) {
    Points[i] = [
        Math.floor(Math.random()*256),
        Math.floor(Math.random()*256),
        Math.floor(Math.random()*256)
    ] ;
}
Process3DRotation() ;

But how to generate randomly 3D points on a hidden shpere like this:

enter image description here


Solution

  • Ok, here simple code to sample uniformly on the sphere. For theory behind it take a look at http://mathworld.wolfram.com/SpherePointPicking.html

    var radius = 10. ;
    var Points = [] ;
    for (var i=0 ; i < 20 ; i++) {
        var phi  = 2. * 3.1415926 * Math.random();
        var csth = 1.0 - 2.0 * Math.random();
        var snth = Math.sqrt(1.0 - csth*csth);
        Points[i] = [
            radius * snth * Math.cos(phi),
            radius * snth * Math.sin(phi),
            radius * csth
        ] ;
    }