Skip to content

Instantly share code, notes, and snippets.

@craftoid
Created October 6, 2016 23:14
Show Gist options
  • Save craftoid/e33bedb3f29aaf2ab33ba7bb22223831 to your computer and use it in GitHub Desktop.
Save craftoid/e33bedb3f29aaf2ab33ba7bb22223831 to your computer and use it in GitHub Desktop.
A Turtlewalk based on the Thue-Morse-Sequence
// @bitcraftlab 2016
boolean animate = true;
int dir = 1;
float a = PI/3;
int iter = 5;
int n = 0;
int steps;
void setup() {
size(640, 400);
};
void draw() {
steps = 1 << (2 * iter);
float d = 2048.0 / pow(3, iter);
background(255);
stroke(0, 80);
strokeWeight(d/4);
translate(150 + d/4, 300 - d);
rotate(radians(-60));
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;
}
// interpret the digit as turtle code
if (r == 0) {
rotate(radians(-180));
} else {
rotate(radians(-120));
}
line(0, 0, d, 0); translate(d, 0);
}
n = animate && iter > 2 ? (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, 1, 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