owen fox project 11

 

turtle

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

var progress = 0;

//color values
var r = 0;
var g = 0;
var b = 0;
//constant
var c = 50;
//where the gradient stops and the color UI begins
var ylimit = 90;

function setup() {
    createCanvas(400,400);
    background("white");
}


function draw() {

   //draws a turtle square at every 3 by 3 pixels
    for (var x = 0; x < width/3; x ++) {
        for (var y = 0; y < width/3; y ++) {
            if (y < ylimit) {
            turtleSquare(r - x,g - x,b,x*3,y*3);
            }
        }
    }

    //prints colot values as text
    textAlign(CENTER);
    textSize(14);
    fill("white");
    text("red:" + r, c, height- c);
    text("green:" + g, width/2, height- c);
    text("blue:" + b, width - c, height- c);

}

function mouseClicked() {
    //whenever a user clicks on the three text graphics created in draw, they will change the value of the color by + 10
    if ((mouseX > c/2) & (mouseX < c + c/2) && (mouseY > height - c - c/3) && (mouseY < height - (c - c/3))) {
        background("white");
        r += 10;
    }
    if ((mouseX > width/2 - c/2) & (mouseX < width/2 + c/2) && (mouseY > height - c - c/3) && (mouseY < height - (c - c/3))) {
        background("white");
        g += 10;
    }
    if ((mouseX > width - c - c/2) & (mouseX < width - c/2) && (mouseY > height - c - c/3) && (mouseY < height - (c - c/3))) {
        background("white");
        b += 10;
    }
}

function turtleSquare(r,g,b,x,y) {
    //uses turtle graphics to draw a 3 by 3 square
    var turtle = makeTurtle(x,y);
    turtle.setColor(color(r,g,b))
    turtle.setWeight(3);
    turtle.penDown();
    turtle.forward(3);
    turtle.right(90);
    turtle.forward(3);
    turtle.right(90);
    turtle.forward(3);
    turtle.right(90);
    turtle.forward(3);
    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;
}

uses small turtle squares to create a user generated gradient.

clicking on the color labels changes their value.

gradient-2gradient-1

Leave a Reply