# Recursive tree painting problem

**URL:** <https://discourse.processing.org/t/recursive-tree-painting-problem/29303>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [April 12, 2021, 12:51pm UTC](https://discourse.processing.org/t/recursive-tree-painting-problem/29303 "2021-04-12T12:51:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![xXvladosXx](https://avatars.discourse-cdn.com/v4/letter/x/2bfe46/32.png) [@xXvladosXx](https://discourse.processing.org/u/xXvladosXx)\
**Post date:** [April 12, 2021, 12:51pm UTC](https://discourse.processing.org/t/recursive-tree-painting-problem/29303/1 "2021-04-12T12:51:43Z")

</div>

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:**

```auto
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:**

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/3/33fa34ba953f33fd94b6901a22d591bd8ebd322d.jpeg)`Preformatted text`

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [April 12, 2021, 1:48pm UTC](https://discourse.processing.org/t/recursive-tree-painting-problem/29303/2 "2021-04-12T13:48:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![xXvladosXx](https://avatars.discourse-cdn.com/v4/letter/x/2bfe46/32.png) [@xXvladosXx](https://discourse.processing.org/u/xXvladosXx)\
**Post date:** [April 12, 2021, 2:32pm UTC](https://discourse.processing.org/t/recursive-tree-painting-problem/29303/3 "2021-04-12T14:32:32Z")

</div>

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

**Problem:**

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/9/930cbeedd3d69db24e7b0aafb93d57b4afc10aa5.png)

**new code:**

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

```

---

<div class="post-metadata">

**Author:** ![xXvladosXx](https://avatars.discourse-cdn.com/v4/letter/x/2bfe46/32.png) [@xXvladosXx](https://discourse.processing.org/u/xXvladosXx)\
**Post date:** [April 12, 2021, 2:33pm UTC](https://discourse.processing.org/t/recursive-tree-painting-problem/29303/4 "2021-04-12T14:33:22Z")

</div>

Maybe I`ve done something wrong

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [April 12, 2021, 2:40pm UTC](https://discourse.processing.org/t/recursive-tree-painting-problem/29303/5 "2021-04-12T14:40:15Z")

</div>

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![xXvladosXx](https://avatars.discourse-cdn.com/v4/letter/x/2bfe46/32.png) [@xXvladosXx](https://discourse.processing.org/u/xXvladosXx)\
**Post date:** [April 12, 2021, 6:04pm UTC](https://discourse.processing.org/t/recursive-tree-painting-problem/29303/6 "2021-04-12T18:04:27Z")

</div>

Okay, I`ve got this one  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/cb0ef5a6752d30aa2f30e4ce8c649b34defa9aac.png)

```auto
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();

}

```

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [April 12, 2021, 7:12pm UTC](https://discourse.processing.org/t/recursive-tree-painting-problem/29303/7 "2021-04-12T19:12:57Z")

</div>

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

```auto
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();

}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 12, 2021, 7:17pm UTC](https://discourse.processing.org/t/recursive-tree-painting-problem/29303/8 "2021-04-12T19:17:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![xXvladosXx](https://avatars.discourse-cdn.com/v4/letter/x/2bfe46/32.png) [@xXvladosXx](https://discourse.processing.org/u/xXvladosXx)\
**Post date:** [April 13, 2021, 1:27pm UTC](https://discourse.processing.org/t/recursive-tree-painting-problem/29303/9 "2021-04-13T13:27:56Z")

</div>

> [@TfGuy44](#):
>
> ```auto
> 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();
> 
> }
> 
> ```

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