Created
November 3, 2012 09:33
-
-
Save thomasboyt/4006777 to your computer and use it in GitHub Desktop.
sierpinski
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
class EqTriangle { | |
public float x, y, h, w; | |
EqTriangle(float xx, float yy, float hh, float ww) { | |
x = xx; | |
y = yy; | |
h = hh; | |
w = ww; | |
} | |
//copy | |
EqTriangle(EqTriangle tri) { | |
this(tri.x, tri.y, tri.h, tri.w); | |
} | |
void half() { | |
this.h = h/2; | |
this.w = w/2; | |
} | |
void render() { | |
float x1 = this.x; | |
float y1 = this.y; | |
float x2 = this.x + (this.w / 2); | |
float y2 = this.y - this.h; | |
float x3 = this.x + this.w; | |
float y3 = this.y; | |
triangle(x1, y1, x2, y2, x3, y3); | |
} | |
} | |
void sierpinski(EqTriangle tri, int depth, int maxDepth) { | |
EqTriangle tri1 = new EqTriangle(tri); | |
tri1.half(); | |
EqTriangle tri2 = new EqTriangle(tri); | |
tri2.half(); | |
tri2.x = tri.x + (tri.w / 2); | |
EqTriangle tri3 = new EqTriangle(tri); | |
tri3.half(); | |
tri3.x = tri.x + (tri.w / 4); | |
tri3.y = tri.y - (tri.h/2); | |
if (depth == maxDepth) { | |
tri1.render(); | |
tri2.render(); | |
tri3.render(); | |
} | |
if (depth < maxDepth) { | |
sierpinski(tri1, depth + 1, maxDepth); | |
sierpinski(tri2, depth + 1, maxDepth); | |
sierpinski(tri3, depth + 1, maxDepth); | |
} | |
} | |
void setup() { | |
size(500,500); | |
noStroke(); | |
fill(255,255,255); | |
EqTriangle container = new EqTriangle(0, 500, 500, 500); | |
container.render(); | |
fill(0,0,0); | |
sierpinski(container, 0, 4); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment