Elena Deng-Project 11-Turtles

sketch

/*Elena Deng
Section E
  edeng1@andrew.cmu.edu
  Project-11
}
*/

var t = [];
function setup() {
    createCanvas(400, 400);
    background(10);
    for(var i = 0; i < t.length; i++){ //sets various numbers of turtles
        t[i] = makeTurtle(width/2, height / 2 + random(-100, 100));
        t[i].setColor(color(random(200), random(200), random(150), 100));

    }
}

function draw() {
    var targetX = mouseX + noise(frameCount / 100); //sets the target
    var targetY = mouseY + noise(frameCount / 100);

    strokeWeight(5);

    point(targetX, targetY); //turtle will always be inclined to go to target x and y

    for (var a = 0; a < t.length; a++){
      t[a].setWeight(random(1,5))
      t[a].penDown();
      t[a].forward(1.5);
      t[a].turnToward(targetX, targetY,1);
        t[a].left(random(-5,5));} //draws the turtle
}

function mousePressed(){ //sets the number of turtles and increases it when mouse is pressed
  var newTurtle = makeTurtle(mouseX, mouseY);
  newTurtle.setColor(color(random(150), random(150), random(20), 100));
	t.push(newTurtle); //push turtle into t array

}

//////////////////////////

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

This week I was really excited to revisit the concept of turtles. I wanted to revisit the lines seen in the title notes from class. If you click on the screen, a new line will form and attempt to follow the mouse location. Continue clicking and you can see the way the turtle attempts to reach the mouse location more clearly.

View after adding a number of turtles

Leave a Reply