Random points on surfaces

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;
}
9 Likes

Wow, this looks amazing!!

2 Likes

I have some samples of other (fractal) structures done similarly.

Pyramid

Cube

Dodecahedron

Octahedron

Icosahedron

3 Likes

can i ask how to convert the outer circle to a heart shape?

From the Collection of examples from discourse.threejs.org
ProceduralHeart

From Addon. Produces almost infinite many time-varying geometries with functions

see examples THREEf
and
form library THREEf.js :

//0024 heart (pulsating by t)  @author hofk 
 radiusSegments:	50,
 height:		10,
 withTop:		true,
 withBottom:		true,
 rCircHeight:	function ( u, v, t ) { return 20 * ( u - 0.5 ) * ( u - 0.5 ) + 0.3 * Math.sin ( Math.PI * v ) + 0.3 * Math.sin( t ) },
 moveX:		function ( u, v, t ) { return  -Math.cos( 2 * Math.PI * u ) },
 topHeight:	function ( u, t ) { return   0.6 * ( 1.4 + Math.cos ( Math.sin( t ) * Math.sin( t ) * u ) ) }

See also Heart Curve -- from Wolfram MathWorld

1 Like

Thankyou very much