Project 11 – Turtle Freestyle – John Legelis

sketch

// John Legelis
// Section D


var t // turtle
var yF = 273// Winning Y value
var w // State of play (Play, Win, or Lose)
var s // osilator incriment

//load background image
function preload() {
    maze = loadImage("https://i.imgur.com/OPGsUjg.png")
}

function setup() {
    createCanvas(400, 300);
    background(0);
    image(maze,0,0)
    
    fill(0,255,0)
    noStroke()
    rectMode(CORNER)
    rect(361, yF, 33,10)

    // initialize t
    t = makeTurtle(20,0)
    t.setWeight(4)
    mouseX = 20
    mouseY = 1 
    s = 0
    w = 0
    yF = 263
}

function draw() {
    //Oscilate between colors at winning and loosing screens
    s = s + 1
    if (s % 3 == 1){
        var lcol = [255,255,255]
        var wcol = [0,255,0]
    }
    else {
        var lcol = [255,0,0]
        var wcol = [255,255,255]
    }

    // retrive brightness at turtle location
    var p = maze.get(t.x, t.y)
    var Tbright = brightness(p)
    // If in "play" mode, move turtle towards the mouse
    if (w == 0) {
        theta = t.angleTo(mouseX, mouseY)
        t.right(theta)
        t.forward(1)
    }


    //if hit wall, lost
    if (Tbright < 100){
        w = -1
    }
    // if below "win line", won
    if (t.y > yF) {
        w = 1
    }
   
    // if lost...
    if (w == -1){
        background(lcol)
        stroke(255)
        textSize(50)
        text("G A M E O V E R", 10, 150)
        textSize(25)
        text("mouse in box to restart", 40, 30)
        noStroke()
        fill(0)
        rectMode(CENTER)
        rect(25,25, 10,10)
    }
    //if won...
    if (w == 1){
        background(wcol)
        stroke(255)
        textSize(50)
        text("! Y O U W O N !", 10, 150)
        stroke(255)
        textSize(25)
        text("mouse in box to restart", 40, 30)
        noStroke()
        fill(0)
        rectMode(CENTER)
        rect(25,25, 10,10)
    }

    //If lost or won and mouse is in box initialize and go back to "play" mode
    if (((w==-1) || w==1) & (mouseX > 25 - 10) && (mouseX < 25 + 10) && 
    (mouseY > 25 - 10) && (mouseY < 25 + 10)) {
        w = 0
        setup()
    }

    if (mouseIsPressed){
        w = 1
    }
}


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

When approaching this project I had an idea instantly to use the turtle to make some sort of maze game. Given our recent experience in using brightness and get image I was able to analyze a maze image and detect the walls very efficiently. I polished the game with a win and lose screen and a quick restart function based on mouse position.

Hannah Cai—Project 11—Composition

click to generate a new tree!

/* Hannah Cai
Section C
hycai@andrew.cmu.edu
Project-11-Composition
*/

//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(0),
                  weight: strokeWeight(w),
                  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;
}

/////my code

var angle;
var x;
var y;
var w = 5;
var minAngle = 1;
var maxAngle = 30;
var minRatio = .6;
var maxRatio = .9;

function setup() {
    createCanvas(480, 480);
    background(250);
    strokeJoin(MITER);
    strokeCap(PROJECT);
    frameRate(1);
}

function tree(length, turtle, w, r, l) {
  if (length > 10) { //create recursive branches
    ratio = random(minRatio, maxRatio)
    turtle.forward(length);
    turtle.setWeight(w * ratio)
    turtle.right(r);
    tree(length * ratio, turtle, w*ratio, random(minAngle,maxAngle), random(minAngle, maxAngle));
    turtle.left(r+l);
    tree(length * ratio, turtle, w*ratio, random(minAngle, maxAngle), random(minAngle, maxAngle));
    turtle.right(l);
    turtle.setWeight(w / ratio)
    turtle.back(length);
  } else { //draw flowers!
    turtle.setColor("Pink");
    turtle.setWeight(10);
    turtle.forward(10);
    turtle.setColor(0);
    turtle.setWeight(w);
    turtle.back(10);
  }
  noLoop();
}

