Search code examples
cmathfractalsmandelbrot

My Mandelbrot Set displays wrong outlines when plotting with few iterations


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?


Solution

  • 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)