Yingyang Zhou-Project-11-Composition

click to plant trees!

//Yingyang Zhou
//yingyanz@andrew.cmu.edu
//Project 11
//section A


var turtle;

var treeX = 300;
var treeY = 400;

function setup() {
    createCanvas(600, 400);
    background(224, 216, 208);

    frameRate(1);
}

function draw(){
    turtle = makeTurtle(treeX, treeY);
    turtle.setColor(color(84, 60, 43));
    turtle.setWeight(7);
    turtle.penUp();

    var trunkRandomDegree = random(5);
  	var trunkRandomHeight = random(minTrunk,maxTrunk);
    turtle.penDown();
    turtle.left(90+trunkRandomDegree);
  	turtle.forward(trunkRandomHeight);
  	turtle.right(90+trunkRandomDegree);
  	turtle.penUp();
  	noLoop();

  	turtle.setColor(color(119, 166+random(-20,20), 119));
  	branch(turtle, floor(random(1, 5)), 10, 3, random(5), random(5));

}
var height = 400;
var minTrunk = height/4;
var maxTrunk = height/3;
var minBranch = height/10;
var maxBranch = height/3;
var minDegree = 3;
var maxDegree = 45;


function branch(turtle, number, length, width, left, right){
  var number = floor(random(1, 5));
  var length = 7;
  var width = 0.5;
  for(var i = 0; i < number; i++){
  		turtle.penDown();
  		turtle.left(90+random(minDegree,maxDegree));
  		turtle.forward(length);
  		branch(turtle, number/i, length, width, random(5), random(5));
  		length = length/i;
  		width = width/i;

  }

  turtle.penDown();

  turtle.left(90+trunkRandomDegree);
  turtle.forward(-trunkRandomHeight/2);
  turtle.penDown();
  turtle.left(random(minDegree))

  turtle.left(random(minDegree,maxDegree));
  turtle.forward();
  for(var i = 0; i < 6; i++){
    turtle.penDown();
    turtle.forward(5);
    turtle.right(60);
  }
  turtle.penUp();
}

function mousePressed(){
	treeX = mouseX;
	treeY = mouseY;
	// setup();
	draw();
}



///////////////////////////////////////////////////////////////////////
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 enjoyed doing this project and the randomness turned out a little surprising but I like it! The idea is when you click on the canvas, there would be a tree grow from where you clicked.

Leave a Reply