110 lines
2.0 KiB
JavaScript
110 lines
2.0 KiB
JavaScript
let t;
|
|
const len = 1000;
|
|
|
|
function setup() {
|
|
// createCanvas(400, 300);
|
|
createCanvas(window.innerWidth, window.innerHeight);
|
|
noLoop(); // will draw only once
|
|
t = new Turtle(); // getting ready to draw
|
|
// green
|
|
stroke(0, 200, 0);
|
|
strokeWeight(5);
|
|
}
|
|
|
|
function draw() {
|
|
background(240);
|
|
|
|
// getting ready
|
|
t.setxy(-len / 2, 0);
|
|
t.right(90);
|
|
|
|
// draw Koch line
|
|
koch(5, len);
|
|
}
|
|
|
|
function koch(order, length) {
|
|
// PLACEHOLDER
|
|
t.forward(200);
|
|
}
|
|
|
|
// Don't write underneath this line
|
|
|
|
class Turtle {
|
|
constructor() {
|
|
this.oldx = int(width / 2);
|
|
this.oldy = int(height / 2);
|
|
this.x = this.oldx;
|
|
this.y = this.oldy;
|
|
this.tcolor = 0;
|
|
this.angle = 0;
|
|
stroke(this.tcolor);
|
|
}
|
|
|
|
forward(step) {
|
|
this.x = this.oldx - step * cos(radians(this.angle + 90));
|
|
this.y = this.oldy - step * sin(radians(this.angle + 90));
|
|
line(this.oldx, this.oldy, this.x, this.y);
|
|
this.oldx = this.x;
|
|
this.oldy = this.y;
|
|
}
|
|
|
|
back(step) {
|
|
this.x = this.oldx + step * cos(radians(this.angle + 90));
|
|
this.y = this.oldy + step * sin(radians(this.angle + 90));
|
|
line(this.oldx, this.oldy, this.x, this.y);
|
|
this.oldx = this.x;
|
|
this.oldy = this.y;
|
|
}
|
|
|
|
home() {
|
|
this.oldx = int(width / 2);
|
|
this.oldy = int(height / 2);
|
|
line(this.oldx, this.oldy, this.x, this.y);
|
|
this.oldx = this.x;
|
|
this.oldy = this.y;
|
|
this.angle = 0.0;
|
|
}
|
|
|
|
setx(step) {
|
|
this.x = this.oldx + step;
|
|
this.oldx = this.x;
|
|
}
|
|
|
|
sety(step) {
|
|
this.y = this.oldy + step;
|
|
this.oldy = this.y;
|
|
}
|
|
|
|
setxy(stepx, stepy) {
|
|
this.x = this.oldx + stepx;
|
|
this.y = this.oldy + stepy;
|
|
this.oldx = this.x;
|
|
this.oldy = this.y;
|
|
}
|
|
|
|
left(dangle) {
|
|
this.angle -= dangle;
|
|
}
|
|
|
|
right(dangle) {
|
|
this.angle += dangle;
|
|
}
|
|
|
|
setheading(nangle) {
|
|
this.angle = nangle;
|
|
}
|
|
|
|
pencolor(ncolor) {
|
|
this.tcolor = ncolor;
|
|
stroke(this.tcolor);
|
|
}
|
|
|
|
penup() {
|
|
noStroke();
|
|
}
|
|
|
|
pendown() {
|
|
stroke(this.tcolor);
|
|
}
|
|
}
|