Project 11, odh

I chose to make two turtles that would follow the mouse to reveal and image below. I gave each turtle a different angle of rotation to give them a “dance-like” feel, while they reveal an abstract picture.

Final Image:

odhP11

//Owen D Haft
//Section D
//odh@andrew.cmu.edu
//Project 11

//Declares the image
var abstract;

//Declares the turtles
var turtle1;
var turtle2;

function preload() {
    abstract = loadImage("https://i.imgur.com/ve9JtKn.jpg");
}

function setup() {
    createCanvas(480, 480);
    background(100, 100, 200);
    abstract.loadPixels();
    frameRate(20000);

    //Sets the start point of the turtles
    turtle1 = makeTurtle(width/2, height/2);
    turtle2 = makeTurtle(width/2, height/2);
}

function draw() {

    //Pulls the color from the image
    var color1 = abstract.get(turtle1.x, turtle1.y); //Gets the color from the image 
    var color2 = abstract.get(turtle2.x, turtle2.y);

    turtle1.setColor(color1);
    turtle1.setWeight(10);

    turtle2.setColor(color2);
    turtle2.setWeight(10);

//Turtles move towards the mouse
    turtle1.forward(10);
    turtle2.forward(10);

//Target is the mouse location
    var targetX = mouseX;
    var targetY = mouseY;

    turtle1.turnToward(targetX, targetY, 4); 
    turtle2.turnToward(targetX, targetY, 6); 
}


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

Leave a Reply