Recursive tree painting problem

Hi guys, I have a problem with understanding how to work with Recursive Tree. I have to built something like a tree with green leaves (photo below), however I have only a default recursive tree.

Task:
Make a green like tree

Do u have any ideas how to make it? Thanks in advance for any help

Code:

float theta;   
float b = 1;
int flag = 1;
  
void setup() {
  size(640, 360);
}

void draw() {
  if (b <= 90 && flag == 1) {
    b++;
    theta = radians(b);
    if(b == 90)
      flag = 0;
  }
  if(b >= 0 && flag ==0){
    
    b--;
    theta = radians(b);
    
    if(b == 0)
      flag = 1;

  }
  background(0);
  frameRate(30);
  stroke(255);

  translate(width/2,height);

  line(0,0,0,-120);

  translate(0,-120);

  branch(120);
  }
  


void branch(float h) {

  h *= 0.66;
 

  if (h > 2) {
    pushMatrix();    
    rotate(theta);  
    line(0, 0, 0, -h);  
    translate(0, -h); 
    branch(h);       
    popMatrix();     
   
  
    
    pushMatrix();
    rotate(-theta);
    line(0, 0, 0, -h);
    translate(0, -h);
    branch(h);
    popMatrix();
  
  
  }
}

Photo:

Preformatted text

Right now, your tree branches until h is less than 2. That’s a fine, but it makes it slightly harder to map the recursive value to other things - like trunk width and branch color.

Instead, consider re-writing it so that the recursive value is how many more branches you have left to draw. If you also use that value as the stroke width, your tree will thin out naturally. You can even calculate how long a branch should be! It’d be 120 * 0.6^(level).

Once the level gets down to 0 or 1, you can switch to using a green stroke for leaves instead.

2 Likes

Aha, I understood how it works, but I have another problem now

Problem:

image

new code:

float theta;   
float b = 1;
int flag = 1;
int leftToDraw = 12;
   
void setup() {
  size(640, 360);
}

void draw() {
  if (b <= 90 && flag == 1) {
    b++;
    theta = radians(b);
    if(b == 90)
      flag = 0;
  }
  if(b >= 0 && flag ==0){
    
    b--;
    theta = radians(b);
    
    if(b == 0)
      flag = 1;

  }
  background(0);
  frameRate(30);
  stroke(255);

  translate(width/2,height);

  line(0,0,0,-120);

  translate(0,-120);

  branch(120);
  }
  


void branch(float h) {

  h *= 0.66;
 

  if (leftToDraw >= 0) {
    pushMatrix();    
    rotate(theta);  
    line(0, 0, 0, -h);  
    translate(0, -h);
    branch(h);       
    popMatrix();     
    leftToDraw--;  
  
    pushMatrix();
    rotate(-theta);
    line(0, 0, 0, -h);
    translate(0, -h);
    branch(h);
    popMatrix();
    leftToDraw--;  
  }
}

Maybe I`ve done something wrong

You’re recusing almost endlessly. Since your recursive value only gets cut down by a third each time, it’s pretty much always greater than or equal to zero for a few hundred recurses!

This is yet another reason I suggest you use a LEVEL (not a LENGTH) for the recursing value.

Consider:

void branch( int level ){
  if( level == 0 ){ return; }
  strokeWeight( level );
  stroke( 200,0,0 );
  if( level == 1 ){ stroke( 0, 200, 0); }
  pushMatrix();    
  line(0, 0, 0, -10 * level);  
  translate(0, -10 * level);
  branch(h-1);
  popMatrix();
}

Make sure you don’t call this with branch(120)!!!

A better call would be branch(10). That is, you only want it to go 10 levels deep.

1 Like

Okay, I`ve got this one
image

float theta;   
float b = 1;
int flag = 1;
   
void setup() {
  size(640, 360);
}

void draw() {
  if (b <= 90 && flag == 1) {
    b++;
    theta = radians(b);
    if(b == 90)
      flag = 0;
  }
  if(b >= 0 && flag ==0){
    b--;
    theta = radians(b);
    if(b == 0)
      flag = 1;
  }
  background(0);
  frameRate(30);
  stroke(255);

  translate(width/2,height);

  line(0,0,0,-120);

  translate(0,-120);

  branch(10);
  }
  


void branch( int level ){
  if( level == 0 ){ return; }
  strokeWeight( level );
  stroke( 200,0,0 );
  if( level == 1 ){ stroke( 0, 200, 0); }
  pushMatrix();    
  line(0, 0, 0, -10 * level);  
  translate(0, -10 * level);
  branch(level-1);
  popMatrix();

}

It’s hard to get all the details right. Here:

float theta = QUARTER_PI/2;   

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

void draw() {
  background(0);
  translate(width/2, height);
  
  // Single trunk starter.
  strokeWeight( 12 );
  stroke( random(100,200), random(70,100), random(30) );
  line(0,0,0,-50);
  translate(0,-50);
  
  branch(10);
  noLoop();
}



void branch( int level ) {
  if ( level == 0 ) { 
    return;
  }
  strokeWeight( level );
  stroke( random(100,200), random(70,100), random(30) );
  if ( level == 1 ) { 
    stroke( 0, random(100,255), 0);
  }
  
  pushMatrix();
  pushStyle();
  rotate(theta);
  line(0, 0, 0, -5*level);  
  translate(0, -5*level);
  branch(level-1);
  popStyle();
  popMatrix();     

  pushMatrix();
  rotate(-theta);
  line(0, 0, 0, -5*level);  
  translate(0, -5*level);
  branch(level-1);
  popMatrix();

}

in my opinion the right images could be 3D, because some branches are in the background

And there is more random, for example

  • the angles, the length of branches, the depth
2 Likes

Oh, I see. Thank you a lot, I didn`t know what to do without your help. Your answer saved my life!