xxxxxxxxxx
109
function setup() {
createCanvas(windowWidth, windowHeight);
angleMode(DEGREES);
background(220);
ted = new Turtle();
textSize(30);
fill(128);
textAlign(CENTER, CENTER);
text('tap to start', width / 2, height / 2);
}
function mousePressed() {
logo(ted, random(45, 315));
}
function touchStarted() {
logo(ted, random(45, 315));
}
function logo(t, angle) {
t.clearscreen();
textSize(30);
fill(128);
textAlign(LEFT, TOP);
text(floor(angle * 10) / 10, 20, 20);
var i = 0;
while (t.x > 0 && t.x < width && t.y > 0 && t.y < height) {
t.forward(i);
t.right(angle);
i++;
}
}
class Turtle {
constructor(x_ = width / 2, y_ = height / 2, direction_ = -90, penDown_ = true, penColor_ = color(0)) {
this.x = x_;
this.y = y_;
this.direction = direction_;
this.penDown = penDown_;
this.penColor = penColor_;
}
forward(d) {
let oldX = this.x;
let oldY = this.y;
this.x += d * cos(this.direction);
this.y += d * sin(this.direction);
if (this.penDown) {
stroke(this.penColor);
line(oldX, oldY, this.x, this.y);
}
}
backward(d) {
this.forward(-d);
}
right(a) {
this.direction += a;
}
left(a) {
this.direction -= a;
}
setpos(newx, newy) {
let oldX = this.x;
let oldY = this.y;
this.x = newx
this.y = newy
if (this.penDown) {
stroke(this.penColor);
line(oldX, oldY, this.x, this.y);
}
}
home() {
this.x = width / 2;
this.y = height / 2;
this.direction = -90;
}
setheading(a) {
this.direction = a;
}
pendown() {
this.penDown = True;
}
penup() {
this.penDown = False;
}
pencolor(c) {
this.penColor = c;
}
clean() {
background(220);
}
clearscreen() {
this.home();
this.clean();
}
}