How Fractals are Generated, Part Two

Here is where the fun starts. We get into some confusing math, and therefore you should probably read this only if you have good math skills, and really want to understand fractals. As we said in part one, fractals are created through iteration, but we never clearly described any particular functions and how they operate. Here's where we do that.

The first thing you need to know is what complex numbers are, at least basically. Complex numbers are a way to put two coordinates, and x and a y, into one number. They have two parts. One is a real number, which is any normal number like 5 or 6.2 or pi or 7/3, and the other is an imaginary number, which is a number like the square root of negative one. Now when you take a number and square it, whether the number is negative or positive, you always get a positive number, right? So how can you get the square root of a negative number? Well, in our system of math, you can't, hence the name imaginary. The square root of negative one is represented by the constant i. While you can't write i in numerical form, by definition i squared (i^2) equals negative one (-1). So complex numbers are a real number plus an imaginary number. Examples of complex numbers are (6 + 3i), (.346 - 6.2i), and (0 + .264i). How are complex numbers used in fractals? Well, each point has two coordinates, right? And there are two numbers "within" a complex number, right? So you can plot a complex number by using the real portion as the x- coordinate, and the imaginary portion as the y-coordinate. So in that sense, (4 + 6i) is the same as (4,6).

One thing that should be cleared up is that the complex coordinates we use to generate the fractal are not the coordinates of the pixel they represent. Pixel coordinates are always greater than zero, and are always less than the bounds of the screen. They are usually something like (24, 531). The coordinate range we use for our computations depends on the fractal, but it is often something like: x range from -2 to 2, and the y range from -1.5 to 1.5. Therefore, the complex coordinates that we perform the math on are long, small decimals, not integers. We are dividing 3 or 4 units (the virtual coordinates) into 700 or 800 segments (the pixel coordinates), so we get tiny little decimals. This is why we need powerful computers to generate fractals quickly, because there is so much math on tiny little numbers that must be exact.

Now we learn how to perform operations on complex numbers. Adding and subtracting is easy, we just add or subtract real parts and imaginary parts separately:

(5 + 7i) + (2 - 3i)=

5+2 + 7i-3i=

7 + 4i

To multiply complex numbers, however, we use the distributive law. Here is a simple multiplication:

(9 + 6i) * (4 - 2i)=

9*4 + 6i*4 - 9*2i - 6i*2i=

36 + 24i - 18i - 12(i^2)=

36 + 6i + 12=

48 + 6i

Notice what we have done. First we separate the different multiplications using the distributive law. Then we do the multiplication: 9*4=36, 6i*4=24i, 9*2i=18i, and 6i*2i=12(i^2)(i squared ). Now we add like terms, and get: 36 + 6i - 12(i^2). What next? Remember that (i^2)=-1. So we have: 36 + 6i - 12(-1). Which can then be simplified to: 36 + 6i + 12, and then added to get the answer of: 48 + 6i.

Division of complex numbers is not used much in fractals, but here is the formula anyway. We will not bother going through all the steps:

(x1+y1i)/(x2+y2i) =[(x1x2 + y1y2) + i(x2y1 - x1y2)]/(x2^2 + y2^2)

If that makes sense to you, good. If not, don't worry.

So, now we know a little about complex numbers. The next thing to do is explain a function used to generate a fractal. As is mentioned in part one, there are an infinite number of these functions, but we will use a function for a Julia set as an example. The function for certain Julia sets is: f(z)=z^2+c. That's it. The new complex coordinate is set to the old one squared plus "c". What is c? It is simply a complex number, and it can have any value you like. Different c values produce different Julia sets. Let's use (1 + 1i) as c. So, if we were to start with the point (2 + 1i), the first iteration would be:

f(z)=f(2+1i)=

(2 + 1i)^2 + (1 + 1i)=

2*2 + 2*1i + 2*1i + 1i*1i + 1 + 1i=

4 + 2i + 2i + 1(-1) + 1 + 1i=

5 + 5i -1=

4 + 5i

So the first iteration brings us to (4 + 5i). We can do it again now.

f(z)=f(4 + 5i)=

(4 + 5i)^2 + (1 + 1i)=

4*4 + 4*5i + 4*5i + 5i*5i + 1 + 1i=

16 + 20i + 20i + 25(-1) + 1 + 1i=

17 + 41i - 25=

-8 + 41i

So our second iteration gives us (-8 + 41i). We continue to do this as described in part one, and each time we test to see if it has "left the screen". Actually, we are testing to see if the point ever leaves the circle centered at the origin with radius 2. We can prove that if it leaves this circle, it is bound to go to infinity, so we simply stop iterating once it leaves the circle. The number of times we must iterate the function before it leaves the circle is used to choose the color for the original point.

We must also set a limit of iteration on our fractal. Since the points inside the Mandelbrot set never leave the screen, we will iterate our function forever if we wait for them to leave our circle. To get around this, we set a limit on the number of times we will iterate it. If the point is still in our circle after that many iterations, we assume it is part of the set. The more iterations we use, the more exact and detailed our image will be, but the longer it will take to generate. When we have done this with every pixel, we have a fractal. Other equations than this one produce different fractals. Mandelbrot sets are produced the same way as Julia sets, except that c is different for every point. When generating a Mandelbrot set, c is equal to the point we are determining the color for. We start with 0, the origin. Then we square it and add c. We square this new value and again add c. When this finally leaves the circle, or when we have reached our iteration limit, we color the point at the complex coordinate c. Then we move to the next point. C is changed to that new point, and once again we start with the origin and iterate. Of course there are many other fractals that can be created with simple equations. If you download Fractint, you can create your own formulas and run them to see what fractals they will produce. Some of the fractals in our Fractal Gallery were created using formulas that our team invented.
If you would like to read the basics behind the math described here, read Part One