我的 Mandelbrot 集代码有什么问题?
我正在尝试用 C 语言实现 Mandelbrot 集,但遇到了一个奇怪的问题。我的代码如下:
#include <stdio.h>
#include <math.h>
#include <complex.h>
int iterate_pt(complex c);
int main() {
FILE *fp;
fp = fopen("mand.ppm", "w+");
double crmin = -.75;
double crmax = -.74;
double cimin = -.138;
double cimax = -.75; //Changing this value to -.127 fixed my problem.
int ncols = 256;
int nrows = 256;
int mand[ncols][nrows];
int x, y, color;
double complex c;
double dx = (crmax-crmin)/ncols;
double dy = (cimax-cimin)/nrows;
for (x = 0; x < ncols; x++){
for (y = 0; y < nrows; y++){
double complex imaginary = 0+1.0i;
c = crmin+(x*dx) + (cimin+(y*dy)) * imaginary;
mand[x][y] = iterate_pt(c);
}
}
printf("Printing ppm header.");
fprintf(fp, "P3\n");
fprintf(fp, "%d %d\n255\n\n", ncols, nrows);
for (x = 0; x < ncols; x++) {
for (y = 0; y < nrows; y++){
color = mand[x][y];
fprintf(fp, "%d\n", color);
fprintf(fp, "%d\n", color);
fprintf(fp, "%d\n\n", color); //Extra new line added, telling the ppm to go to next pixel.
}
}
fclose(fp);
return 0;
}
int iterate_pt(double complex c){
double complex z = 0+0.0i;
int iterations = 0;
int k;
for (k = 1; k <= 255; k++) {
z = z*z + c;
if (sqrt( z*conj(z) ) > 50){
break;
}
else
++iterations;
}
return iterations;
}
但是,该程序的输出(存储为 ppm 文件)如下所示:
感谢您的帮助!
I'm trying to implement the Mandelbrot set in C, but I'm having a weird problem. My code is as follows:
#include <stdio.h>
#include <math.h>
#include <complex.h>
int iterate_pt(complex c);
int main() {
FILE *fp;
fp = fopen("mand.ppm", "w+");
double crmin = -.75;
double crmax = -.74;
double cimin = -.138;
double cimax = -.75; //Changing this value to -.127 fixed my problem.
int ncols = 256;
int nrows = 256;
int mand[ncols][nrows];
int x, y, color;
double complex c;
double dx = (crmax-crmin)/ncols;
double dy = (cimax-cimin)/nrows;
for (x = 0; x < ncols; x++){
for (y = 0; y < nrows; y++){
double complex imaginary = 0+1.0i;
c = crmin+(x*dx) + (cimin+(y*dy)) * imaginary;
mand[x][y] = iterate_pt(c);
}
}
printf("Printing ppm header.");
fprintf(fp, "P3\n");
fprintf(fp, "%d %d\n255\n\n", ncols, nrows);
for (x = 0; x < ncols; x++) {
for (y = 0; y < nrows; y++){
color = mand[x][y];
fprintf(fp, "%d\n", color);
fprintf(fp, "%d\n", color);
fprintf(fp, "%d\n\n", color); //Extra new line added, telling the ppm to go to next pixel.
}
}
fclose(fp);
return 0;
}
int iterate_pt(double complex c){
double complex z = 0+0.0i;
int iterations = 0;
int k;
for (k = 1; k <= 255; k++) {
z = z*z + c;
if (sqrt( z*conj(z) ) > 50){
break;
}
else
++iterations;
}
return iterations;
}
However, the output of this program, which is stored as a ppm file looks like this:
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试将 cimax 设置为 -0.127,我也在研究这个项目,它似乎可以解决问题;)
Try setting cimax to -0.127, I'm also working on this project and it seems to do the trick ;)
代码看起来不错。
但你的起始矩形看起来不正确!
您正在使用
您确定这是您想要的吗?对我来说,这似乎是一个非常拉伸的 Y 尺度。
此外,标准曼德尔布罗算法倾向于使用
而不是 50。
作为逃脱检查。尽管这不应该影响该集合的实际形状。
The code looks good.
But your starting rectangle doesn't look right!
you are using
are you sure this is what you intended? It seems like an awfully stretched y-scale to me.
Also, standard mandelbrot algorithms tend to use
rather than 50.
as an escape check. Though this shouldn't affect the actual shape of the set.
顺便说一句,计算 z*conj(z) 的 sqrt 是没有意义的。只需对不等式两边的表达式求平方,给出
if (z*conj(z) > 2500)
即可提高性能。BTW, there's no point in computing the sqrt of z*conj(z). Simply square the expressions on both sides of the inequality, giving
if (z*conj(z) > 2500)
and you've boosted the performance.