Created
February 2, 2020 13:56
-
-
Save aadebdeb/f62d21057bb7badd3bcf35ead2349358 to your computer and use it in GitHub Desktop.
random on sphere with p5.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Please run on p5.js Web Editor or OpenProcessing. | |
// https://editor.p5js.org/ | |
// https://www.openprocessing.org/ | |
function randomOnSphere() { | |
const cosTheta = -2.0 * Math.random() + 1.0; | |
const sinTheta = Math.sqrt(1.0 - cosTheta * cosTheta); | |
const phi = 2.0 * Math.PI * Math.random(); | |
return [ | |
sinTheta * Math.cos(phi), | |
sinTheta * Math.sin(phi), | |
cosTheta | |
]; | |
} | |
function setup() { | |
createCanvas(300, 300, WEBGL); | |
noLoop(); | |
} | |
function draw() { | |
camera(0, 0, 250, 0, 0, 0, 0, 1, 0); | |
lights(); | |
const halfSize = [0.5 * width, 0.5 * height]; | |
background(240); | |
fill(255); | |
noStroke(); | |
for(let i = 0; i < 2000; i++) { | |
const pos = randomOnSphere(); | |
push(); | |
translate(100.0 * pos[0], 100.0 * pos[1], 100.0 * pos[2]); | |
sphere(2.0); | |
pop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment