getting the Shiffman tree to grow
in
Programming Questions
•
2 years ago
So I've been messing around with the wonderful tree in the example by Daniel Shiffman. I mildly understand the code but cannot figure out how to make it grow trunk by trunk instead of just appearing instantly. Does the whole code need to be revamped or is there a simple trick?
so far:
- /**
- * Recursive Tree
- * by Daniel Shiffman.
- *
- * Renders a simple tree-like structure via recursion.
- * The branching angle is calculated as a function of
- * the horizontal mouse location. Move the mouse left
- * and right to change the angle.
- */
- float theta;
- void setup() {
- size(1000, 800);
- smooth();
- //noLoop();
- }
- void draw() {
- frameRate(1);
- //stroke(155,207,137);
- stroke(162,104,76);
- fill(155,207,137);
- background(240,240,240);
- strokeWeight(20);
- line(500,700,500,400);
- translate(500,400);
- branch(200,20);
- }
- void branch(float h, float s) {
- // Each branch will be 2/3rds the size of the previous one
- h *= 0.55+random(.25);
- s *= 0.66;
- // All recursive functions must have an exit condition!!!!
- if (h > 10) {
- strokeWeight(s);
- pushMatrix(); // Save the current state of transformation (i.e. where are we now)
- rotate(0.6+random(0.35));
- strokeWeight(s);
- stroke(162,104,76);
- line(0, 0, 0, -h); // Draw the branch
- translate(0, -h); // Move to the end of the branch
- branch(h,s); // Ok, now call myself to draw two new branches!!
- popMatrix(); // 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!
- pushMatrix();
- rotate(-0.6-random(0.35));
- strokeWeight(s);
- stroke(162,104,76);
- line(0, 0, 0, -h);
- translate(0, -h);
- branch(h,s);;
- popMatrix();
- }
- }
1