0

I'm writing an algorithm to generate the Mandelbrot set in Java. However, the final picture is incorrect. It looks like this

enter image description here

I was wondering if the algorithm was incorrect.

public void Mandlebrot() {
    float reMax=2;
    float imMax=2;
    float reMin=-2;
    float imMin=-2;
    float xDelta = (reMax - reMin)/test.width;
    float yDelta = (imMax - imMin)/test.height;

    int N=20000;
    float complex = imMin;
    for(int y=0; y<test.height; y++) {
        float real = reMin;
        for(int x=0; x<test.width; x++) {
            int count = 0;
            float complexC = 0.4f;
            float realC = 0.3f;

            while(count<N && complexC*complexC+realC*realC<=4.0f) {
                complexC = realC*realC-complexC*complexC + complex;
                realC = 2*complexC*realC + real;
                count++;
            }

            if(complexC*complexC+realC*realC<=4.0f) {
                setPixel(x,y) = 1000;
            } else {
                setPixel(x,y) = 1+ 1000*count/N;
            }

            real+=xDelta;
        }
        complex+=yDelta;
    }
}
Daniel Fischer
  • 206,697
simplicity
  • 3,694

1 Answers1

4

In the loop

while(count<N && complexC*complexC+realC*realC<=4.0f) {
    complexC = realC*realC-complexC*complexC + complex;
    realC = 2*complexC*realC + real;
    count++;
}

You use the updated complexC to compute the new realC, but you ought to use the old one:

float oldC = complexC;
complexC = realC*realC-complexC*complexC + complex;
realC = 2*oldC*realC + real;
count++;

Besides, you seem to have flipped the real and imaginary parts. I think that only rotates the picture, though.

Daniel Fischer
  • 206,697