【发布时间】:2017-10-31 00:40:37
【问题描述】:
我正在尝试制作一个 julia 集图像并用 C 进行编程。我曾尝试研究一种算法来创建 Julia 集的图像,但对大多数在线示例感到困惑(因为大多数示例似乎是复制的,但对逐步发生的事情几乎没有解释)。
我已经多次重写我的代码以适应我在网上找到的算法,但收效甚微。目前,我正在使用迭代函数来定义以下每个像素的颜色:
// Loop through each pixel to work out new image set
int x, y;
for(x = 0; x < SIZE; x++){
for(y = 0; y < SIZE; y++){
int i = iterate(x, y, maxIterations, c);
image[x][y] = i % 256 + 255 + 255 * (i < maxIterations);
}
}
/* Iterate function */
int iterate(int x, int y, int maxI, double c[]){
double z, zx, zy, oldRe, oldIm; //real and imaginary parts of new and old
double xmin = -1.0, xmax = 1.0, ymin = -1.0, ymax = 1.0;
int k; // number of times iterated
//calculate the initial real and imaginary part of z
// z0 = (x + yi)^2 + c = (0 + 0i) + c = c
zx = 1.5*(x - SIZE/2)/(0.5*SIZE);
zy = 1.0*(y - SIZE/2)/(0.5*SIZE);
//start the iteration process
for(k = 1; k < maxI; k++){
//remember value of previous iteration
oldRe = zx;
oldIm = zy;
z = zx*zx - zy*zy + c[0];
//the actual iteration, the real and imaginary part are calculated
zx = oldRe * oldRe - oldIm * c[1] + c[0];
zy = 2 * oldRe * oldIm + c[1];
zy = 2.0*zx*zy + c[1];
zx = z;
//if the point is outside the circle with radius 2: stop
if((zx * zx + zy * zy) > 4) break;
}
/*
while(((zx * zx + zy * zy) > 4) && (k < maxI)) {
//remember value of previous iteration
z = zx*zx - zy*zy + c[0];
zy = 2*x*y-c[1];
zx = z;
//if the point is outside the circle with radius 2: stop
}
return k;
} */ 注释掉的 while 循环是我在网上找到的第二种算法,但也是不正确的。我不确定我在等式中遗漏了什么。有什么猜测吗?
(我不是在寻找解决方案,只是一些可以将实数、虚数复数转换为我的边界框中的位置的伪代码,在本例中为:-1,1)。
【问题讨论】:
-
复数类型是 c 中内置的类型,所以可以使用 z = z*z + c
标签: c algorithm math mandelbrot