(Re)curse of the IFS

2010 September 25

Fractals are pretty. They are also conceptually fairly simple. One of the ways to build fractal images is a Iterated Function System. That wikipedia article has a pretty good writeup, but it's kinda mathy. So here's what it is in almost plain English.

An Iterated Function System is exactly what it sounds like: Applying a function over and over. In the case of fractals, that function takes a set of pixels (represented in our case by a BitmapData), and outputs a set of pixels (again, BitmapData). What it actually does to those pixels isn't all that important as long as it adheres to a couple of simple rules. The first rule is that the transformation should be generally contractive. That is, pixels that are widely spaced before the transformation should end up closer together. This is not strictly necessary for the mathematical definition, but if it is not true then the resulting pictures get sparser and sparser and that's just not visually interesting. The other rule that we use is that the transformation is affine, which makes it representable by a Matrix. This rule too is not strictly necessary, but it does make our implementation a lot easier.

So, what exactly do we need to generate a fractal?

  1. A starting bitmap. I use noise in this project
  2. One or more matrices which represent affine transformations. I just use between 3 and 7 randomly generated matrices which have x and y scale factors < 1.
  3. framework code to draw and apply our system.

That really is the heart of it. I've added a few tweaks for visual interest, such as a fade and expand which gives an interesting smoky texture to the animation. Another thing that can be done is to use a coloring system to map pixels to colors in a palette, but I have not done that.

So, psuedocode:

     generate random bitmap
     generate N affine contractive matrices
     for (some number of iterations){
       for (i = 0 .. N){
         bitmap = apply(matrix[i], bitmap);
       }
       apply mild fade effect //visual effect only
       render bitmap
     }
   

And here's the final result.

Code here

privacy policy
Sponsored links