5

The problem is currently solved. In case some one wants to see the colored fractal, the code is here.

Here is the previous problem:

Nonetheless the algorithm is straight forward, I seems to have a small error (some fractals are drawing correctly and some are not). You can quickly check it in jsFiddle that c = -1, 1/4 the fractal is drawing correctly but if I will take c = i; the image is totally wrong.

Here is implementation.

HTML

<canvas id="a" width="400" height="400"></canvas>

JS

function point(pos, canvas){
    canvas.fillRect(pos[0], pos[1], 1, 1);  // there is no drawpoint in JS, so I simulate it
}

function conversion(x, y, width, R){   // transformation from canvas coordinates to XY plane
    var m = R / width;
    var x1 = m * (2 * x - width);
    var y2 = m * (width - 2 * y);
    return [x1, y2];
}

function f(z, c){  // calculate the value of the function with complex arguments.
    return [z[0]*z[0] - z[1] * z[1] + c[0], 2 * z[0] * z[1] + c[1]];
}

function abs(z){  // absolute value of a complex number
    return Math.sqrt(z[0]*z[0] + z[1]*z[1]);
}

function init(){
    var length = 400,
        width = 400,
        c = [-1, 0],  // all complex number are in the form of [x, y] which means x + i*y
        maxIterate = 100,
        R = (1 + Math.sqrt(1+4*abs(c))) / 2,
        z;

    var canvas = document.getElementById('a').getContext("2d");

    var flag;
    for (var x = 0; x < width; x++){
        for (var y = 0; y < length; y++){  // for every point in the canvas plane
            flag = true;
            z = conversion(x, y, width, R);  // convert it to XY plane
            for (var i = 0; i < maxIterate; i++){ // I know I can change it to while and remove this flag.
                z = f(z, c);
                if (abs(z) > R){  // if during every one of the iterations we have value bigger then R, do not draw this point.
                    flag = false;
                    break;
                }
            }
            // if the
            if (flag) point([x, y], canvas);
        }
    }
}

Also it took me few minutes to write it, I spent much more time trying to find why does not it work for all the cases. Any idea where I screwed up?

4

1 回答 1

5

好消息!(或坏消息)

你的实施是完全的。正确的。不幸的是,有了c = [0, 1],Julia 集的点很少。我相信它是零测量(不像说,曼德布罗集)。所以一个随机点在那个 Julia 集中的概率是 0。

如果您将迭代次数减少到 15 ( JSFiddle ),您可以看到分形。一百次迭代更“准确”,但随着迭代次数的增加,400 x 400 网格上的一个点将包含在分形近似中的机会减少到零。

通常,您会看到 Julia 分形有多种颜色,其中颜色表示它发散的速度(或根本不发散),就像在这个Flash 演示中一样。这使得 Julia 分形即使在像 c = i 这样的情况下也有些可见。

你的选择是

(1) 减少迭代次数,可能取决于c.

(2) 增加采样(和画布)的大小,可能取决于c.

(3) 根据R超出的迭代次数为画布上的点着色。

最后一个选项将为您提供最强大的结果。

于 2013-10-30T00:50:09.013 回答