gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 17:23:55 24 4
gpt4 key购买 nike

我正在尝试用 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 文件)如下所示:

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

感谢您的帮助!

最佳答案

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

关于c - 我的 Mandelbrot 集代码有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7829809/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com