# dayoungl Project-11

``````//Sharon Lee
//dayoungl@andrew.cmu.edu
//Section E
//Project 11 - freestyle turtle

// 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?
var t1;
var t1;

function setup(){
createCanvas(400,400);
background(220);
noLoop();

t1 = makeTurtle(width/2, height/2); //start drawing from the centre
t1.setColor(0);
t1.setWeight(1.5);
t1.penDown();
for (var i = 0; i < width; i ++){
//randomize stroke colour & direction
var x = floor(random (1,4));
if (x == 1){
t1.setColor("yellow");
t1.forward(12);
t1.right(90);
}
if(x == 2){
t1.setColor("red");
t1.forward(12);
t1.left(10); // adding a bit of randomness to 90 degree angles
}

if(x == 3){
t1.setColor("blue");
t1.back(20);
t1.left(90);
}
}
}

//turtle API
function turtleLeft(d) {
this.angle -= d;
}

function turtleRight(d) {
this.angle += d;
}

function turtleForward(p) {
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(128),
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;
}

``````

For this project, I wanted to create something simple yet interesting. Instantly, I was reminded of legos because how it works is very simple – you simply stack the blocks on top of each other. However, there are millions of ways to apply this simple mechanism to create so many different things. Another source of inspiration for the project came from one of my favourite painter, Mondrian. The three primary colours combined with 90 degree angles are indications of my inspiration from Mondrian’s works.