Sylvain LC's Blog

A GitHub blog, usefull to me, hope the same for you.

I know because I already coded it on my ATARI 520 ST, it’s a very easy fractal to compute.

The definition is trivial, if you just have an idea of what complex numbers are (it’s not so complex !)

You just need this little complex number multiplication function to compute Z*Z.

You can cut and paste the code below in Shadertoy as an example

#define MAX_STEP 256
#define cmpxmul(a, b) vec2(a.x*b.x-a.y*b.y, a.x*b.y+a.y*b.x)
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    // Normalized pixel coordinates (from 0 to 1)
    vec2 uv = (2.0*fragCoord-iResolution.xy)/iResolution.y;
    uv *= 2.;
    // Time varying pixel color
    vec3 col = vec3(0);
    
    vec2 m = (2.0*iMouse.xy-iResolution.xy)/iResolution.y;
    if ( iMouse.x > 0.0 ) {
        uv *= 1.0 + m.y;
        uv.x -= m.x;
    }
    
    vec2 z = vec2(0);
    
    int i;
    for ( i=0 ; i<MAX_STEP ; i++ ) {
        z = cmpxmul(z,z) + uv;
        if ( dot(z,z) > 100. ) break;
    }
    
    // colors
    if ( i < MAX_STEP ) {
        col = vec3(float(i+1)/float(MAX_STEP));
    }

    // Output to screen
    fragColor = vec4(col,1.0);
}

Iq made a short version of it here :

If you want to know about this fractal, you are in the good place on Shadertoy. I’am impressed by the articles IQ wrote on this topic.

Please note that there is an infinite zoom possible on the Mandelbrot’s set, the only limits are the precision of the float and the number of allowed iterations to compute the details of the set.

References