我正在尝试制作Mandlebrot集合的图片,因为我认为它们很漂亮。我想尝试用JavaScript解决绘制这种图形的问题,看看我能做些什么。我查看了几种算法,包括以下内容:http://library.thinkquest.org/26242/full/progs/a2.html。我将其翻译成了这个样子:
drawGraph: function(canvas,resolution,iterations,colors,coefficent){
var context = canvas.getContext('2d');
for(var m = 0; m < resolution.x; m++){
for(var n = 0; n < resolution.y; n++){
var x = m,
x2 = x*x,
y = n,
y2 = y*y;
var i;
for(i = 1; i < iterations; i++){
if(x2 + y2 > 4) break;
var new_x = x2 - y2 + coefficent.a;
var new_y = 2*x*y + coefficent.b;
x = new_x;
y = new_y;
}
var color = i % colors;
DrawUtils.drawPoint(context,m,n,color);
}
}
}
本质上绘制了一个单一颜色的方框。
然后我尝试了这个:
http://en.wikipedia.org/wiki/Mandelbrot_set#Escape_time_algorithm
我将其翻译为:
drawGraph: function(canvas,resolution,iterations,colors,coefficent){
var context = canvas.getContext('2d');
for(var m = 0; m < resolution.x; m++){
for(var n = 0; n < resolution.y; n++){
var x = 0,
y = 0,
x0 = ((m/resolution.x) * 3.5) - 2.5,
y0 = ((n/resolution.y) * 2) - 1;
var i = 0;
while(x*x + y*y < 4 && i < iterations){
var x_temp = x*x - y*y + x0;
y = 2*x*y + y0;
x = x_temp;
i++;
}
var color = 0;
if(x*x + y*y >= 4){
color = i % colors;
}
DrawUtils.drawPoint(context,m,n,color);
}
}
}
这将产生一个黑盒子。然而算法中的措辞有些让我困惑,因为它说x0和y0缩放是像素的因素,但在算法之后,它说系数c = x0 + iy0; 那么,这是否意味着我不需要将预定系数传递到函数中?
对于大多数这些测试,我使用的系数是0.25 + 0i,但我尝试了其他产生完全相同结果的系数。
我在这里做错了什么?