# Mandelbrot set incorrect picture

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

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;
}
}

• A description of your algorithm in psuedocode would be more appropriate for this site. – Austin Mohr Jul 6 '13 at 12:21
• It is called Mandelbrot, by the way. See en.wikipedia.org/wiki/Mandelbrot_set – martin.koeberl Jul 6 '13 at 12:22
• I think you knowingly/unknowingly created java-applet tag. (it has zero tags, correct me if not). I think it is not appropriate for this site. – Dinesh Jul 6 '13 at 12:26
• @Edit: The java-applet has six questions tagged (without counting this one). – Asaf Karagila Jul 6 '13 at 12:28
• Why do you initialise complexC and realC to $0.4$ and $0.3$ respectively? They should both be initialised to $0$. – Peter Taylor Jul 6 '13 at 13:20

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.