Created
August 16, 2016 16:20
-
-
Save chamun/8f0f88489ec094433944eceffd14c5a2 to your computer and use it in GitHub Desktop.
My implementation of a Koch Snowflake plot using 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); | |
} | |
void draw() { | |
background(#ffffff); | |
int steps = 4; | |
PVector a = new PVector(100, 200); | |
PVector b = new PVector(500, 200); | |
PVector c = PVector.add(a, PVector.sub(b, a).rotate(radians(60))); | |
step(a, b, steps); | |
step(b, c, steps); | |
step(c, a, steps); | |
} | |
void step(PVector a, PVector b, int depth) { | |
if (depth > 0) { | |
PVector ab = PVector.sub(b, a).mult(1 / 3f); | |
PVector ap = PVector.add(a, ab); | |
PVector bp = PVector.sub(b, ab); | |
PVector c = PVector.add(ap, ab.copy().rotate(- radians(60))); | |
step(a, ap, depth - 1); | |
step(ap, c, depth - 1); | |
step(c, bp, depth - 1); | |
step(bp, b, depth - 1); | |
} else { | |
stroke(#000000); | |
line(a.x, a.y, b.x, b.y); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment