gpt4 book ai didi

代码有助于确定点是否在 Mandelbrot 集中(检查我的解决方案)

转载 作者:太空宇宙 更新时间:2023-11-04 04:57:02 27 4
gpt4 key购买 nike

这是我的函数,用于测试两个点 x 和 y 在 MAX_ITERATION 255 之后是否在 mandelbrot 集合中。如果不在,它应该返回 0,如果在,则返回 1。

int isMandelbrot (int x, int y) {


int i;
int j;
double Re[255];
double Im[255];
double a;
double b;
double dist;
double finaldist;
int check;

i=0;
Re[0]=0;
Im[0]=0;
j=-1;
a=0;
b=0;

while (i < MAX_ITERATION) {

a = Re[j];
b = Im[j];

Re[i]=((a*a)-(b*b))+x;
Im[i]=(2 * a * b) + y;

i++;
j++;
}

finaldist = sqrt(pow(Re[MAX_ITERATION],2)+pow(Im[MAX_ITERATION],2));

if (dist > 2) { //not in mandelbrot
check = 0;
} else if (dist <= 2) { //in mandelbrot set
check = 1;
}

return check;
}

鉴于它是正确的(有人可以验证......或者写一个更有效的吗?)。这是我打印它的代码,但是它不起作用! (它不断给出所有点都在集合中)。我在这里做错了什么?

int main(void) {

double col;
double row;

int checkSet;

row = -4;
col = -1;

while (row < 1.0 ) {
while (col < 1.0) {
checkSet = isMandelbrot(row, col);
if (checkSet == 1) {
printf("-");
} else if (checkSet == 0) {
printf("*");
}
col=col+0.5;
}
col=-1;
row=row+0.5;
printf("\n");
}
return 0;
}

最佳答案

您的代码中存在一些错误。例如,您这样做:

a = Re[j];
b = Im[j];

但在第一次迭代中,j = -1,因此您将获得数组索引 -1 处的值。那不是你想做的。

此外,为什么是 ReIm 数组 - 您真的需要跟踪计算中的所有中间结果吗?

维基百科包含 the algorithm 的伪代码,您可能需要检查自己的代码。

关于代码有助于确定点是否在 Mandelbrot 集中(检查我的解决方案),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5685744/

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