How to add curvature on a recursive tree

Hi there. I’m doing a sketch of a recursive tree in processing and i want to add curvature on the branches but i don’t know how…

There is the code below.


float minBranchLen = 0.5;
float angle= radians (30);
float branchratio = 0.9;

void setup() {
  size(1300, 1000);
  background(0);
  stroke(200,255,0,30);
}

void draw() {

  pushMatrix();
  translate(width/2, height);
  desenharvore(300);
  popMatrix();
}

void desenharvore(float l) {
  if (l>minBranchLen) {

    line(0, 0, 0, -l);
    pushMatrix();

    translate(0, -l);
    rotate(-angle);
    desenharvore(random(l * branchratio));
    rotate(2*angle);
    desenharvore(random(l * branchratio));
    
    popMatrix();
  }
}

Thanks, :slight_smile:

i try adding to the angle, that did not show the double V shape, but still not looked natural.

but look my version

//  https://processing.org/examples/tree.html
//  Recursive Tree by Daniel Shiffman. 
// kll add:
// make dynamic width, and color brown to green
// add mouseY move sun and color sun and background

float first = 80, strk = 20, ang =45, lvl, shrink = 0.766;       // kll TEST stroke width to make tree more natural
float theta;   


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

void draw() {
  float s = mouseY/(float) height;
  background(100*s, 100*s, 100+100*s);
  noStroke();
  fill(200,200*s,0);                        // yellow sun
  ellipse(width-60,200-100*s,60,60);
  fill(0, 100, 0);                        // dark green grass
  rect(0, height*0.85, width, height*(1-0.85));
  lvl = 1;                               // kll init branch level
  float a = (mouseX / (float) width) * ang;                 // Let's pick an angle [ang] degrees based on the mouse position
  theta = radians(a);                                       // Convert it to radians
  translate(width/2, height);                               // Start the tree from the bottom of the screen
  stroke(80, 40*lvl, 80);                // kll brown to green 
  strokeWeight(strk);                    // kll
  line(0, -20, 0, -first);               // kll shorter, grow on the green                     // Draw a line 120 pixels
  translate(0, -first);                                       // Move to the end of that line
  branch(first, strk,lvl);                                    // Start the recursive branching!
}

void branch(float h, float strk,float lvl) {        // kll added stroke width as parameter
  h    *= shrink;                                           // Each branch will be [shrink] the size of the previous one
  strk *= shrink;                         // kll
  stroke(80, 80*lvl, 80);                 // kll brown to green 
  strokeWeight(strk);                     // kll
  lvl *= 0.85/shrink;
  //                                                        All recursive functions must have an exit condition!!!!
  if (h > 2) {                                              // Here, ours is when the length of the branch is 2 pixels or less
    push();                                           // 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, strk,lvl);                      // kll             // Ok, now call myself to draw two new branches!!
    pop();                                            // 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!
    push();
    rotate(-theta);
    line(0, 0, 0, -h);
    translate(0, -h);
    branch(h, strk,lvl);                      // kll
    pop();
  }
}
1 Like