Skip to content

Instantly share code, notes, and snippets.

@reuk
Created December 19, 2013 21:01
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 reuk/8046199 to your computer and use it in GitHub Desktop.
Save reuk/8046199 to your computer and use it in GitHub Desktop.
Sierpinski pyramid zoom
//PImage logo;
void setup() {
size(500, 500, P3D);
colorMode(RGB, 1);
frameRate(25);
// logo = loadImage("logo.png");
// logo.filter(INVERT);
}
final int LOOP_LENGTH = 30;
void draw() {
final int FRAME = frameCount % LOOP_LENGTH;
final float TIME = FRAME / float(LOOP_LENGTH);
final float COLOR = 1;
background(0);
fill(COLOR);
noStroke();
lights();
translate(width * 0.9, height * 0.1);
final float BASE_SIZE = 400;
final float CURRENT_SIZE = BASE_SIZE * pow(2, TIME);
translate(-CURRENT_SIZE * sqrt(2) * 2 / 3, 2 * CURRENT_SIZE / 3);
rotateY(PI * 0.75);
directionalLight(0, 1, 1, -0.3, 0, 0.1);
directionalLight(1, 0, 0, 0.2, 0, -0.1);
drawPyramid(11, new PVector(0, 0, 0), CURRENT_SIZE);
camera();
// image(logo, width - logo.width, height - logo.height);
saveFrame("###-tetra.png");
if (frameCount == LOOP_LENGTH + 2) exit();
}
void translate(final PVector P) {translate(P.x, P.y, P.z);}
void vertex(final PVector P) {vertex(P.x, P.y, P.z);}
void drawPyramid(final int LEVEL, final PVector CENTER, final float SIDE_LENGTH) {
PVector P1 = new PVector(1, 1, 1);
PVector P2 = new PVector(-1, -1, 1);
PVector P3 = new PVector(-1, 1, -1);
PVector P4 = new PVector(1, -1, -1);
if (LEVEL == 0) {
pushMatrix();
translate(CENTER);
final float L = (2 * SIDE_LENGTH) / 3;
P1.mult(L);
P2.mult(L);
P3.mult(L);
P4.mult(L);
beginShape(TRIANGLES);
vertex(P1); vertex(P2); vertex(P3);
vertex(P1); vertex(P3); vertex(P4);
vertex(P1); vertex(P4); vertex(P2);
vertex(P2); vertex(P3); vertex(P4);
endShape();
popMatrix();
return;
}
final float FACTOR = SIDE_LENGTH / 3;
P1.mult(FACTOR); P1.add(CENTER);
P2.mult(FACTOR); P2.add(CENTER);
P3.mult(FACTOR); P3.add(CENTER);
P4.mult(FACTOR); P4.add(CENTER);
drawPyramid(LEVEL - 1, P1, SIDE_LENGTH / 2);
drawPyramid(LEVEL - 1, P2, SIDE_LENGTH / 2);
drawPyramid(LEVEL - 1, P3, SIDE_LENGTH / 2);
drawPyramid(LEVEL - 1, P4, SIDE_LENGTH / 2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment