Created
February 2, 2020 13:57
-
-
Save aadebdeb/f49b4abc6801c5ca13fad67d0ebfb952 to your computer and use it in GitHub Desktop.
random in 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 randomInSphere() { | |
const cosTheta = -2.0 * Math.random() + 1.0; | |
const sinTheta = Math.sqrt(1.0 - cosTheta * cosTheta); | |
const phi = 2.0 * Math.PI * Math.random(); | |
const radius = Math.pow(Math.random(), 1.0 / 3.0); | |
return [ | |
radius * sinTheta * Math.cos(phi), | |
radius * sinTheta * Math.sin(phi), | |
radius * 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 = randomInSphere(); | |
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