【发布时间】:2016-01-24 19:01:41
【问题描述】:
我想在 C 中将 Mandelbrot 绘制到 PPM 文件中。我的代码正在运行,但我的绘图始终是黑色的。我有这个来自wikia的代码。我成功的关键是思考“阿尔法”(我是这么认为的)。我不知道阿尔法应该是什么。这是我的代码:
#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;
}
【问题讨论】:
-
iteration和max_interation是整数,所以(iteration / max_iteration)是整数除法,当iteration小于max_iteration时总是会产生0。去掉括号,让乘法在前。 -
您的迭代代码可能需要对位置
i和j做一些事情。只有在分配颜色时才使用它们。您是否应该通过i和j初始化x0和y0,例如通过将它们映射到欲望区间? -
最后,我认为您应该将 Mandelbrodt 值映射到 0 到 255 范围内的颜色。
-
如果我理解正确:1-您在互联网上获取了一些随机代码,2-它没有按预期工作,3-您要求我们对其进行调试。请编辑问题并至少解释您已经尝试过的内容。
-
谢谢大家 alfa = 255 :D
标签: c ppm mandelbrot