xxxxxxxxxx
let word = "";
function setup() {
createCanvas(710, 500);
colorMode(HSB, 360, 100, 100, 1.0);
word = fibonacciWord(15);
}
function draw() {
let a = map(cos(radians(frameCount)),1,-1,PI,-PI);
a = PI/2;
const l = 7; //map(cos(radians(frameCount)),1,-1,25,1);
background(0, 0, 5);
stroke(190, 85, 90);
strokeWeight(5);
const turtle = new Turtle(10, height - 10, l, -PI/2, a);
for (let k = 0; k < word.length; k++) {
turtle.forward();
let c = word.charAt(k);
if (c === "0") {
if (k % 2 === 0) {
turtle.left();
} else {
turtle.right();
}
}
}
// noLoop();
}
function fibonacciWord(n) {
if (n === 0) return "0";
if (n === 1) return "01";
return fibonacciWord(n - 1) + fibonacciWord(n - 2);
}
class Turtle {
constructor(x, y, l, a, ainc) {
this.x = x;
this.y = y;
this.a = a || 0;
this.ainc = ainc || PI/2;
this.l = l;
}
forward() {
// draw line
push();
translate(this.x, this.y);
rotate(this.a);
line(0, 0, this.l, 0);
pop();
// calculate new position
const v = createVector(this.x, this.y);
const av = p5.Vector.fromAngle(this.a);
av.setMag(this.l);
v.add(av);
this.x = v.x;
this.y = v.y;
}
left() {
this.a = this.a + this.ainc;
}
right() {
this.a = this.a - this.ainc;
}
}