Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
float x = 100;
float y = 300;
int rgb1 = 102;
int rgb2 = 153;
int rgb3 = 204;
int randomSeedNumber = 10;
boolean night = false;

void setup(){
  size(800, 800);
  smooth();
}

void draw(){
  //Center all ellipses
   ellipseMode(CENTER);
  
  //draw, rotate sun and moon
  pushMatrix();
  translate(400, 550);
  rotate(radians(x/1.10));
    //sky
    noStroke();
    fill(rgb1, rgb2, rgb3);
    ellipse(0, 0, 2000, 2000);
  
    //sun
    fill(255, 215, 0);
    ellipse(-470, 100, 100, 100);
  
    //moon
    fill(255);
    ellipse(470, -100, 100, 100);
   popMatrix();
  
  //stars
  if (night) {
    fill(255);
    randomSeed(randomSeedNumber);
    for (int i = 0; i < 150; i++){
      ellipse(random(0, 800), random(0, 700), 3, 3);
    }
  }
  
  //ground
  fill(105, 139, 34);
  noStroke();
  ellipse(400, 800, 800, 300);
  ellipse(0, 800, 200, 200);
  ellipse(800, 800, 200, 200);
  
  drawTree();
  drawMainBranch();
  drawCenterpiece();
}

/*__________________Drawing______________________*/
//drawTree          line  67
//drawHalfTrunk     line  83
//drawCenterpiece   line  119
//drawMainBranch    line  130
//MainBranchCopy    line  143
//positionBranches  line  153
//drawBranches      line  171
//drawSubBranch     line  186

void drawTree(){
  drawHalfTrunk();
  positionBranches();
  MainBranchCopy();
  
  //mirror firstHalfOfTree
  pushMatrix();
  translate(400, 400);
  scale(-1, 1);
  translate(-400, -400);
    drawHalfTrunk();
    positionBranches();
    MainBranchCopy();
  popMatrix();
}

void drawHalfTrunk(){
  noStroke();
  fill(139, 115, 85);
  
  //trunkPiece a
  beginShape();
    vertex(300, 700);
    bezierVertex(380, 690, 370, 550, 370, 400); //300, 700 - 370, 400
    bezierVertex(370, 340, 330, 300, 240, 280); //370, 400 - 300, 350
    bezierVertex(360, 300, 400, 350, 400, 400); //300, 350 - 400, 400
    bezierVertex(400, 650, 390, 690, 300, 700); //400, 400 - 300, 700
  endShape();
  
  //trunkPiece b
  pushMatrix();
    translate(355, 300);
    rotate(radians(-15));
    translate(-355, -300);
    beginShape();
      vertex(360, 335);
      bezierVertex(280, 280, 390, 260, 370, 200); //360, 335 - 370, 250
      bezierVertex(390, 260, 300, 280, 360, 335); //350, 250 - 360, 355
    endShape();
   popMatrix();
   
   //trunkPiece c
  beginShape();
   vertex(400, 720);
   bezierVertex(350, 650, 380, 600, 380, 550); //400, 720 - 380, 550
   bezierVertex(390, 300, 390, 250, 300, 205); //380, 550 - 280, 220
   bezierVertex(320, 210, 380, 250, 390, 300); //280, 220 - 380, 300
   bezierVertex(400, 350, 400, 400, 400, 720); //380, 300 - 400, 720
  endShape();
}

//draw topmost branch (not to be mirrored)
void drawCenterpiece(){
  noStroke();
  fill(139, 115, 85);
  
  beginShape();
    vertex(380, 400);
    bezierVertex(380, 350, 420, 250, 410, 180);
    bezierVertex(430, 250, 380, 350, 425, 700);
  endShape();
}

void drawMainBranch(){
  noFill();
  stroke(139, 115, 85);
  beginShape();
    vertex(410, 180);
    bezierVertex(410, 120, 450, 90, 450, 50);
    bezierVertex(450, 40, 440, 15, 425, 15);
    bezierVertex(400, 15, 395, 40, 395, 50);
    bezierVertex(395, 70, 410, 80, 420, 80);
    bezierVertex(435, 80, 445, 65, 430, 50);
  endShape();
}

void MainBranchCopy(){
  pushMatrix();
  translate(425, 155);
  scale(-1, 1);
  rotate(radians(45));
  translate(-300, -175); //-increase moves right, -increase moves up
    drawMainBranch();
  popMatrix();
}

void positionBranches(){
  //position branches
  pushMatrix();
  translate(425, 417);
  rotate(radians(-90));
  translate(-275, -350);
    drawBranches();
  popMatrix();
  
  pushMatrix();
  translate(425, 417);
  rotate(radians(-20));
  scale(0.6, 0.97);
  translate(-410, -400);
    drawBranches();
  popMatrix();
}

void drawBranches(){
  drawMainBranch();
  
  drawSubBranch();
  
  //mirror subBranch
  pushMatrix();
  translate(490, 165);
  scale(-1, 1);
  rotate(radians(-30));
  translate(-343, -120);
    drawSubBranch();
  popMatrix();
}

void drawSubBranch(){
  noFill();
  stroke(139, 115, 85);
  
  beginShape();
    vertex(417, 140);
    bezierVertex(425, 120, 440, 110, 460, 110);
    bezierVertex(480, 110, 490, 120, 490, 140);
    bezierVertex(490, 160, 475, 165, 460, 165);
    bezierVertex(440, 160, 450, 140, 460, 140);
  endShape();
}

/*__________________Mouse Events______________________*/


void mouseDragged(){
  x = mouseX;
  y = mouseY;
  
  if ((x > 199 && x < 401) || (x > 599 && x < 801)){
    rgb1 = 47;
    rgb2 = 48;
    rgb3 = 79;
    night = true;
  } else if ((x > -1 && x < 200) || (x > 400 && x < 600)){
    rgb1 = 102;
    rgb2 = 153;
    rgb3 = 204;
    night = false;
  } 
}