Skip to content

Instantly share code, notes, and snippets.

@craftoid
Created October 6, 2016 23:19
Show Gist options
  • Save craftoid/217377c70f03bcb2d0d2fd44c1d4ea0a to your computer and use it in GitHub Desktop.
Save craftoid/217377c70f03bcb2d0d2fd44c1d4ea0a to your computer and use it in GitHub Desktop.
Yet another turtlewalk of the Thue-Morse Sequence, creating the Koch-Curve.
// @bitcraftlab 2016
boolean animate = true;
int dir = 1;
int iter = 5;
int n = 0;
int steps;
void setup() {
size(640, 400);
};
void draw() {
steps = 2 << (2*(iter-1));
float d = 1024.0 / pow(3, iter);
background(255);
strokeWeight(d/4);
translate(150, 240);
rotate(radians(-120));
for (int i = 0; i < n; i++) {
// calculate thue morse digit
int b = i;
int r = 0;
while (b > 0) {
r = (r + b) % 2;
b /= 2;
}
// interprete the digit as turtle code
if (r == 0) {
rotate(radians(180));
} else {
rotate(radians(-60));
line(0, 0, d, 0); translate(d, 0);
}
}
n = animate && iter > 3 ? (n + dir + steps) % steps : steps;
}
void keyPressed() {
int i = iter;
switch(keyCode) {
case ' ': animate = !animate; break;
case UP: i--; break;
case DOWN: i++; break;
case LEFT: dir = -1; break;
case RIGHT: dir = 1; break;
}
i = constrain(i, 2, 6);
n *= pow(4, i - iter);
iter = i;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment