Mandelbrot PPM绘图总是黑色的

时间:2016-01-24 19:01:41

标签: c ppm mandelbrot

我想在C中绘制Mandelbrot到PPM文件。我的代码工作正常,但我的绘图总是黑色的。我有来自wikia的代码。我成功的关键是思考" alfa" (我认同)。我不知道阿尔法应该是什么。这是我的代码:

#include <stdio.h>
#include <stdlib.h>

#define W 800
#define H 800

//rgb struct
struct RGB {
    int r;
    int g;
    int b;
};
struct RGB picture[W][H];

void draw() {
int i, j, iteration, max_iteration, alfa, color;
float x, y, x0, y0, xtemp;

for(i=0;i<W;++i)
{
    for(j=0;j<H;++j)
    {
         x0 = 1; //scaled x (e.g interval(-2.5, 1))
         y0 = -1; //scaled y (e.g interval(-1, 1))
         x = 0.0;
         y = 0.0;
         iteration = 0;
         max_iteration = 1000;

         while (x*x + y*y < 2*2 && iteration < max_iteration)
         {
             xtemp = x*x - y*y + x0;
             y = 2*x*y + y0;
             x = xtemp;
             iteration = iteration+ 1;
             alfa = x*y; //???
         }

         color = alfa * (iteration / max_iteration);

         picture[i][j].r = color;
         picture[i][j].g = color;
         picture[i][j].b = color;
    }
}
}

int main() {

//variables
int i,j;

draw();

FILE *fp;
fp = fopen("picture.ppm", "w");
fprintf(fp,"P3\n#test\n%d %d\n256\n", W, H);

for (i=0; i < W; ++i)
    {
        for (j=0; j < H; ++j)
        {
        fprintf(fp,"%d %d %d ", picture[i][j].r, picture[i][j].g , picture[i][j].b);
        }
    fprintf(fp, "\n");
    }

fclose(fp);

return 0;
}

1 个答案:

答案 0 :(得分:1)

在这一行

color = alfa * (iteration / max_iteration);

divison iteration / max_iterationint分区,其结果始终为0(如果1,可能为iteration == max_iteration

尝试使用float,或像这样重新排列

color = alfa * iteration / max_iteration;

删除括号。但是你必须注意int范围并没有被打破。

话虽如此,似乎你不确定自己alfa是什么。我建议255,这样你就会得到一张灰度图像。