Fractals can be easily transformed in artistic canvas. In this tutorial I show you how to achieve continuous colouration for the well-known fractals.
A bit of theory A fractal is an object that displays self-similarity on all scales. Mathematically speaking, a fractal is a set generated by a recursive formula in which every new element is calculated starting from the last computed element.
Well known examples of fractals are the Mandelbrot’s and Julia’s sets, the Newton fractal (in the picture above) and the so-called Burning Ship fractal.
Computers are great in repeating the same thing over and over again, so coding a fractal generator program is the best way to explore fractals and their artistic potential. Here are the formulas to generate some notorious fractals:
Mandelbrot’s and Julia’s Sets:
Burning Ship Fractal:
These formulas teach how to calculate the nth element of the progression, however they don’t explain how to draw a fractal. To do that we need an algorithm.
The Escape Time Algorithm The escape time algorithm is the easiest method to create fractal images. The mechanism is simple: for each pixel of our plot, we iterate the fractal formula and count the loops it takes to the computed value to escape a given threshold. Then we pick a colour according to the number of iterations.
The threshold is composed by an arbitrary number of iterations (the greater, the sharper the image will be) and a fixed constant value equal to 4. You may legitimately ask why exactly 4 and not whatsoever. Obviously, there is a mathematical demonstration for that!
The implementation is straightforward. Here I propose a simple one in C language (in order to keep this snippet accessible to everybody).
Mandelbrot and Julia sets implementation is basically the same. The only change concerns the initial C constant value. Indeed Julia’s set is a specific case, namely a sub-set, of Mandelbrot’s one. For the Burning Ship fractal we just have to introduce a new concept: the magnitude of a complex number. Its symbol is |Zn| and its value is:
So we have:
Newton fractal is a bit more complicated instead, because we have to deal with powers and derivatives of complex numbers. C language, like C++, Java and others, offers a library to deal with complex numbers. It can be useful for keeping your code cleaner and more expressive. However in this snippet I chose to do without in order to keep the post consistent.
The real challenge of drawing fractals is to draw beautiful fractals. While sharpening our fractals is quite easy (we just have to increase the iteration threshold), improving their colour smoothness is trickier. There are different techniques to reach a gorgeous colouring effect. The easiest one is to map the escaping values onto a logarithm scale in order to obtain a continuous gradient of iterations.
In simple words: the time escape algorithm returns an integer that tells us how many iterations a pixel needed to escape the loop. An integer is a discrete value. That means that if we want to pass from a value to its next we have to jump. For instance, when we pass from 100 to 101, we skip all the values in between. We ignore 100.1, 100.2 and 100.3 but also 100.33334, 100.33335, 100.33336 and so on.
It is self-evident that if we use a discrete colour scale to compute pixel colours, then our fractal image will have a discrete colouring, that means a lot of choppy and plain colour bands.
To avoid this ugly effect, we can use natural logarithms to transform the magnitude of every escaped pixel into a value between 0 and 1. By adding this value to the iterations count of the considered pixel we obtain at last a continuous scale of iterations values.
Knowing that a pixel escaped the loop after 105.34 iterations, while another one escaped after 105.38 allows us to pick the colour up in a more precise way.
Endless colour palette repetition
Now that we have a continuous colour index, we just have to do an extra step to find a good colour chart. For our purpose we consider a colour chart to be good when its colours blend smoothly and endlessly (this because fractals can be zoomed indefinitely).
So… Which is the mathematical function that repeats itself continuously and endlessly and that can help us building a perfect fractal colour palette? Sinus and cosinus, of course!
In this wonderful post of Jim Bumgardner you can find all steps to derive the formulas for real time colour-blending. I really suggest you to read in depth the whole post because it is plenty of handy tricks.
where frequency and phase are arbitrary values, center is the middle of the colour channel range and delta is the maximum variation gap. continuous_index is the value we calculated before. Considering that a whole channel is 255 we obtain:
This is just an example that you can tweak as you prefer. Again, read Jim’s blog carefully! Here are the mixings I used for the pictures above: