Skip to content

Instantly share code, notes, and snippets.

@shiffman
Created November 13, 2013 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shiffman/7443212 to your computer and use it in GitHub Desktop.
Save shiffman/7443212 to your computer and use it in GitHub Desktop.
Spherical coordinates in Processing
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