// Joshua Brown
// Jdbrown@andrew.cmu.edu
// Section C
// Project 11: Turtle Fun
// makeTurtle(x, y) -- make a turtle at x, y, facing right, pen down
// left(d) -- turn left by d degrees
// right(d) -- turn right by d degrees
// forward(p) -- move forward by p pixels
// back(p) -- move back by p pixels
// penDown() -- pen down
// penUp() -- pen up
// goto(x, y) -- go straight to this location
// setColor(color) -- set the drawing color
// setWeight(w) -- set line width to w
// face(d) -- turn to this absolute direction in degrees
// angleTo(x, y) -- what is the angle from my heading to location x, y?
// turnToward(x, y, d) -- turn by d degrees toward location x, y
// distanceTo(x, y) -- how far is it to location x, y?
function setup () {
createCanvas (480, 480);
background (0);
var t1 = makeTurtle(270, 120);
var t2 = makeTurtle(255, 160);
var t3 = makeTurtle(330, 340);
var t4 = makeTurtle(30, 280);
for (var i = 0; i < 1000; i++) {
makeMoz(t1); // makes the mini-med sized pattern;
makeMaz(t2); // makes the smallest patter;
makeMax(t3); // makes the largest pattern;
makeThing(t4); // makes the larger-med sized pattern
}
}
function makeThing (t4) {
t4.forward(10);
t4.left(45);
t4.forward(25);
t4.left(90);
t4.forward(50);
t4.left(135);
t4.forward(75);
t4.left(180);
t4.forward(150);
t4.right(45);
t4.forward(50);
t4.right(90);
}
function makeMax (t3, a1) {
t3.forward(200);
t3.left(90);
t3.forward(180);
t3.left(90);
t3.forward(160);
t3.left(45);
t3.forward(140);
t3.left(90);
t3.forward(120);
t3.left(90);
t3.forward(100);
t3.left(45);
t3.forward(80);
t3.left(90);
t3.forward(60);
t3.left(90);
t3.forward(40);
t3.left(45);
t3.forward(20);
t3.left(90);
}
function makeMoz (t1) {
t1.forward(50);
t1.right(90);
t1.forward(50);
t1.right(45);
t1.forward(50);
t1.right(90);
t1.forward(50);
t1.right(90);
t1.forward(50);
t1.right(90);
t1.forward(50);
t1.right(45);
t1.forward(50);
t1.right(90);
t1.forward(50);
t1.right(90);
t1.forward(50);
t1.right(90);
t1.forward(50);
t1.right(45);
t1.forward(50);
t1.right(90);
t1.forward(50);
t1.right(90);
t1.forward(50);
t1.right(90);
t1.forward(50);
t1.right(45);
t1.forward(50);
t1.right(90);
t1.forward(50);
t1.right(90);
t1.forward(50);
t1.forward(50);
t1.right(90);
t1.forward(50);
t1.right(45);
t1.forward(50);
t1.right(90);
t1.forward(50);
}
function makeMaz (t2) {
t2.forward(25);
t2.right(90);
t2.forward(25);
t2.right(45);
t2.forward(25);
t2.right(90);
t2.forward(25);
t2.right(90);
t2.forward(25);
t2.right(90);
t2.forward(25);
t2.right(45);
t2.forward(25);
t2.right(90);
t2.forward(25);
t2.right(90);
t2.forward(25);
t2.right(90);
t2.forward(25);
t2.right(45);
t2.forward(50);
t2.right(90);
t2.forward(50);
t2.right(90);
t2.forward(50);
t2.right(90);
t2.forward(50);
t2.right(45);
t2.forward(50);
t2.right(90);
t2.forward(50);
t2.right(90);
t2.forward(50);
t2.forward(50);
t2.right(90);
t2.forward(50);
t2.right(45);
t2.forward(50);
t2.right(90);
t2.forward(50);
}
function turtleLeft(d) {
this.angle -= d;
}
function turtleRight(d) {
this.angle += d;
}
function turtleForward(p) {
var rad = radians(this.angle);
var newx = this.x + cos(rad) * p;
var newy = this.y + sin(rad) * p;
this.goto(newx, newy);
}
function turtleBack(p) {
this.forward(-p);
}
function turtlePenDown() {
this.penIsDown = true;
}
function turtlePenUp() {
this.penIsDown = false;
}
function turtleGoTo(x, y) {
if (this.penIsDown) {
stroke(this.color);
strokeWeight(this.weight);
line(this.x, this.y, x, y);
}
this.x = x;
this.y = y;
}
function turtleDistTo(x, y) {
return sqrt(sq(this.x - x) + sq(this.y - y));
}
function turtleAngleTo(x, y) {
var absAngle = degrees(atan2(y - this.y, x - this.x));
var angle = ((absAngle - this.angle) + 360) % 360.0;
return angle;
}
function turtleTurnToward(x, y, d) {
var angle = this.angleTo(x, y);
if (angle < 180) {
this.angle += d;
} else {
this.angle -= d;
}
}
function turtleSetColor(c) {
this.color = c;
}
function turtleSetWeight(w) {
this.weight = w;
}
function turtleFace(angle) {
this.angle = angle;
}
function makeTurtle(tx, ty) {
var turtle = {x: tx, y: ty,
angle: 0.0,
penIsDown: true,
color: color(255),
weight: 1,
left: turtleLeft, right: turtleRight,
forward: turtleForward, back: turtleBack,
penDown: turtlePenDown, penUp: turtlePenUp,
goto: turtleGoTo, angleto: turtleAngleTo,
turnToward: turtleTurnToward,
distanceTo: turtleDistTo, angleTo: turtleAngleTo,
setColor: turtleSetColor, setWeight: turtleSetWeight,
face: turtleFace};
return turtle;
}
There’s something simplistic and beautiful about these turtle designs. I’ve chosen to go a simpler route and just explore the shapes turtles can effortlessly create. I really enjoy these mosaic-shapes.