【发布时间】:2011-04-16 09:59:46
【问题描述】:
这是我的函数,它测试两个点 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;
}
【问题讨论】:
-
你似乎没有从
main()调用你的函数... -
哎呀,复制了错误的主目录。
-
效率详情:不要使用
pow(xxx, 2),pow函数旨在计算任意数字的 x^y。它将通过使用复杂算法的近似来做到这一点。请改用x*x。sqrt(x) < 2与x < 4相同,但速度更快。 (这在 20 年前我在 C64 上画曼德布罗时非常明显。)
标签: c function ascii mandelbrot