function draw() {
    var turtle = makeTurtle(width / 2, height);
    //trunk
    turtle.penDown();
    turtle.right(270);
    turtle.forward(length);
    //branches
    tree(100, turtle, w, random(minAngle,maxAngle), random(minAngle,maxAngle))
}

//refresh the canvas when mouse is clicked
function mouseClicked() {
  setup();
  draw();
}

For this project, I was inspired by recursive trees, and I wanted to see if I could make one with turtle. Although it took a long time to figure out, I’m really proud of the end result! In the future, I’d want to try and animate the tree or make it interactive, although it might turn out to be too computationally expensive. Overall, I feel like I learned a lot about turtles and recursive functions, and I had a lot of fun!

Project-11 Composition-Veronica

sketch

//Veronica Wang
//Section B
//yiruiw@andrew.cmu.edu
//Project-11

var turtle = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 
var angle = 0; //current direction turtle is facing
var turnangle = 5; //turn angle
var incre = 0.1; //speed increment
var counter = 0; //how many times turtle had completed a full geometry

function setup() {
    createCanvas(480, 300);
    background(0);
    frameRate(10);

    //create array of turtles
    for (var i = 0; i < turtle.length; i++) {
       turtle[i] = makeTurtle(random(0, width), random(0, height), random(1, 3));
       turtle[i].setColor(color(random(255), random(255), random(255), 60));
    };
}

function draw() {
  //draw turtles
  for (var i = 0; i < turtle.length; i++) {
    turtle[i].penDown();
    turtle[i].setWeight(5);
    //change turtle direction after 100 instances
    if (counter < 100){
      if(angle < 0 || angle > 90){
            turnangle *= -5;
        }
      angle += turnangle;
      turtle[i].forward(1);
      turtle[i].right(turnangle);
      turtle[i].speed += incre;
      counter += 1;
    }
    turtle[i].speed += 0.5;
    turtle[i].forward(2);
    turtle[i].right(turnangle);
  }
    
}

function mousePressed(){
    turtle.push(makeTurtle(mouseX, mouseY, random(1, 3)))
    turtle[turtle.length - 1].setColor(color(random(255), random(100), random(255), 60));
    
}



//------------------------------------------------------------------------------------  


function turtleLeft(d) {
    this.angle -= d;
}

function turtleRight(d) {
    this.angle += d;
}

function turtleForward() {
    var rad = radians(this.angle);
    var newx = this.x + cos(rad) * this.speed;
    var newy = this.y + sin(rad) * this.speed;
    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) {
    var angle = this.angleTo(x, y);
    if (angle < 180) {
        this.angle += speed;
    } else {
        this.angle -= speed;
    }
}

function turtleSetColor(c) {
    this.color = c;
}

function turtleSetWeight(w) {
    this.weight = w;
}

function turtleFace(angle) {
    this.angle = angle;
}

