不正确的 mandelbrot 集输出绘图

improper mandelbrot set output plotting

我正在尝试编写代码来显示 Mandelbrot 集之间的数字 (-3,-3) 到 (2,2) 在我的终端上。 main 函数生成一个复数并将其提供给 analyze 函数。 分析函数returns 字符“*”为集合内的复数Z和“.”对于位于集合之外的数字。

代码:

#define MAX_A   2       // upperbound on real
#define MAX_B   2       // upper bound on imaginary

#define MIN_A   -3      // lowerbnd on real
#define MIN_B   -3      // lower bound on imaginary

#define NX      300     // no. of points along x
#define NY      200     // no. of points along y

#define max_its 50
int analyze(double real,double imag);
void main()
{
double a,b;
int x,x_arr,y,y_arr;
int array[NX][NY];

int res;

for(y=NY-1,x_arr=0;y>=0;y--,x_arr++)
{
 for(x=0,y_arr++;x<=NX-1;x++,y_arr++)
        {
        a= MIN_A+ (  x/( (double)NX-1)*(MAX_A-MIN_A) );
        b= MIN_B+ (  y/( (double)NY-1 )*(MAX_B-MIN_B) );
        //printf("%f+i%f ",a,b);        
        res=analyze(a,b);

        if(res>49)
                array[x][y]=42;
        else
                array[x][y]=46;


        }
//      printf("\n");

}

for(y=0;y<NY;y++)
        {
        for(x=0;x<NX;x++)
                printf("%2c",array[x][y]);
        printf("\n");
        }

}

分析函数接受假想平面上的坐标; 并计算 (Z^2)+Z 50 次;并且在计算复数是否爆炸时,然后函数 returns 在完成 50 次迭代后立即函数 returns;

  int analyze(double real,double imag)
    {
     int iter=0;
     double r=4.0;

            while(iter<50)
            {
                    if (  r < ( (real*real) + (imag*imag) )  )
                    {
                            return iter;
                    }
                    real=  ( (real*real) - (imag*imag) + real);
                    imag=  ( (2*real*imag)+ imag);
                    iter++;
            }
            return iter;

    }

所以,我正在分析 60000 (NX * NY) 个数字并将其显示在终端上 考虑到 3:2 ratio (300,200) ,我什至尝试了 4:3 (NX:NY) ,但输出保持不变并且生成的形状甚至不接近 mandlebrot 集:

因此,输出显示为反转, 我浏览并遇到了像这样的行:

   (x - 400) / ZOOM;
   (y - 300) / ZOOM;

关于许多 mandelbrot 代码,但我无法理解这一行如何纠正我的输出。

我想我在将输出映射到终端时遇到了问题!

(LB_Real,UB_Imag) --- (UB_Real,UB_Imag)
    |                       |
(LB_Real,LB_Imag) --- (UB_Real,LB_Imag)

任何 Hint/help 都会非常有用

Mandelbrot 递归为 zn+1 = zn2 + c。

这是您的实现:

real=  ( (real*real) - (imag*imag) + real);
imag=  ( (2*real*imag)+ imag);

问题 1。在使用旧值计算新值 imag 之前,您正在将 real 更新为其下一个值。

问题 2。假设你解决了问题 1,你正在计算 zn+1 = zn 2 + zn.

以下是我使用 double 的方法:

int analyze(double cr, double ci) {
    double zr = 0, zi = 0;
    int r;
    for (r = 0; (r < 50) && (zr*zr + zi*zi < 4.0); ++r) {
        double zr1 = zr*zr - zi*zi + cr;
        double zi1 = 2 * zr * zi + ci;
        zr = zr1;
        zi = zi1;
    }
    return r;
}

但是如果使用标准的 C99 对复数的支持会更容易理解:

#include <complex.h>

int analyze(double cr, double ci) {
    double complex c = cr + ci * I;
    double complex z = 0;
    int r;
    for (r = 0; (r < 50) && (cabs(z) < 2); ++r) {
        z = z * z + c;
    }
    return r;
}