Julia 设置 JavaScript
我正在摆弄一些 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,但我尝试了其他产生完全相同结果的系数。
我在这里做错了什么?
I'm messing around with some Mandlebrot set stuff because I think the pictures it produces are pretty. I thought I might try to tackle the problem of drawing one in javascript to see what I could do. I looked at a couple algorithms, namely:
http://library.thinkquest.org/26242/full/progs/a2.html
Which I translated into this:
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);
}
}
}
Which essentially draws a box of one color.
Then I tried this one:
http://en.wikipedia.org/wiki/Mandelbrot_set#Escape_time_algorithm
Which I translated into this:
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);
}
}
}
Which produces a black box. The wording in the algorithm kind of confused me though since it said x0 and y0 scaled are factors of the pixel, but then after the algorithm, it says the coefficient c = x0 + iy0; so, does that mean I don't pass a predetermined coefficient into the function?
For most of these tests I was using the coefficient 0.25 + 0i, but I tried others that produced the exact same results.
What am I doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一点:你需要清楚Julia集和Mandelbrot集之间的区别。两者都是对迭代下
f(z) = z^2 + c
行为的见解,但视角不同。对于 Julia 集合,我们修复
c
并绘制不同初始z
的行为图对于 Mandelbrot设置后,我们绘制了相同的初始
z = 0
对于不同c
的行为方式图。解决了这个问题...
对于您的第一个代码(它尝试为
coefficient
中的c
绘制 Julia 集),您从第一页中的 BASIC 翻译而来链接不太正确。哪里有你有
这很接近,但关键的一点是您没有采取任何步骤来实现
STEP x_resolution
。您的m
是一个整数,从0
到resolution.x - 1
,步长为1
;并且您的x
设置为m
。因此,您不是查看从
-2-2i
到2+2i
的复平面(查看 Julia 集的一个不错的视口),而是查看复平面从0
到resolution.x + resolution.y i
的平面,该平面最多在其左下角设置几个像素。第二个代码(尝试绘制 Mandelbrot 集)确实具有缩放到正确范围的代码,并且我无法立即看到出了什么问题 - 我会调试并查看是否
m /resolution.x 始终为
0
,正如 @user973572 所暗示的可能是问题所在。First point: you need to be clear about the difference between Julia sets and the Mandelbrot set. Both are insights into the behaviour of
f(z) = z^2 + c
under iteration, but from different perspectives.For a Julia set, we fix
c
and make a plot of how different initialz
s behaveFor the Mandelbrot set, we make a plot of how the same initial
z = 0
behaves for differentc
s.With that addressed...
For your first code (which tries to draw the a Julia set for the
c
incoefficient
), your translation from the BASIC in the first page you link to is not quite right. Where that hasyou have
which is close, except for the crucial point that you are not taking any steps to implement
STEP x_resolution
. Yourm
is an integer that runs from0
toresolution.x - 1
in steps of1
; and yourx
is set tom
.So instead of looking at the complex plane from say
-2-2i
to2+2i
(a decent viewport for seeing a Julia set), you are instead looking at the complex plane from0
toresolution.x + resolution.y i
, which will have at most a few pixels set in its lower-left corner.The second code (which attempts to draw a Mandelbrot set) does have code to scale to a correct range, and I can't immediately see what's going wrong - I would debug and see if
m/resolution.x
is always0
, as @user973572 suggests may be the problem.在你的第一个例子中,我认为你忘记更新 x2 和 y2 所以它们总是相同的值。在检查总和是否大于4之前,您需要更新x2和y2。类似的事情
可能是错误的,因为我对javascript一无所知。
In your first example, I think you forgot to update x2 and y2 so they are always the same value. You need to update x2 and y2 before checking if the sum is greater than 4. Something like
which is probably wrong because I know nothing about javascript.