function makeTurtle(tx, ty, vel) {
    var turtle = {x: tx, y: ty,
                  angle: 0.0, 
                  penIsDown: true,
                  color: color(128),
                  weight: 1,
                  speed: vel,
                  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;
}

In this project I wanted to make a somewhat random drawing machine that generates geometry based on rotation and random population. I liked the aggregation of triangles in this iteration and how the rotation made them look more circular/gear like.

Beginning stage 
More complex stage
Complex/chaotic stage

Austin Treu – Project 11

atreu-proj-11

/*Austin Treu
  atreu@andrew.cmu.edu
  Section B
  Project 11*/

var turt, mouseTurt, colCount = 0, revTurt, uLine = false,
    colArr = ['purple','orange','blue','yellow','green','red','pink'];

function setup() {
    createCanvas(480, 480);
    background(250);

    //display title
    textSize(48);
    fill("green")
    textAlign(CENTER);
    text("Turtle Draw!", width/2, 50);
    textSize(15);
    text('Click to draw, Press Space to clear,', width/2, 80);
    text('Press Enter to change color', width/2, 95);


    //make turtles
    turt = makeTurtle(width/4, 60);
    turtL = makeTurtle(0,0);
    turtR = makeTurtle(width,height);
    turtT = makeTurtle(width,0);
    turtB = makeTurtle(0,height);
    mouseTurt = makeTurtle(0,0);
    revTurt = makeTurtle(width, height);
}

function draw() {
    //turtle to draw underline/outline
    if(!uLine){
        turt.penDown();
        turt.setColor('blue');
        turt.setWeight(10);
        if(turt.x < 3*width/4)
            turt.forward(2);
        else
            uLine = true;
    }

    //turtle corresponding with mouse pos
    if(mouseIsPressed){
        mouseTurt.penDown();
        revTurt.penDown();
    }
    else{
        mouseTurt.penUp();
        revTurt.penUp();
    }
    mouseTurt.setWeight(5);
    mouseTurt.goto(mouseX, mouseY);
    var ind = colCount%colArr.length;
    mouseTurt.setColor(colArr[ind]);

    revTurt.setWeight(5);
    revTurt.goto(width-mouseX, mouseY);
    var ind = colCount%colArr.length;
    revTurt.setColor(colArr[ind]);

    keyPressed();
}

function keyPressed(){
    //reset the screen 
    if(keyCode === 32){
        background(255);
        //display title
        textSize(48);
        fill("green")
        noStroke();
        textAlign(CENTER);
        text("Turtle Draw!", width/2, 50);
        uLine = false;
        turt.goto(width/4,60)
    }

    //cycle colors
    if(keyCode === 13)
        colCount++;

    keyCode = -1;
}

//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 setColor(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: setColor, setWeight: turtleSetWeight,
                  face: turtleFace};
    return turtle;
}

I didn’t quite have an idea for what to do with this project at first, so I just started experimenting with the turtles and the mouse. Ultimately, I thought this was a pretty solid design that was fun to use.

Han Yu Project 11 Composition

click on Canvas to make spirals!

sketch

// Han Yu
// Section D
// hyu1@andrew.cmu.edu
// Project 11 Compostion

var ttl;

function setup() {
    createCanvas(400, 400);
    background(255, 200, 200);
    ttl = makeTurtle(width/2, height/2);
    ttl.setColor(255);
    ttl.setWeight(2);
    ttl.penDown();
    frameRate(50);
}

function draw() {
	var step = (frameCount/150);
	ttl.forward(step);
	ttl.left(5);
}

function mousePressed() {
	ttl = makeTurtle(mouseX, mouseY);
	var c = [255, 'purple', 255, 155]; //maximize the chances of getting a white stroke
	ttl.setColor(c[floor(random(0,4))]);
	ttl.setWeight((random(0,2)));

}

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

Spirals are my favorite patterns so I wanted to make my project with them. It was fun making variations in color and stroke but setting the steps for spirals are hard. Overall, doing this project has definitely made me more familiarized with turtles.

Screenshot of project.

JasonZhu_Project-11-Composition

sketch

/* Jason Zhu
Section E
jlzhu@andrew.cmu.edu
Project 11
*/

var bigturtle;

function setup() {
    createCanvas(450, 450);
    // set color variables for background
    var cx = constrain(mouseX, 0, 480);
	var cy = constrain(mouseY, 0, 480);
	var red = cx * .3
	var green = cy * .3
	var blue = 100
    background(red * .3 - 15, green * .3 - 15, blue * .3 - 15);
    // set stroke settings
    strokeJoin(MITER);
    strokeCap(PROJECT);
    // create turtle and adjust settings
    bigturtle = makeTurtle(width / 2, height / 2);
    bigturtle.setColor(255);
    bigturtle.setWeight(2);
    bigturtle.penDown();
    frameRate(999);
}

