Owen Fox Project 10

landscape

//Owen Fox
//olf@andrew.cmu.edu
//Section C
//Project 10

//initialize movement variable
var a = 0;

function setup() {
    createCanvas(600,400);
    background(255);
    //fill canvas with green turtle squares of varying shades of green
    for (var x = 0; x < width; x += 4) {
        for (var y = 0; y < height; y += 4) {
        var c = color(random(50),random(255),random(50));
        turtlePixel(x,y,c);
        }
    }

    frameRate(48);
}

function draw() {
   //draw new turtle squares in sweeping diagonals across the canvas
    for(var y1 = 0; y1 < height; y1 += 4) {
        var c1 = color(random(50),random(255),random(50));
        turtlePixel(a * 4,y1,c1);
        a = a + 1;
        //resets animation when a is too large
        if(a * 4 > width) {
        a = 0;
        }
    }

}

function turtlePixel(x,y,c) {
var turtle = makeTurtle(x,y);
turtle.setWeight(4);
turtle.setColor(c);
turtle.penDown();
turtle.forward(4);
turtle.right(90);
turtle.forward(4);
turtle.right(90);
turtle.forward(4);
turtle.right(90);
turtle.forward(4);
turtle.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;
}

originally my idea was like this:

20161104_233413

where random party people, pets and decorations would be generated, but that was too hard and I gave up.

I decided to make something vaguely pointillist instead, because I figured that would simplify the coding process.

heres my brainstorm that’s closer to the finished product:

20161104_233446

Leave a Reply