Fractal Apple

pic00107

//variable
float theta;   
int r = 10;
int g = 255;
int b = 255;
int f = frameCount;
int toggle = 0;
float s = 0.66;



void setup() {
  size(1000, 640);
}

void draw() {
  background(0);
  frameRate(-1);
  stroke(r,g,b);

//control code
//red control  
  if(keyPressed){
    if(key == 'r'){
      if(r > 255){
        r = 10;
      }
      else{
        r += 5;
      }
    }
  }
//green control 
  if(keyPressed){
    if(key == 'g'){
      if(g > 255){
        g = 10;
      }
      else{
        g += 5;
      }
    }
  }
//blue control
  if(keyPressed){
    if(key == 'b'){
      if(b > 255){
        b = 10;
      }
      else{
        b += 5;
      }
    }
  }
//size control
  if(keyPressed){
    if(keyCode == UP){
      if(s >= 0.7){
        s = 0.7;
      }
      else{
        s += 0.01;
      }
    }
    if(keyCode == DOWN){
      if(s < 0.2){ s = 0.2; } else{ s -= 0.01; } } } if(keyPressed){ if(key == 'c'){ r = 0; g = 255; b = 255; } } if(keyPressed){ if(key == 'y'){ r = 255; g = 255; b = 0; } } if(keyPressed){ if(key == 'p'){ r = 255; g = 0; b = 255; } } if(keyPressed){ if(key == 'w'){ r = 255; g = 255; b = 255; } } float a = (frameCount / (float) height) * 180f; // Convert it to radians theta = radians(a); // Start the tree from the middle of the screen translate(width/2,(height/2)+120); // Draw a line 60 pixels //line(0,0,0,-120); // Move to the end of that line translate(0,-120); // Start the recursive branching! branch(120); branch2(120); branch3(120); branch4(120); } void branch(float h) { //branch size h *= s; // All recursive functions must have an exit condition!!!! // Here, ours is when the length of the branch is 2 pixels or less if (h > 1) {
    pushMatrix();    // Save the current state of transformation (i.e. where are we now)
    rotate(theta);   // Rotate by theta
    line(0, 0, 0, h);  // Draw the branch
    translate(0, h); // Move to the end of the branch
    branch(h);       // Ok, now call myself to draw two new branches!!
    popMatrix();     // Whenever we get back here, we "pop" in order to restore the previous matrix state
    
    // Repeat the same thing, only branch off to the "left" this time!
    pushMatrix();
    rotate(-theta);
    line(0, 0, 0, h);
    translate(0, h);
    branch(h);
    popMatrix();
  }
}

void branch2(float h) {
  
  h *= s;
  
  if (h > 1) {
    pushMatrix();    
    rotate(theta);   
    line(0, 0, 0, -h);  
    translate(0, -h); 
    branch2(h);       
    popMatrix();     
    
    
    pushMatrix();
    rotate(-theta);
    line(0, 0, 0, -h);
    translate(0, -h);
    branch2(h);
    popMatrix();
  }
}


void branch3(float h) {
  h *= s;
  
 
  if (h > 1) {
    pushMatrix();    
    rotate(theta);   
    line(0, 0, 0, -h); 
    translate(0, -h); 
    branch3(h);       
    popMatrix();     
    
  
    pushMatrix();
    rotate(-theta);
    line(0, 0, 0, h);
    translate(0, h);
    branch3(h);
    popMatrix();
  }
}

void branch4(float h) {
  h *= s;
  
  if (h > 1) {
    pushMatrix();    
    rotate(theta);   
    line(0, 0, 0, h);  
    translate(0, h); 
    branch4(h);       
    popMatrix();      
    
    
    pushMatrix();
    rotate(-theta);
    line(0, 0, 0, -h);
    translate(0, -h);
    branch4(h);
    popMatrix();
  }
}

This program, coded in Processing, is a modified version of the commonly seen “Fractal Tree” code in which a line branches out into two lines, with each iteration smaller than the original. The angle that the lines split from is determined by the framecount, causing the movement.
Link to OpenProcessing page

Leave a Reply

Your email address will not be published. Required fields are marked *