xxxxxxxxxx
// a simple version of Papert's turtle
var x = [];
var y = [];
var a = [];
var d = 10;
var t = 30; // turn amount
function setup() {
createCanvas(windowWidth, windowHeight);
init();
strokeWeight(10);
noLoop();
}
function draw() {
}
function keyTyped() {
if(key=='c') init();
if(key=='F') // draw forward
{
let x1 = x[x.length-1] + d*cos(a[a.length-1]);
let y1 = y[y.length-1] + d*sin(a[a.length-1]);
line(x[x.length-1], y[y.length-1], x1, y1);
x[x.length-1] = x1;
y[y.length-1] = y1;
redraw();
}
if(key=='+') // turn right
{
a[a.length-1]+=radians(t);
}
if(key=='-') // turn left
{
a[a.length-1]-=radians(t);
}
if(key=='[') // start branch
{
x.push(x[x.length-1]);
y.push(y[y.length-1]);
a.push(a[a.length-1]);
}
if(key==']') // end branch
{
x.pop();
y.pop();
a.pop();
}
}
function init() {
background(255);
x = [];
y = [];
a = [];
x[0] = width/2;
y[0] = height/2;
a[0] = PI*3/2;
}