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);
}
function conversion(x, y, width, R){
var m = R / width;
var x1 = m * (2 * x - width);
var y2 = m * (width - 2 * y);
return [x1, y2];
}
function f(z, c){
return [z[0]*z[0] - z[1] * z[1] + c[0], 2 * z[0] * z[1] + c[1]];
}
function abs(z){
return Math.sqrt(z[0]*z[0] + z[1]*z[1]);
}
function init(){
var length = 400,
width = 400,
c = [-1, 0],
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++){
flag = true;
z = conversion(x, y, width, R);
for (var i = 0; i < maxIterate; i++){
z = f(z, c);
if (abs(z) > R){
flag = false;
break;
}
}
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?