Let's play with Bezier curves !
Log in to post a comment.
// You can find the Turtle API reference here: https://turtletoy.net/syntax
Canvas.setpenopacity(1);
const size = 95; // min = 5, max = 100, step = 5
const space = 20; // min = 0, max = 20, step = 1
const amplitude = 1; // min = 0.1, max = 3, step = 0.1
let turtle = new Turtle();
class cubicBezier{
constructor(a, b, c, d){
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.turtle = new Turtle();
this.turtle.jump(this.a);
}
walk(t){
let l1 = lerp(this.a, this.b, t);
let l2 = lerp(this.b, this.c, t);
let l3 = lerp(this.c, this.d, t);
l1 = lerp(l1, l2, t);
l2 = lerp(l2, l3, t);
this.turtle.goto(lerp(l1, l2, t));
}
}
function lerp(a, b, t){
let x = a[0] + (b[0] - a[0]) * t;
let y = a[1] + (b[1] - a[1]) * t;
return [x, y];
}
function rand(){
return Math.random() * 2 * size - size;
}
function randomPoint(){
return [amplitude * rand(), amplitude * rand()];
}
let curves = [];
let b1 = randomPoint();
let b2 = randomPoint();
let c1 = randomPoint();
let c2 = randomPoint();
for(let i = 0; i <= 1; i += 0.05){
let y = i * 2 * space - space;
let curve = new cubicBezier([-size, y], lerp(b1, b2, i), lerp(c1, c2, i), [size, y]);
curves.push(curve);
}
// The walk function will be called until it returns false.
function walk(i) {
for(let j = 0; j <= 1.001; j += 0.01){
curves[i].walk(j);
}
if(i >= curves.length - 1) return false;
return true;
}