# How to add curvature on a recursive tree

**URL:** <https://discourse.processing.org/t/how-to-add-curvature-on-a-recursive-tree/16257>\
**Category:** Coding Questions\
**Created:** [December 9, 2019, 12:40am UTC](https://discourse.processing.org/t/how-to-add-curvature-on-a-recursive-tree/16257 "2019-12-09T00:40:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcoo](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@marcoo](https://discourse.processing.org/u/marcoo)\
**Post date:** [December 9, 2019, 12:40am UTC](https://discourse.processing.org/t/how-to-add-curvature-on-a-recursive-tree/16257/1 "2019-12-09T00:40:08Z")

</div>

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.

```auto

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, 🙂

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [December 9, 2019, 3:47am UTC](https://discourse.processing.org/t/how-to-add-curvature-on-a-recursive-tree/16257/2 "2019-12-09T03:47:39Z")

</div>

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

but look my version

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

```
