Day 46 – Cherry Blossoms

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.

After successfully creating tree branches using a recursive function, I wanted to add flowers to the end of the branches. Here’s what I ended up with:
Screen Shot 2015-02-26 at 10.39.52 PM

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.
Screen Shot 2015-02-26 at 10.29.51 PMTada! Less creepy, more pretty. Clicking on the page generates a different tree every time.

Day 46 – Cherry Blossoms

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s