Michal Luria – Project 11 – Composition

mluria-11

/*  Submitted by: Michal Luria
    Section A: 9:00AM - 10:20AM
    mluria@andrew.cmu.edu
    Assignment-11-Project
*/

var originalImage;
var px = 0;
var py = 0;
var currentColor;
var pBrightness;

function preload() {
    var myImageURL = "http://i.imgur.com/cU9Kpic.jpg"; //image link
    originalImage = loadImage(myImageURL); //load image
}

function setup() {
    createCanvas(500, 667); 
    background(255);
    originalImage.loadPixels(); //load pixels

}

function draw() {


    //create new turtle
    var turtle = makeTurtle(px, py);

    //settings for turtle
    turtle.setColor(0);
    turtle.setWeight(4);


    while (px < width) { //check first line

        currentColor = originalImage.get(px,py); //fetch color of photo

        if (isColor(currentColor)) { //check that it is a color
            pBrightness = brightness(currentColor); //what is the brightness
        }

        if (pBrightness < 50) { //if the brightness is less than 50 

            //draw a short black line
            turtle.goto(px,py); 
            turtle.penDown();
            turtle.forward(6);
            turtle.penUp();

        }

        px +=6; //adjust px location

    }

    py += 4; //move to next line
    px = 0; //start over on the x axis

}

//check the get function fetched the color
function isColor(c) {
    return (c instanceof Array);
}

//---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, drawPent: turtle};
return turtle;}

This week for my project I wanted to use a turtle to create a graphics project that would look different using a turtle than other methods. I decided to compute a “cartoon” version, a solid black and white picture, from a regular photo. Using this method, the photo can be recreated by computing it in slightly different ways – each small variation would create a very different and new composition from the same source. Furthermore, I liked how any photo can be uploaded into the code and by only changing the photo link – instantly create a new image. The one presented is the version that appealed most to me.

Leave a Reply