【问题标题】:My Mandelbrot does not look like it should. Does anyone know why?我的 Mandelbrot 看起来不应该。有谁知道为什么?
【发布时间】:2020-08-03 23:37:12
【问题描述】:

我最近发现了 Mandelbrot 集,现在我正在尝试在 Processing 3.0 中生成一个 Mandelbrot 集。我在youtube 上找到了一个关于编程的教程,并试图在 Progressing 中实现它。

  background(255);
  size(1280, 720);
}

void draw(){
    int maxIteration = 100;
  
    double reMid = -0.75;
    double iMid = 0;
    
    double rangeR = 3.5;
    double rangeI = 2;
    
    double xPixels = 1280;
    double yPixels = 720;
    
    for(int x = 0; x < xPixels; x++){
        for(int y = 0; y < yPixels; y++){
            double xP       = (double)x / xPixels;
            double yP       = (double)y / yPixels;
          
            double cReal    = xP * rangeR + reMid - rangeR / 2;
            double cIm      = yP * rangeI + iMid - rangeI  / 2;
          
            double zReal    = 0;
            double zIm      = 0;
          
            int iteration   = 0;
            while(iteration < maxIteration && zReal * zReal + zIm * zIm <= 4) {
                double temp = zReal * zReal - cIm * cIm + cReal;
                zIm         = 2 * zReal * zIm + cIm;
                zReal       = temp;
                
                iteration++;
            }
            
            if(iteration >= maxIteration - 1){
                stroke(0);
            }
            else{
                stroke(255);
            }

            point(x, y);
        }
    }

}

但是当我生成 Mandelbrot 集时,它看起来与应有的不同:

我已经将我的代码与视频中的代码进行了比较,但我没有发现我的代码有错误。 有谁知道我做错了什么?

【问题讨论】:

标签: math processing fractals mandelbrot


【解决方案1】:

我把它缩小了一点,长尾巴一直延伸到无限远。由于所有|C| &gt; 2 都应该发散,因此很容易找到失败的特定案例,例如cReal = 2; cImg = -1.5;

您的代码说它会收敛,但手动操作表明它很快就会发散:

Z0 = 0 + 0i
Z1 = (0 + 0i)^2 + 2 - 1.5i = 2 - 1.5i
Z2 = 2*2 - 2*2*1.5i - 1.5^2 = 1.75 - 6i

单步执行您的代码会得到zReal, zImg

-1.5, -0.25
-0.75, -0.1875
-1.21875, -0.21484375
-0.976318359375, -0.2038421630859375
-1.1019703075289726, -0.20844837254844606
[...]

换句话说,你的循环是错误的。立即怀疑的代码行是这样的:

double temp = zReal * zReal - cIm * cIm + cReal;

它在做cIm*cIm,但不应该对 C 的任何组件进行任何乘法:它只是在末尾添加。

所以发生的事情是你不小心把zIm换成了cIm

将它们切换回去,您应该会得到更好的结果:

double temp = zReal * zReal - zIm * zIm + cReal;

【讨论】:

  • @that_other_guy 非常感谢,我搜索了一个小时,但没有找到这个简单的错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-06
  • 1970-01-01
  • 1970-01-01
  • 2021-11-25
  • 2011-09-29
  • 2011-05-22
相关资源
最近更新 更多