Fractals 101: The Mandelbrot Set

This is the second part of Fractals 101, a series of tutorial dedicated to fractals. This post will investigate two popular fractals: the Mandelbrot set and its 3D cousin, the Mandelbulb.

mandelbulb

Introduction

The previous post in this series, Fractals 101, showed how fractals can be constructed by iteration. Fractals created this way have indeed an infinite complexity, but they are also very boring. Their strong self-similarity doesn’t really allow for any interesting complexity to arise. Luckily, this is not always the case. Most fractals appear in the most unexpected places, rewarding you with endless beauty.

The Mandelbrot Set

One of the most famous fractals of this kind is the Mandelbrot set. Firstly defined in the 1978 , it was later computed and visualised by the mathematician Benoit Mandelbrot in 1980. The Mandelbrot set arises from an extremely simple equation:

    \[f_c\left(x\right)=x^2+c\]

In order for this fractal to appear, both x and c must be complex numbers. This blog has dedicated an entire post on Complex Numbers, in the context of 2D rotations.

The complexity hidden in f_c comes to light when you keep repeating iterating the function, over and over. Which means, when you keep feeding the result of f_c to f_c itself. Starting with z=0, we obtain the following sequence:

    \[z_0 =0\]

    \[z_1 = f_c\left(z_0\right)\]

    \[z_2 = f_c\left(z_1\right)\]

    \[z_3 = f_c\left(z_2\right)\]

    \[...\]

…and so on.

For any c, this sequence has two possible behaviours: it either diverse towards infinity, or it stays forever bounded. The Mandelbrot is made of all those complex numbers c for which iterating f_c\left(0\right) does not escape to infinity. When computing the Mandelbrot set, f_c is iterated several times; if it stays small enough, it is assumed to be part of the set.

At a first glance, this produces a black and white figure:

322px-Mandelset_hires

White points are the values of c for which f_c\left(0\right) diverges to infinity; while black hones are the values of c for which f_c\left(0\right) stays confined.

The really interesting behaviour happens on the edge of the Mandelbrot set, where the white and black parts of the plane meet. Not only the edge exhibits self-similarity; it also hides an endless sea of complexity.

Mandelbrot_zoom

Colouring the Fractal

Mandelbrot_sequence_new

The original equation that defines the Mandelbrot set only allows for a black and white figure. Many coloured version of the Mandelbrot set exist, using several different technique. The most common relies on the speed at which c changes. This produces stunning animations that truly highlights the complexity that hides in the fringe of the Mandelbrot set:

If you’re familiar with Shaders, the following code will allow you to calculate a Mandelbrot fractal. It requires c as an input, and it outputs and a number from 0 to 1 that indicates how many iterations were performed before the function diverged:

float mandelbrot (float2 c)
{
    float2 z = 0;
    float2 zNext;
    int i;
    for (i = 0; i < _MaxIterations; i ++)
    {
        // f(z) = z^2 + c
        zNext.x = z.x * z.x - z.y * z.y   + c.x;
        zNext.y = 2 * z.x * z.y           + c.y;
        z = zNext;

        // Bounded?
        if ( dist(z,float2(0,0)) > 2)
            break;
    }

    return i / float(_MaxIterations);
}

The mandelbrot function can be fed with the UV of a piece of geometry. Its return value can be used to sample a ramp texture. This allows a fine control over the final colour of the produced fractal.

It’s important to notice that c and z are complex numbers; they are not simple 2D vectors. As such, the operation squaring z results in the following expression:

    \[z = x+\i y\]

    \[z^2 = z\cdot z = \left(x+\i y\right)\left(x+\i y\right)=\]

    \[=x^2 + \i^2 y^2 + 2\i xy\]

And, since \i^2=-1, we obtain:

    \[z^2 = x^2 - y^2 + 2\i xy\]

The Mandelbulb

The concept behind the Mandelbrot set has been subject of extensive research. While a 3D equivalent of the Mandelbrot set does not exist,  Daniel White and Paul Nylander came up with a 3D shape that exhibits similar properties. This shape has been called the Mandelbulb, since its similarity with a round bulb.

The logic behind its creation is similar to the one that generated the Mandelbrot set. Starting from zero, we iterate a 3D function g_c\left(v\right). Both v and c are 3-dimensional vector, and v=\left[x,y,z\right].

    \[g_c\left(v\right) =\left[ r^n\sin \left( n\theta \right)\cos \left(n\phi\right) , r^n\sin \left(n\theta \right)\sin \left(n\phi\right), r^n\cos \left(n\theta\right)\right]\]

With:

    \[r=\sqrt{x^2+y^2+z^2}\]

    \[\phi=arctan\left(\frac{y}{x}\right)\]

    \[\theta=arccos\left(\frac{z}{r}\right)\]

The parameters n is known as the power factor of the Mandelbulb, and it’s used to control its shape. The introduction of trigonometric functions are used to express the Mandelbulb in spherical coordinates. Exactly like we did before, the 3D points c that belongs to the Mandelbub are the ones for which the sequence:

    \[v_0 =\left[0,0,0\right]\]

    \[v_1 = g_c\left(v_0\right)\]

    \[v_2 = g_c\left(v_1\right)\]

    \[v_3 = g_c\left(v_2\right)\]

    \[...\]

stays bounded.

1024px-Power_8_mandelbulb_fractal_overview

Visualising the Mandelbulb

To give a better understanding of what the Mandelbulb looks like, I have created a 360 video that shows the birth of this fractal, slowly changing its power factor n from 1.4 to 6.

The Mandelbulb has been rendered inside out, using a volumetric shader in Unity. The 360 effect has been achieved using 3 cameras with a large field of view. All images are wrapped to an equirectangular projection using another shader. Despite being rendered at a very high resolution, the compression of YouTube makes some parts of video slightly blurred.

📰 Ad Break

Conclusion

Other resources

  • Part 1. Fractals 101
  • Part 2. Fractals 101: The Mandelbrot Set

Comments

5 responses to “Fractals 101: The Mandelbrot Set”

  1. […] For this toy example, we will use a simple Mandelbrot fractal. The Mandelbrot Set is one of the most popular and recognisable fractals. Where it originates from and how it is calculated has been covered extensively in a previous article titled Fractals 101: The Mandelbrot Set. […]

  2. I just came across your site, what an incredible work, the top notch quality !
    Are you a former demoscener ?

    I already plan to spend a lot of time reading your posts, so for sure I will start donating the next month.

    Anyway, thanks a lot and keep up the good work 🙂

    PS: “In order for this fractal to appear, both x and c must be complex numbers. This blog has dedicated an entire post on Complex Numbers, in the context of 2D rotations.”
    => The link to the post is broken.

    1. Hey!
      Thank you very much for your kind words. It’s because of comments like this one that I keep writing tutorials.

      Yes, I could say I’m a demoscener. But I have never really released the stuff I’ve been working on, unfortunately! 😀

  3. […] bom material para começar a estudar fractais: Fractals and Mathematical Art , Fractals-101 e Fractals-101-Mandelbrot. Veja também o […]

  4. […] Part 2. Fractals 101: The Mandelbrot Set […]

Leave a Reply

Your email address will not be published. Required fields are marked *