function draw() {
		for (var i=0;i<1;i++){
    	turtle = makeTurtle(-25,-25)
    	turtle.penDown
    }
        // set color variables for turtle
        var cx = constrain(mouseX, 0, 450);
		var cy = constrain(mouseY, 0, 450);
    	var red = cx * .58
		var green = cy * .58
		var blue = 108
		turtle.setColor(color(red,green,blue))
		turtle.setWeight(mouseY/20)
		turtle.penDown()
		turtle.forward(mouseY)// move depending on the y position of the mouse.
		turtle.right(90) // turn turtle right.
		turtle.forward(mouseX) // move again depending ont he xposition of the mouse.
		turtle.left(90) // turn turtle left.
		turtle.back(mouseX) // move again depending ont he xposition of the mouse.
		turtle.right(90) // turn turtle right.
		turtle.back(mouseX) // move again depending ont he xposition of the mouse.
		turtle.left(90) // turn turtle left.
		turtle.back(mouseX) // move again depending ont he xposition of the mouse.
	}

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

For this project I wanted to try to create hallway compositions with p5.js. I devised the following system in order to replicate hallways under a compliment color scheme when a user draws a line diagonally. Overall, I found the project stimulating and particularly informative in the inner workings of how the turtle function runs.

Example of a hallway being rendered with the code (1) when a line is drawing primarily diagonally towards the left.
Example of a hallway being rendered with the code (1) when a line is drawing primarily diagonally towards the right.
Free from random drawing.

Sophia Kim – Project 11 – Sec C

sketch

// Sophia S Kim
// Section C 1:30
// sophiaki@andrew.cmu.edu 
// Project-11-Turtle Freestyle

var strOpt = 1; 
var turtle1;
var turtle2;

function setup() {
  createCanvas(480, 400);
}
 
function draw() {
  background(175);
  if (strOpt === 1) {
    orangeLines();
  }
  if (strOpt === 2) {
    redLines();
  }
  if (strOpt === 3) {
    greenLines();
  }
  if (strOpt === 4) {
    purpleLines(); 
  }
}

//function for line 1 - longest line  
function line1(x, y, z){
  turtle1 = makeTurtle(x, y); 
  turtle1.setWeight(10); 
  turtle1.setColor(z);

  turtle1.right(30);
  turtle1.penDown();
  for (i = 0; i < 6; i++) {
    turtle1.forward(15);
    turtle1.right(50);
    turtle1.forward(15);
    turtle1.left(50);
  }
  turtle1.penUp();
}

//function for line 2 
function line2(a, b, n) {
  turtle2 = makeTurtle(a, b); 
  turtle2.setWeight(10); 
  turtle2.setColor(n);

  turtle2.left(30);
  turtle2.penDown();
  for (i = 0; i < 3; i++) {
    turtle2.forward(15);
    turtle2.left(50);
    turtle2.forward(15);
    turtle2.right(50);
  }
  turtle2.penUp();
}

//function for line 3 
function line3(j, k, f) {
  turtle3 = makeTurtle(j, k); 
  turtle3.setWeight(10); 
  turtle3.setColor(f);

  turtle3.right(50);
  turtle3.penDown();
  for (i = 0; i < 4; i++) {
    turtle3.forward(15);
    turtle3.right(50);
    turtle3.forward(15);
    turtle3.left(50);
  }
  turtle3.penUp();
}

//function for line 4 
function line4(l, m, p) {
  turtle4 = makeTurtle(l, m); 
  turtle4.setWeight(10); 
  turtle4.setColor(p);

  turtle4.right(-50);
  turtle4.penDown();
  for (i = 0; i < 4; i++) {
    turtle4.forward(15);
    turtle4.right(50);
    turtle4.forward(15);
    turtle4.left(50);
  }
  turtle4.penUp();
}

//function for line 5 
function line5(o, h, q) {
  turtle5 = makeTurtle(o, h); 
  turtle5.setWeight(10); 
  turtle5.setColor(q);

  turtle5.right(-80);
  turtle5.penDown();
  for (i = 0; i < 5; i++) {
    turtle5.forward(15);
    turtle5.right(50);
    turtle5.forward(15);
    turtle5.left(50);
  }
  turtle5.penUp();
}
//draws white lines, pink shapes, and green block in background
function orangeLines() {
  //makes the green block of color in background
  fill("GREEN");
  noStroke();
  beginShape();
  vertex(0, 150);
  vertex(480,75);
  vertex(480, 400);
  vertex(0, 400); 
  endShape(CLOSE);
  //line color orange
  line1(350, 30, "ORANGE"); 
  line2(50, 330, "ORANGE");
  line3(370, 270, "ORANGE");
  line4(50, 100, "ORANGE");
  line5(190, 190, "ORANGE");

}

//draws pink lines, white shapes, and blue block in background
function redLines() {
  //makes the blue block in background
  fill("BLUE");
  noStroke();
  beginShape();
  vertex(0, 140);
  vertex(480, 200);
  vertex(480, 400);
  vertex(0, 400); 
  endShape(CLOSE);

  //line color red 
  line1(20, 240, "RED");
  line2(250, 90, "RED");
  line3(100, 90, "RED");
  line4(340, 360, "RED");
  line5(300, 270, "RED");
}

//draws green lines, yellow shapes, and pink block in background 
function greenLines() {
  //makes pink block in background
  fill(255, 125, 125);
  noStroke();
  beginShape();
  vertex(0, 150);
  vertex(420, 0);
  vertex(480, 0);
  vertex(480, 400);
  vertex(0, 400); 
  endShape(CLOSE);

  //draw green line 
  line1(60, 250, "GREEN");
  line2(370, 350, "GREEN");
  line3(390, 50, "GREEN");
  line4(50, 90, "GREEN");
  line5(220, 250, "GREEN");
}

//draws orange lines, blue shapes, and yellow block in background
function purpleLines() {
  //makes orange block in background
  fill("GOLDENROD");
  noStroke();
  beginShape();
  vertex(0, 0);
  vertex(100, 0);
  vertex(480, 300);
  vertex(480, 480); 
  vertex(0, 480);
  endShape(CLOSE);

  //draws purple line
  line1(350, 50, "PURPLE");
  line2(120, 90, "PURPLE");
  line3(90, 190, "PURPLE");
  line4(350, 380, "PURPLE");
  line5(220, 200, "PURPLE");
}

function mousePressed() {
  strOpt ++; // mouse is pressed every time changes the lines & line colors
  if (strOpt > 4) {
    strOpt = 1; 
  }
}


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

Starting this project, I really wanted crinkle-cut fries. Also, in one of the studio classes I am taking for design, we talked about color theory, so I decided to combine both the “crinkle-cut fries” idea and color theory into this project. It was definitely really fun to experiment with turtles.

first of four frames
last of four frames

Nina Yoo – Project 11 – Composition

sketch

/* Nina Lee Yoo
Section E
nyoo@andrew.cmu.edu
Project-11:A Composition*/

function setup() {
    createCanvas(480, 480);
    background(220);


    }


function draw() {

	for (var t=0;t<10;t++){
    	turtle = makeTurtle(0,0)
    	turtle.penDown
    }
	
		var r = (random(0,200));
		var g = (random(50,100));
		var b = (random(100,150));
		
	
	


		turtle.setColor(color(r,g,b))
		turtle.setWeight(1)
		turtle.penDown()
		turtle.forward(mouseX)// moving the initial fowards with them mouseX
		turtle.right(50)
		turtle.forward(mouseY) //movingthesecondfoward(creating longer lengthfor th first line)
		turtle.right(50)
		turtle.forward(100)
		turtle.right(mouseX)//(manipulating the end of the string)
		turtle.forward(100)
		turtle.right(20)
		turtle.forward(100)

		

}







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


It was cool seeing how if you changed the points on the turtle with mouseX or mouseY how it affected the whole drawing. It was difficult to grasp the concept at first due to my idea of turtles being static, but when doing the project it has helped me understand the underlying idea of turtle.

Xindi Lyu-Project 11-Composition

sketch

 
/*
week 10-assignment-b
Xindi Lyu
Section A
xindil@andrew.cmu.edu
*/
 var T1
 var T2
 var T3
 var T4
 //make the hexagons
 
