cduong-project 11-Composition

sketch

//Name: Colleen Duong
//Email: cduong@andrew.cmu.edu
//Section: d
//Assignment-10-b

//  cows = loadImage("https://i.imgur.com/cqxAWPM.jpg");


var cows;
var t1;
var t2;
var t3;
var t4;
var t5;
var t6;

function preload() {
  cows = loadImage("https://i.imgur.com/cqxAWPM.jpg");  //Cow Image Preload
}

function setup() {
  createCanvas(480, 480); //Change
  background(0);
  cows.loadPixels();
  frameRate(2000);
  t1 = makeTurtle(width/2, height/2); //Starts from the center of the canvas
  t2 = makeTurtle(width/2, height/2);
  t3 = makeTurtle(width/2, height/2);
  t4 = makeTurtle(width/2, height/2);
  t5 = makeTurtle(width/2, height/2);
  t6 = makeTurtle(width/2, height/2);
}

function draw() {
    var color1 = cows.get(t1.x, t1.y);  //Get colors of the picture
    var color2 = cows.get(t2.x, t2.y);
    var color3 = cows.get(t3.x, t3.y);
    var color4 = cows.get(t4.x, t4.y);
    var color5 = cows.get(t5.x, t5.y);
    var color6 = cows.get(t6.x, t6.y);
    t1.setColor(color1);  //Sets color for each line
    t2.setColor(color2);
    t3.setColor(color3);
    t4.setColor(color4);
    t5.setColor(color5);
    t6.setColor(color6);
    t1.setWeight(3);
    t2.setWeight(3);
    t3.setWeight(3);
    t4.setWeight(3);
    t5.setWeight(3);
    t6.setWeight(3);

//Turtles move towards the mouse
    t1.forward(10);
    t2.forward(10);
    t3.forward(10);
    t4.forward(10);
    t5.forward(10);
    t6.forward(10);

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

//
  t1.turnToward(targetX, targetY, 2); //Mouse controls line
  t2.turnToward(targetX, targetY, 4);
  t3.turnToward(targetX, targetY, 6);
  t4.turnToward(random(width), random(height), 8);  //Lines are drawn randomly
  t5.turnToward(random(width), random(height), 10);
  t6.turnToward(random(width), random(height), 12);
//creates circles when drawing over
}



//TURTLE CODE
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 really enjoyed the concept of the portrait project that we did last so I wanted to do another version of it using turtles instead for this week’s assignment. I also wanted users to somewhat be able to control where the lines go.


Final product-ish

Leave a Reply