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;
}
}
In the loop
You use the updated
complexCto compute the newrealC, but you ought to use the old one:Besides, you seem to have flipped the real and imaginary parts. I think that only rotates the picture, though.