Skip to content

Instantly share code, notes, and snippets.

@EDDxample
Created February 17, 2019 01:35
Show Gist options
  • Save EDDxample/da7ac60077b660351b7fcf4d52ac877b to your computer and use it in GitHub Desktop.
Save EDDxample/da7ac60077b660351b7fcf4d52ac877b to your computer and use it in GitHub Desktop.
3D Fractal based on "The Moon is a Harsh Mistress", each cell (Group of 3 people), can talk with its leader (upper level), other 2 cells at the same level and each member leads its own Subcell
void setup() {
size(400, 400, P3D);
}
float deg = 3;
void draw() {
background(200);
translate(width/2, height/20);
scale(1,-1,1);
rotateX(radians(10));
rotateY(deg += 0.01);
rotateX(radians(142));
rotateZ(radians(45));
recursion(0, 0, 0, 120);
//if (deg < 5.1) saveFrame("frames/tetra_###");
}
void recursion(float x, float y, float z, float size) {
// main tetrahedron
tetra(x, y, z, size);
if (size <= 5) return;
// child tetrahedra
float csize = size / 1.99f;
recursion(x + size, y + size, z, csize);
recursion(x, y + size, z + size, csize);
recursion(x + size, y, z + size, csize);
// connections
stroke(20);
line(x + size + csize, y + size, z + csize,
x + size + csize, y + csize, z + size);
line(x + size, y + size + csize, z + csize,
x + csize, y + size + csize, z + size);
line(x + size, y + csize, z + size + csize,
x + csize, y + size, z + size + csize);
}
void tetra(float x, float y, float z, float size) {
fill(255,100,100,50);
drawtetra(x,y,z,size);
hint(DISABLE_DEPTH_TEST);
noFill();
stroke(0);
drawtetra(x,y,z,size);
}
void drawtetra(float x, float y, float z, float size) {
beginShape(TRIANGLE_STRIP);
vertex(x, y, z); // vertex 1
vertex(x + size, y + size, z); // vertex 2
vertex(x + size, y, z + size); // vertex 3
vertex(x, y + size, z + size); // vertex 4
vertex(x, y, z); // vertex 1
vertex(x + size, y + size, z); // vertex 2
vertex(x, y + size, z + size); // vertex 4
vertex(x + size, y, z + size); // vertex 3
vertex(x + size, y + size, z); // vertex 2
endShape(CLOSE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment