首页 > 解决方案 > 如何在 mandelbrot ppm 图像中获得更多细节

问题描述

当我在 C 中生成一个旨在表示 Mandelbrot 分形的 PPM 时,我只得到 4 个带有颜色的像素,表明它们属于该集合。这是有道理的,但是当我放大它并让我的 800X800 图像,每个像素代表一个介于 -2->2 之间的数字时,我在分形中没有得到额外的细节,它只是第一张图像中的“4”像素,但是每个都有 200x200 的大小。

我尝试过: - 在我的第二个函数中使用常量代替 (a/4) 和 (b/4)。- 改变图像的尺寸。- 在我的 FOR 循环中使用常量。- 评估 main() 函数中的每个像素,而不是单独的函数。- 在测试是否包含在集合中时更改我的迭代变量的值。

#include <math.h>

int inMandelbrot(int x, int y, int a, int b);
int main(){
    const int dimx = 800, dimy = 800;
    int x,y;
    FILE *mandelbrot;
    mandelbrot = fopen("mandelbrot.ppm","w");
    fprintf(mandelbrot, "P3\n%d %d\n255\n", dimx, dimy);
        for (y=-dimy/2; y< dimy/2 ;++y){
            for(x=-dimx/2;x<dimx/2;++x){
                if (inMandelbrot(x,y,dimx,dimy)==1){
                    fprintf(mandelbrot, "0 255 0\n");
                }
                else if (inMandelbrot(x,y,dimx,dimy)==0){
                    fprintf(mandelbrot, "0 0 255\n");
                }
                else if (inMandelbrot(x,y,dimx,dimy)==2){
                    fprintf(mandelbrot, "225 225 225\n");
                }
            }
        }
       (void) fclose(mandelbrot);
        return 1;
}

int inMandelbrot(int x, int y, int a, int b){
    double xnew,xold,ynew,yold;
    double Ex=(x/(a/4));
    double Ey=(y/(b/4));
    int iterations=0;
        xnew=0;
        ynew=0;
        yold=0;
        xold=0;
        while((xnew*xnew+ynew*ynew<4)&&(iterations<10000)){
                xnew=(xold*xold-yold*yold)+Ex;
                ynew=(2*xold*yold)+Ey;
                iterations++;
                xold=xnew;
                yold=ynew;
        }
        if (xnew*xnew+ynew*ynew>=4){
                iterations--;
                return 1;
        }
        else if (iterations>=10000){
            return 0;
            }
        return 2;
    }

我希望输出看起来像具有一定精度的 mandelbrot 分形,但是它只有 16 个 200x200“像素”而不是 64000 像素大小的像素。

标签: c

解决方案


推荐阅读