4

I'm writing a program that plots the Mandelbrot set in C. I've been able to display it and it looks fine however when I lower the number of iterations I get this effect that generates what I can only describe as "clouds": Mandelbrot set

And here's how it should look (I got it from a website) :

Right Mandelbrot set.

How can I make mine look like the above? Here's the code that plots a single point:

double getFracPoint(double x,double y){

    //scale x and y
    x = x * ((plotEnd.x-plotStart.x) / SCREENWIDTH) + plotStart.x;
    y = y * ((plotEnd.y-plotStart.y) / SCREENHEIGHT) + plotStart.y;

    x/=zoom;
    y/=zoom;

    //instead of using the complex number library of the C standard
    //I decided to use regular numbers as it turns out to be faster.

    //The first number is the real part the second number is the imaginary
    //part.
    double z[2];
    z[0] = z[1] = 0;

    double c[2];
    c[0] = x;
    c[1] = y;

    int n = 0;

    for(int i = 0; i < ITERS; i++,n++){

        //if it's out of boundaries we are sure it does not belong to the set.
        if(z[0] > 4 || -4 > z[0] || -4 > z[1] || 4 < z[1])
            break;

        double t = z[1]; //store z[1]
        //multiply z to itself
        z[1] = (z[0] * z[1]) + (z[0] * z[1]);
        z[0] = z[0] * z[0] + -(t*t);

        //add C to Z
        z[0] += c[0];
        z[1] += c[1];

    }


    return (double)n/(double)ITERS;
}

What am I doing wrong here?

Gabriel
  • 75
  • 5
  • see [GLSL RT Mandelbrot](https://stackoverflow.com/a/56197067/2521214) for some inspiration on how to improve the look much much more... – Spektre Dec 29 '22 at 10:25

1 Answers1

5

Your "out of bounds" test checks that z falls within a square of radius 4:

    if(z[0] > 4 || -4 > z[0] || -4 > z[1] || 4 < z[1])

The typical test, however, is to check the Euclidean distance from origin (i.e. the complex number norm, i.e. check that it falls within a circle of radius 4):

    if(z[0]*z[0] + z[1]*z[1] > 4*4)
Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
  • This still won't produce continuous contours, but instead will show artifacts of the particular cutoff used. To get continuous contours, you need to iterate beyond a radius of 2 (which ifs the minimum), or 4, or any small number. Once the magnitude is large enough that subsequent additions of the original point are small, you can then interpolate based on the magnitude of the value and the number of iterations needed to reach it, producing continuous contours. It's the method used by all high-quality Mandelbrot set images, but most people don't understand it or even know about it. – Tom Karzes Dec 25 '22 at 00:37
  • @TomKarzes all true, but that's not what was asked. – Yakov Galka Dec 25 '22 at 02:08
  • Well, strictly speaking your `4*4` should be just `4`. That uses a radius of 2, which is what OP is presumably looking for. Changing it to 16 as you have will reduce the distortion, making it look closer to continuous contours but less like what OP expects. – Tom Karzes Dec 25 '22 at 14:22
  • 1
    @TomKarzes OP uses Linf(z) > 4 for the escape condition, I changed it to L2(z) > 4; that's the reasoning. – Yakov Galka Dec 25 '22 at 17:31