这个分形实现有什么不对?

时间:2011-04-09 17:10:06

标签: image image-processing processing

我的输出显示与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,(numIterations % 16) * 14, (numIterations % 128) * 2);
    }
  } 

  updatePixels();

   }

1 个答案:

答案 0 :(得分:2)

关键线是

zr = zrSquared - ziSquared + cr;
zi = 2.0*(zr*zi) + ci;

第二个分配使用已经新计算的zr,因此不是正确的值。您可以只交换两行:

zi = 2.0*(zr*zi) + ci;
zr = zrSquared - ziSquared + cr;