Created
November 13, 2013 03:27
-
-
Save shiffman/7443212 to your computer and use it in GitHub Desktop.
Spherical coordinates in Processing
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
void setup() { | |
size(600, 600, P3D); | |
} | |
void draw() { | |
background(0); | |
translate(width/2, height/2); | |
rotateX(frameCount*0.02); | |
rotateY(frameCount*0.03); | |
randomSeed(4); | |
for (int i = 0; i < 1000; i++) { | |
PVector v = random3D(); | |
// This is built into Processing now | |
// PVector v = PVector.random3D(this); | |
v.mult(100); | |
stroke(255); | |
strokeWeight(4); | |
point(v.x, v.y, v.z); | |
} | |
} | |
PVector random3D() { | |
float angle = random(0,TWO_PI); | |
float vz = random(-1,1); | |
float vx = sqrt(1-vz*vz)*cos(angle); | |
float vy = sqrt(1-vz*vz)*sin(angle); | |
PVector v = new PVector(vx, vy, vz); | |
return v; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment