Part I
“The essence of mathematics is not to make simple things complicated, but to make complicated things simple.”
(S. Gudder)


FRACTALGENERATOR: A SIMPLE FRACTALS VIEWER

Introduction

Back when I was in High School I was reading a book about the origins of the Chaos Theory when I saw in a footnote the recipe to generate the well known Mandelbrot Fractal. It was surprisingly simple so I tried to implement it using C++. At the time I was not aware that there was already a Complex Number class available with the standard library, so I started by learning something about complex arithmetic and implemented my own class. After that, generating that beatiful figure was close to trivial.

This is why I decided to write this tutorial: a very simple algorithm that draws so wonderful pictures is a perfect candidate for a tutorial. I choosed C# instead than C++ to make it even simpler.

In order to follow the tutorial a bit of knowledge of .Net and the Visual Studio IDE is required: I will focus on the image generation code and thus I will not describe in much detail how to set up the project or how to add an event handler to a button. Should you have any trouble you can look directly at the source code.

Download the Visual Studio Solution

CONTENT

Part I: A Bit Of Theory: Complex Numbers And Fractals

Part II: Setting Up the Project

Part III: The FractalView Window

Part IV: The IGenerator Interface And The Generator Implementations

Part V: Conclusions


Part I: A Bit Of Theory: Complex Numbers And Fractals

There is no need to face the complexity of the Fractals mathematics for this tutorial, but at least a couple of techincal details have to be handled. First of all, as we are going to use Complex Numbers, let's start with them.

Complex Numbers

We all have been told that negative numbers does not have a square root, right? Wrong! Negative numbers does not have a real square root:

x^2 = -1

This cannot be solved if we mean to use real numbers only. But we can define a new unit:

i^2 = -1

This means that, for example:

sqrt(-9) = 3i

The letter i stands for 'imaginary', in opposition to real numbers. Complex numbers have the following structure:

a + ib

Where a is the real component and b is the imaginary component.
Many arithmetic operators are defined for Complex Numbers, and some of them are quite straighforward. As an example, here is the addition:

(a + ib) + (c + id) = (a + c) + i(b + d)

The .Net API does not come with a Complex type, so we are going to use a simple implementation found at http://www.codeproject.com/KB/cs/CompLib.aspx. I had to add a few operations and I have overridden the ToString() method.

The set of all the Complex Numbers describes the Complex Plane, where the real component is the x coordinate, and the imaginary component is the y coordinate. The Fractals described in this tutorial are drawn in the Complex Plane.

For more informations, the http://en.wikipedia.org/wiki/Complex_number page is a good starting point.

Fractals

Fractals are mathematical shapes wich are very detailed no matter wich scale you look them at, and at every scale you can recognize sub shapes that roughly resemble the whole object (self-similarity). In the real world you can find many examples of Fractals: shores, trees, mountains, clouds are all examples of the self-similarity property. If you ever used graphic tools, you might have noticed that some of the algorithms used to simulate natural effects (lightning, storms) use Fractals. Lately Fractals have been appreciated for their aesthetic value, becoming one of the most interesting forms of CG art.

The surprising thing about Fractals is that often the mathematical functions behind them are notably simple. One of the most famous images in the world is the well known Mandelbrot Set (see http://en.wikipedia.org/wiki/Mandelbrot_set for more informations) and its definition, althought not really complex, is better left to other sites. For our pourposes it is enough to say that given a point in the Complex Plane, you apply the following function iteratively:

z = z^2 + c

where c and z are both initially equal to the starting point. At each iteration, the new z value is computed by powering the previous value by 2 and by adding c.

A point belongs to the Mandelbrot Set if z is bounded, that is, z never exceeds a given value, no matter how many iterations you apply. Of course you cannot iterate infinitely, so a fixed limit must be choosen. In practice, usually you iterate a few hundred times (100 – 300). A point is considered part of the Mandelbrot Set if the module of the underlying Complex Number never exceeds 2 while iterating.

The wonderful thing is that the border line between the point inside the set and those outside it shows an infinite amount of details, even if magnified thousands of times. Actually, only the computer precision, the image resolution and the maximum iterations allowed limit the output complexity.

I will cover the algorithm in greater detail later.