Miranda-Luong-Project 11

sketch

/* Miranda Luong
Section E
mluong@andrew.cmu.edu
Project-11-Composition
*/

var myTurtle

function setup() {
    createCanvas(480, 480);
    background(0);
    myTurtle = makeTurtle(width/2,height/2); //places starting turtle at center
    myTurtle.setColor('black');
    myTurtle.setWeight(6);
}

// When the user clicks the mouse
function mousePressed() {
    //turtle moves to mouse
    myTurtle.goto(mouseX,mouseY);
    //if mouse is south of canvas' horizontal center, pen is red
    if (mouseY > height/2){
      myTurtle.setColor('red');
    }
    //if mouse is north of canvas' horizontal center, pen is blue
    if (mouseY < height/2){
      myTurtle.setColor('blue');
    }
    //size of circle is random from range between 5 and 20
    var size = random(5,20);
    //draw circle
    for (var i = 0; i < 360; i+=size){
        myTurtle.penDown();
        myTurtle.right(size);
        myTurtle.forward(10);
        myTurtle.penUp();
    }
}


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(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;
}

I went for a pretty simple drawing. When a visitor clicks on the canvas, a circle is drawn. The colors of these circles change according to where they clicked.

Leave a Reply