# Random points on surfaces

**URL:** https://discourse.threejs.org/t/random-points-on-surfaces/34153
**Category:** Resources
**Tags:** points
**Created:** [January 26, 2022, 10:00pm UTC](https://discourse.threejs.org/t/random-points-on-surfaces/34153 "2022-01-26T22:00:39Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![prisoner849](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/prisoner849/32/535_2.png) [@prisoner849](https://discourse.threejs.org/u/prisoner849)
#### Post date: [January 26, 2022, 10:00pm UTC](https://discourse.threejs.org/t/random-points-on-surfaces/34153/1 "2022-01-26T22:00:39Z")

</div>

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](https://math.stackexchange.com/q/2017079/1018985)  
Sphere is based on the `.randomDirection()` method of `THREE.Vector3()`.

Example: [JSFiddle](https://jsfiddle.net/prisoner849/yzx8fbqu/show)

 ![изображение](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/7/1/7159ad834d6f370550c6ae51eb6566eb1e1340b1.png)

Code:

```javascript
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;
}

```

---

<div class="post-metadata">

### Author: ![NHQue](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/nhque/32/23292_2.png) [@NHQue](https://discourse.threejs.org/u/NHQue)
#### Post date: [January 26, 2022, 10:03pm UTC](https://discourse.threejs.org/t/random-points-on-surfaces/34153/2 "2022-01-26T22:03:07Z")

</div>

Wow, this looks amazing!!

---

<div class="post-metadata">

### Author: ![amitlzkpa](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/amitlzkpa/32/1735_2.png) [@amitlzkpa](https://discourse.threejs.org/u/amitlzkpa)
#### Post date: [February 2, 2022, 4:06pm UTC](https://discourse.threejs.org/t/random-points-on-surfaces/34153/3 "2022-02-02T16:06:41Z")

</div>

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

[Pyramid](https://jsfiddle.net/amitlzkpa/2rwsey9j/0)

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/5/f/5ff37e9ce26178da5b7488ac757a1d424f82eb31.png)

[Cube](https://jsfiddle.net/amitlzkpa/aL5wmech/)

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/8/6/868b7a8fc8d24411d94dcc571947fad634b1cdcc.png)

[Dodecahedron](https://jsfiddle.net/amitlzkpa/kaj1w5yr/)

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/f/a/fad8b68ffc5a7f262948ecb10ed784ee2a7688da.png)

[Octahedron](https://jsfiddle.net/amitlzkpa/y3fb9d1t/)

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/0/1/0176c2d34e95fc3de88dd3ad5a3442c187ccf47d.png)

[Icosahedron](https://jsfiddle.net/amitlzkpa/qagL8d4z/)

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/a/8/a82c1a29883cc2cd55205fee2e0e2bb1c1f57912.png)

---

<div class="post-metadata">

### Author: ![xyz\_Abc](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/xyz_abc/32/31152_2.png) [@xyz\_Abc](https://discourse.threejs.org/u/xyz_Abc)
#### Post date: [November 17, 2022, 10:31am UTC](https://discourse.threejs.org/t/random-points-on-surfaces/34153/4 "2022-11-17T10:31:52Z")

</div>

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

---

<div class="post-metadata">

### Author: ![hofk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/hofk/32/44944_2.png) [@hofk](https://discourse.threejs.org/u/hofk)
#### Post date: [November 17, 2022, 4:27pm UTC](https://discourse.threejs.org/t/random-points-on-surfaces/34153/5 "2022-11-17T16:27:14Z")

</div>

> [@xyz\_Abc](#):
>
> heart shape

From the [Collection of examples from discourse.threejs.org](https://discourse.threejs.org/t/collection-of-examples-from-discourse-threejs-org/4315)  
[ProceduralHeart](https://hofk.de/main/discourse.threejs/2019/ProceduralHeart/ProceduralHeart.html)

From [Addon. Produces almost infinite many time-varying geometries with functions](https://discourse.threejs.org/t/addon-produces-almost-infinite-many-time-varying-geometries-with-functions/262)

see [examples THREEf](https://hofk.de/main/threejs/sandboxthreef/examplesTHREEf%20r136.html)  
and  
[form library THREEf.js](https://hofk.de/main/threejs/sandboxthreef/formLibrary136.html) :

```javascript
//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](https://mathworld.wolfram.com/HeartCurve.html)

---

<div class="post-metadata">

### Author: ![xyz\_Abc](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/xyz_abc/32/31152_2.png) [@xyz\_Abc](https://discourse.threejs.org/u/xyz_Abc)
#### Post date: [November 20, 2022, 9:39am UTC](https://discourse.threejs.org/t/random-points-on-surfaces/34153/6 "2022-11-20T09:39:50Z")

</div>

Thankyou very much
