what is the wrong in this fractal implementation?
my output isn't showing the same result as Mandelbrot fractal
void mandelbrotFractal()
{
float MAX = 200;
float numIterations;
float ci;
float cr;
float zi;
float zr;
loadPixels();
for(int j = 0; j < height; j++)
{
for(int i = 0; i < width; i++)
{
//H[-2.0,2.0]
//V[-1.5,1.5]
cr = - 2.0 + ((i*(2.0+2.0))/width) ;
ci = - 1.5 + ((j*(1.5+1.5))/height);
numIterations = 0;
zi =0;
zr =0;
while(numIterations < MAX)
{
float zrSquared = (zr*zr);
float ziSquared = (zi*zi);
zr = zrSquared - ziSquared + cr;
zi = 2.0*(zr*zi) + ci;
numIterations = numIterations + 1;
if((zr*zr) + (zi*zi) > 4.0)
break;
}
color r = int((numIterations % 32) * 7);
color g = int((numIterations % 16) * 14);
color b = int((numIterations % 128) * 2);
if(numIterations == MAX)
pixels[j*width + i] = color(0);
else
pixels[j*width + i] = color((numIterations % 32) * 7,(numIteratio开发者_StackOverflow中文版ns % 16) * 14, (numIterations % 128) * 2);
}
}
updatePixels();
}
The crucial lines are
zr = zrSquared - ziSquared + cr;
zi = 2.0*(zr*zi) + ci;
The second assignment uses the already newly calculated zr
and thus not the correct value. You may just swap both lines:
zi = 2.0*(zr*zi) + ci;
zr = zrSquared - ziSquared + cr;
精彩评论