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();
}
}
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.
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.