Fractals in C#: Fern Fractals
Fern fractals are another class of iterative fractals, a type of iterative function system, that draws rather good images of ferns. And with a bit of tweaking of the input variables, the ferns of various sizes and shapes can be created using the same code.
To create a fractal fern you first need to supply a rather large amount of input variables that will control the shape of the fern. So in the application, the variables are stored in a few arrays.
So, now we have arrays dA, dB, dC, dD and iRand. Very simply, the values in dA control the rachis of the fern (the main stem for those, like me, who weren’t listening in biology class), dB controls the left pinna (a’leaflet’), dC controls the right pinna, and dD controls the main shape of a frond (the fern’s ‘leaf’). iRand contains the probability that a value from each of the four arrays will be used.
Now the main function iterates through a specified number of points (which in the sample on this page, I used 100000 iterations), by first setting the current x and y coordinate to 0, and then it starts the iteration.
A random number is generated between 0 and 1, and if the number is less than iRand[0] then it calculates the new xy coordinate based on the dA array. It then checks each probability through all the probabililities, each time calculating the xy coordinate using the array and calculation specified at that value.
Once the new coordinates are calculated, the point is drawn using the desired colour, and then the function loops to the next iteration, using these coordinates as the base coordinates for the next iteration.
Once the function has iterated enough times, the function exits, and you should be left with a decent fern.
Half the fun of this type of fractal is playing around with the input variables to see how they affect the output, so have fun.
The full source code is available at https://github.com/sjmeunier/fractalize.
Originally posted on my old blog, Smoky Cogs, on 30 Sep 2009