Fractal : a pattern that is repeated at a different scales ultimately forming very complex patterns.
Fractal: images of dynamic systems driven by recursion.
To make the tree on the left, I first drew the tree I wanted to generate on a piece of paper. Then, I came up with the following algorithm:
Draw the trunk
Draw the first V at a given thickness and length
For each point at the top of the V draw another V
With each iteration change the angle, length, and thickness of the V until the smallest V is reached.
Done!
Except that is iteration and not recursion!
Try again
Draw the trunk
Draw the first V at a given thickness and length
But first, draw a V that is smaller, thinner, & angled a little left, on the left side of the first V
But first, draw a V that is smaller, thinner, & angled a little right, on the right side of the first V
Keep increasing the angle, decreasing the thickness and length until I reach the base case where the length is at a certain min (any of: angle, thickness or length could serve as a base case).
Done! *If you want a more realistic tree, you can introduce some randomness into the three mentioned variables.
Now you will need to open up your programming toolbox and turn that algorithm into code. * Probably not as easy as
stating the algorithm. Here is what I came up with using javascript and the HTML5 canvas element (some helper functinos included).
I encourage you to try some variations on your own.
Notice Four key sections of code that will be present, in some form, when creating a fractal.
Global variables or Object variables
An initialization function
An update-position function(s)
A draw function(s)
Fractal Generator
The fractal generator below uses a similar set of functions as the tree above to create some common fractals. To use it, select a Fractal and
Number of recursive calls from the lists, and click Draw Fractal
All of these fractals have a commonality. Therefore, we can parameterize functions to reflect that.
Global variables (or Object variables)
Initialize the starting point
Move, Turn, and Resize functions
Positioning functions for each fractal
Draw functions
How to go about writing a similar program
Try changing the number of recursive calls to visualize the repeating patterns.
Write an algorithm for two of the fractals. Compare them, and notice that you can use the same functions to implement similar tasks.
Use iterative programming techniques i.e. agile methods, rapid prototyping. Get one working, then two...
Try different shapes than those listed here.
If you really want to get sophisticated, try color gradients repeating a pattern on a spiral curve :)
Turning a recursive algorithm into code is not the trivial part of understanding recursion, it is the practical part. Like everything else it takes repetition to become good at it.