function setup(){
createCanvas(600,400);
background(255);
T1=makeTurtle(width/2,height/2);
T1.setColor(color("pink"));
T1.setWeight(5);
T1.penDown();
T2=makeTurtle(width/20,height/4);
T2.setColor("green");
T2.setWeight(5);
T2.penDown();
frameRate(10);
T3=makeTurtle(width*9/10,height/2);
T3.setColor(color("blue"));
T3.setWeight(5);
T3.penDown();

}
//get the hexagons to orient in a golden ratio spiral
function draw(){
  T1.forward(6);
  T1.left(random(0,6));
    T1.turnToward(mouseX,mouseY,10);
    T2.forward(6);
  T2.left(random(0,6));
    T2.turnToward(mouseX,mouseY,10);

 T3.forward(6);
  T3.left(random(1,6));
    T3.turnToward(mouseX,mouseY,10);





}



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

For this project I created three turtles following the cursor while maintaining their own circulating procedure. It resulted in a very interesting knotting effect.

Alessandra Fleck – Project 11

sketch

//Name: Alessandra Fleck 
//Class Section : B
//Email: afleck@andrew.cmu.edu
//Project-11


var startFrame;


function setup() {
    createCanvas(480, 480); // create the canvas   
    
}


function draw() {
    background(40,41,35); //setting background color
    var turtle = makeTurtle(80,80); //setting turtle variable



    noFill();
    stroke(0);
    strokeWeight(10);

    turtle.setWeight(20); // make turtle weight 

    // go to starting position but dont start drawing

    //First Iteration of 
    turtle.penUp();

    //placeholder for where mouse should go
    //North Position
    fill('red');
    noStroke();
    ellipse(230,10,30,30);
    //South Position
    fill('red');
    noStroke();
    ellipse(230,450,30,30);
    //West Position
    fill('red');
    noStroke();
    ellipse(10,240,30,30);
    //East Position
    fill('red');
    noStroke();
    ellipse(470,240,30,30);

    for(var i = 0; i<100; i++){

        turtle.penUp();
        turtle.goto(mouseX,150); //set triangle position_01
        turtle.penDown();

        turtle.forward(mouseX);
        turtle.face(-120);
        turtle.forward(mouseX);
        turtle.face(120);
        turtle.forward(mouseY); // creates vertical growth when mouse X is moving in horizontal direction, Y is stretched
    
    }

    for(var i = 0; i<50; i++){
        turtle.penUp();
        turtle.goto(mouseX,150); //set triangle position_02
        turtle.penDown();

        turtle.forward(mouseX);
        turtle.face(-120);
        turtle.forward(mouseX);
        turtle.face(-120);
        turtle.forward(mouseY);
    
    }


    for(var i = 0; i<100; i++){
        turtle.penUp();
        turtle.goto(mouseX,150); //set triangle position_03
        turtle.penDown();

        turtle.forward(mouseX);
        turtle.face(60); // smaller forward motion
        turtle.forward(mouseX);
        turtle.face(60);
        turtle.forward(mouseY);
    
    }

    for(var i = 0; i<100; i++){
        turtle.penUp();
        turtle.goto(mouseY,150); //set triangle position_04
        turtle.penDown();

        turtle.forward(mouseY);
        turtle.face(-120);
        turtle.forward(mouseY);
        turtle.face(120);
        turtle.forward(mouseX); // when mouse moves in Y direction the lines also stretch in x direction
    
    }

    for(var i = 0; i<100; i++){
        turtle.penUp();
        turtle.goto(mouseY,150); //set triangle position
        turtle.penDown();

        turtle.forward(mouseY);
        turtle.face(-60); //smaller forward motion reversed
        turtle.forward(mouseY);
        turtle.face(60);
        turtle.forward(mouseX);
    
    }

    

}





//setting the Turtle functions 


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

 

For this project I wanted to explore how moving the mouse to different part of the canvas could create different distinct images from the intersection of a couple lines. As the mouse moves in the X direction, the Y direction lines are shifted vertically so that some go off the canvas or move up/down. This dynamic  movement makes the drawing full at times and have less lines in others.