현재 문제가 해결되었습니다. 색깔이있는 프랙탈을보고 싶은 사람이 있다면 the code is here. 나는 작은 오류 (일부 도형이 제대로 그리는 일부는 아니다)가있는 것, 그럼에도 불구하고 알고리즘이 정직Julia JS 캔바스 구현
:
다음은 이전의 문제입니다. 당신은 jsFiddle에서 그것을 c = -1, 1/4로 정확하게 검사 할 수 있습니다. 그러나 프랙탈이 올바르게 그려지지만 c = i를 취하면됩니다. 이미지가 완전히 잘못되었습니다.
여기 구현되었습니다.
는 그것을 작성하는 나에게 몇 분이 걸렸다 또한
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);
}
}
}
HTML
<canvas id="a" width="400" height="400"></canvas>
JS, 나는 왜 모든 작동하지 않는 것을 발견하려고 많은 시간을 보냈다 사례. 내가 망 쳤던 어떤 생각?