JS canvas implementation of Julia set

The problem is currently resolved. If someone wants to see a color fractal, the code is here .

Here is the previous problem:

Nevertheless, the algorithm is straightforward, I seem to have a small error (some fractals are drawn correctly, and some are not). You can quickly check this in jsFiddle , that c = -1, 1/4 fractal draws correctly, but if I take c = i; The image is completely wrong.

Here is the 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);
        }
    }
}

And it took me a few minutes to write it, I spent much more time trying to find why it does not work for all cases. Any idea where I squinted?

+4
1

! ( )

. . , c = [0, 1] Julia . , ( , , ). , , , 0.

15 (JSFiddle), . "", , 400 x 400 , .

, Julia , , ( ), Flash-. , c = i.

(1) , , c.

(2) ( ), , c.

(3) #, R.

.

+4

All Articles