Fractals are like alien worlds, strange yet familiar. You can zoom into a fractal endlessly, producing fascinating variations of colour, texture and shape.

Most mathematical formulae when plotted produce wave-like lines, curves, arcs and other simple shapes. Fractals by contrast produce extremely complex lacy colourful patterns. You never really see the same thing twice. They are often natural looking, imitating patterns produced in nature such as ferns or coastlines. Despite this, they are extremely simple to program, as we'll see here.

What is a fractal?

A fractal can be described as "the adventures of a small number in a complex plane." A fractal is a peculiar and very dense graph generated by a mathematical process. Although the graph are infinitely complex, the underlying algorithms are short and relatively simple.

When you want to see the relationship between numbers, to see mathematical expressions, you can put them into a chart or plot. A simple plot is shown below, distance against elevation.

A simple elevation plot showing distance in the x-axis and elevation in the y-axis

By going along the line you can see the altitude of various points change with distance.

Not all numbers used my mathematicians are those we are familiar with. For example to translate waveforms into audio, or to analyse radio signals it is necessary to use "imaginary" numbers.

Complex and Imaginary Numbers

In the 16th century, mathematicians began trying to describe the square root of minus one. When you multiply a number by itself, you square it; 4 * 4 is 16. You can describe this relationship in the opposite way, 4 is the square root of 16. It seems obvious that there is a square root for any number. Squares and roots have been used for thousands of years, but attention was focused on -1 which although paradoxical, eventually proved quite useful.

What is the square root of -1? When you square a positive number, you always get a positive number. When you multiply a negative number by a negative number, you also get a positive number. So, the number you need to multiply by itself to get -1 cannot be positive, nor can it be negative. In other words it is not a real number, it is imaginary.

When you combine imaginary numbers with real nu,bers, you get points on a grid. Real numbers describe the horizontal axis, while imaginary numbers describe the vertical distance. This creates a complex number, and they are an important concept, used in almost every field of mathematics and applied science, from voice processing, image compression, radar analysis, even earthquake monitoring.

A fractal is built pixel by pixel via the repeated transformation of a complex number. Several things can happen when you transform a complex number by multiplying it by itself.

If it is a real number larger than 1, it will grow towards an infinitely large number.

If it is less than one (a fraction) it will shrink towards an infinitely small number close to zero, but never reaching zero.

If it is exactly one, it will remain one. 1 * 1 is 1 and always will be.

The Mandelbrot Set

All numbers that are negative are described as a "set" of negative numbers. The Mandelbrot set is a group of numbers which when plugged into the Mandelbrot equation remain bounded, that is they don't escape to infinity. The numbers which move towards zero, or those which oscillate, when painted in, create the classic Mandelbrot shape.

We want to display these fractals on the screen. We can think of the complex plane as a grid, with real numbers shown horizontally and imaginary numbers shown vertically. To calculate the Mandelbrot set, we'll first choose a zone within the grid to paint in. For each point in this area we assign the corrsponding complex number to c and starting with z = 0, we repeat the transformation against this value enough times to discover whether or not this particular position will escape to infinity. it if will, we colour it white to show it is outside of the Mandelbrot set. If it won't escape we colour it black to show that it is part of the set. The number of times we run this test is the number of iterations.

The result is an intricate shape known as the Mandelbrot set. The irregular line between the black and the white areas is the boundary between the two attractors (infinity and zero). As you can see, it is by no means as simple as a circle.

Classic Black and White Mandelbrot Set

Programming the Mandelbrot Set

Let's first start with the black and white Mandelbrot set. The only parameters to define are:

The area of the complex plane (xMin, xMax, yMin, yMax)

The resolution of the image (nx, ny)

The maximum iterations required to determine if the value escapes or is bounded (maxIter)

Thirty two iterations is a good starting point. Larger values produce more accurate results, however require more calculation time.

I know this is in VisualBasic, it's the language I was using at the time. Later examples use Delphi and Pascal.

The first function essentially goes through each pixel in the complex plain and runs the Mandelbrot calculation. If the value returned from this function does not escape to infinity it assigns that pixel the black colour, while if it does escape to infinity it is assigned the WHITE colour.

The actual calculation is done by the Mandelbrot function. The loop tests to see if the calculated value from the Mandelbrot equation escapes to infinity before the maximum iterations is reached. If it is, it returns the number of iterations, if it doesn't then it returns the max iterations. For this example, the value of 100000 is good enough to represent infinity.

Colouring Fractals

Black and White fractals are all well and good, but coloured fractals are often breathtaking. Where does the colour come from?

Different colours represent different escape velocities. That is, the calculation not only tests to see if a point escapes to infinity, it also tests how fast it escapes. These mysterious variations of escape velocity give fractals their beauty and complexity.

These code examples show code written in Borland Delphi. The fully working example can be downloaded at the end of the article.

We respect your privacy, and will not make your email public. Hashed email address may be checked against Gravatar service to retrieve avatars. This site uses Akismet to reduce spam. Learn how your comment data is processed.

Save my name, email, and website in this browser for the next time I comment.

About the Author

Tim Trott

Tim Trott is a creative photographer, traveller, astronomer and software engineer with a passion for self-growth and a desire for personal challenge.

Hi, I'm Tim Trott. I'm a creative photographer, traveller, astronomer and software engineer with a passion for self-growth and a desire for personal challenge.

This is my website, a place for me to share my experiences, knowledge and photography. I love to help people by writing articles and tutorials about my hobbies that I'm most passionate about. I hope you enjoy reading my articles as much as I enjoy writing them.