Hi community!
Maybe it will be useful for somebody.
Uniformly distributed points on surfaces (sphere, torus, ring).
Torus and ring are based on answers from here: probability - uniform random points on a torus - Mathematics Stack Exchange
Sphere is based on the .randomDirection()
method of THREE.Vector3()
.
Example: JSFiddle
Code:
function getRandomPointsOnRing(R, r, count = 1000){
return new Array(count).fill(0).map(p => {
let rand = Math.random();
let radius = Math.sqrt(R * R * rand + (1 - rand) * r * r);
return new THREE.Vector3().setFromSphericalCoords(radius, Math.PI * 0.5, Math.random() * 2 * Math.PI);
});
}
function getRandomPointsOnTorus(R, r, count = 1000){
let pts = [];
let counter = 0;
let COUNT = count;
let U, V, W;
while(counter < COUNT){
U = Math.random();
V = Math.random();
W = Math.random();
let theta = 2 * Math.PI * U;
let phi = 2 * Math.PI * V;
if(W <= ((R + r * Math.cos(theta)) / (R + r))){
pts.push(new THREE.Vector3(
(R + r * Math.cos(theta)) * Math.cos(phi),
R + r * Math.cos(theta)) * Math.sin(phi),
r * Math.sin(theta)
))
counter++;
}
}
return pts;
}