我的 Mandelbrot 集代码有什么问题?

发布于 2024-12-11 04:42:08 字数 1456 浏览 0 评论 0原文

我正在尝试用 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:

Converted to a GIF using GIMP. I can confirm that the GIF and original PPM look exactly the same as a PPM and GIF

Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

玩世 2024-12-18 04:42:08

尝试将 cimax 设置为 -0.127,我也在研究这个项目,它似乎可以解决问题;)

Try setting cimax to -0.127, I'm also working on this project and it seems to do the trick ;)

椒妓 2024-12-18 04:42:08

代码看起来不错。
但你的起始矩形看起来不正确!

您正在使用

Real ranage [  -.75 ,  -.74 ]
Imag range  [ -.138 ,  -.75 ]

您确定这是您想要的吗?对我来说,这似乎是一个非常拉伸的 Y 尺度。

此外,标准曼德尔布罗算法倾向于使用

magnitude > 2

而不是 50。
作为逃脱检查。尽管这不应该影响该集合的实际形状。

The code looks good.
But your starting rectangle doesn't look right!

you are using

Real ranage [  -.75 ,  -.74 ]
Imag range  [ -.138 ,  -.75 ]

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

magnitude > 2

rather than 50.
as an escape check. Though this shouldn't affect the actual shape of the set.

仙女 2024-12-18 04:42:08

顺便说一句,计算 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文