Search code examples
javaprocessingcomplex-numbersfractalsmandelbrot

When trying to create an inverted Mandelbrot-set it's distorted


When I make a normal Mandelbrot set, it works fine. But when I try to invert it into a teardrop-like thing (see here for more context: https://www.youtube.com/watch?v=mLJJUElQMRY) it is completely distorted and looks nothing like a teardrop.

I've tried looking into it but it seems like I'm doing everything correct. I'm inverting it by dividing 1 by the 'c' variable.

Here is a section of my code which is the actual formula, this is written in processing which is just Java with added visual libraries:

zx2=zx*zx;
zy2=zy*zy;
zy = 2*zx*zy + 1.0/(y); //the "1.0/" is what makes it inverted, a normal Mandelbrot set is just y and x on its own.
zx = zx2-zy2 + 1.0/(x);

It's extremely distorted when I run the code and doesn't even look like a teardrop! Here is what it looks like:

Distorted looking image

Then I tried fixing it by implementing the code of an answer, here's the code:

zx2=zx*zx;
zy2=zy*zy;
zy = 2*zx*zy + (y/(x*x+y*y));
zx = zx2-zy2 + (x/(x*s+y*y));       

But although it does look inverted, it's still distorted and doesn't look like a teardrop. Here's a photo:

Distorted Inverted Mandelbrot.

Have I done something wrong while implementing the code?


Solution

  • We need to think of c as a complex number, so in the normal Mandelbrot case, we have:

    zy = 2*zx * zy + cy;
    zx = zx2 - zy2 + cx;
    

    But to get the reciprocal of c, we have to do a complex reciprocal:

    zy = 2*zx * zy + (cy / (cx**2 + cy**2));
    zx = zx2 - zy2 + (cx / (cx**2 + cy**2));
    

    Of course, since c is constant from the loop's prespective, we can calculate the reciprocal before the loop. In a language like Python with complex numbers, it's a simple change from normal Mandelbrot:

    c = complex(real, imaginary)
    
    z = 0j
    
    for i in range(iterations):
        if abs(z) >= 4.0:
            break
    
        z = z * z + c
    

    to inverted Mandelbrot:

    c = 1 / complex(real, imaginary)
    
    z = 0j
    
    for i in range(iterations):
        # ...
    

    But if we're implementing the complex numbers ourselves, then for the normal Mandelbrot we do:

    x = real
    y = imaginary
    
    zx = 0
    zy = 0
    
    for i in range(iterations):
        zx2 = zx * zx
        zy2 = zy * zy
    
        if ((zx2 + zy2) ** 0.5) >= 4.0:
            break
    
        zy = 2*zx * zy + y
        zx = zx2 - zy2 + x
    

    and for the inverted Mandelbrot we do:

    denominator = real**2 + imaginary**2
    
    x = real / denominator
    y = imaginary / denominator
    
    zx = 0
    zy = 0
    
    for i in range(iterations):
        # ...
    

    Bottom line, it's the difference between:

    1 / complex(real, imaginary)  # correct
    

    and:

    complex(1 / real, 1 / imaginary)  # incorrect
    

    enter image description here