There are some seriously gorgeous trees in Vancouver right now! Spring has finally come! After playing with fractals and making a Sierpinski triangle, I wanted to try creating fractals with a recursive function.
One of the first big problems I ran into was that my function only produced a branch to the left. So I had a fern-looking string of Vs veering to the left of the page. I couldn’t understand why the function was not drawing the second new point to the right! Turns out it was because my countdown to limit the number of iterations (decreasing the branch length) was a global variable. What happened was that the branches were drawn to the left and then when the drawbranch() function was called for the points on the right, the variable for length was now zero! This was fixed by limiting the tree size through a parameter for generations of branches to the function instead.
Yikes! What was going on here? I have a fear of repeating objects (especially holes) so I found this quite frightening. I took a screenshot anyways because it looked so bizarre! The answer was that since canvas positioning starts from 0 at the top, I had to take the new y value and subtract it from the height of the canvas element. I had done this for the tree branches (hence why they are in the upright position) but had forgotten to do the same for the flowers.
Tada! Less creepy, more pretty. Clicking on the page generates a different tree every time.
Day 46 – Cherry Blossoms