Digerkam
Digerkam

Reputation: 1921

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

Upvotes: 0

Views: 236

Answers (1)

Severin Pappadeux
Severin Pappadeux

Reputation: 20080

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
    ] ;
}

Upvotes: 1

Related Questions