Javascript 失败的JS Mandelbrot集生成器输出奇数结构
昨晚我用Javascript制作了这个简单的Mandelbrot集生成器,但它输出了一个非常奇怪的结构。我觉得它看起来很像曼德布罗特的套装,但变形很奇怪。我不知道它为什么会这样扭曲,我一整天都在试图弄清楚。有人知道这是什么原因或如何修复吗Javascript 失败的JS Mandelbrot集生成器输出奇数结构,javascript,mandelbrot,Javascript,Mandelbrot,昨晚我用Javascript制作了这个简单的Mandelbrot集生成器,但它输出了一个非常奇怪的结构。我觉得它看起来很像曼德布罗特的套装,但变形很奇怪。我不知道它为什么会这样扭曲,我一整天都在试图弄清楚。有人知道这是什么原因或如何修复吗 c = document.getElementById("canvas"); ctx = c.getContext("2d"); c.width = 4000; c.height = 4000; declareVariables(); calculateSh
c = document.getElementById("canvas");
ctx = c.getContext("2d");
c.width = 4000;
c.height = 4000;
declareVariables();
calculateShape();
drawShape();
function declareVariables() {
Re = -2;
Im = -2;
input = [Re,Im];
precision = prompt("input precision (higher is better)");
precision = 1/(precision - precision%4);
segmentAmt = 4/precision;
segmentSize = c.width/segmentAmt;
iterate = prompt("input test amount (higher is better)");
set = [];
for (i=0; i<segmentAmt; i++) {
set[i] = [];
}
numberGrid = [];
for (i=0; i<segmentAmt; i++) {
numberGrid[i] = [];
for (j=0; j<segmentAmt; j++) {
}
}
}
function calculateShape() {
for (i=0; i<segmentAmt; i++) {
input[1] = -2;
input[0] += precision;
for (j=0; j<segmentAmt; j++) {
input[1] += precision;
set[i][j] = 0;
z = [0,0];
for (k=1; k<=iterate; k++) {
store = z;
z[0] = store[0]**2 - store[1]**2 + input[0];
z[1] = 2 * store[0] * store[1] + input[1];
if (z[0]**2 + z[1]**2 > 4) {
set[i][j] = k;
k = iterate+1;
}
}
}
}
}
function drawShape() {
ctx.fillStyle = "white";
ctx.fillRect(0,0,c.width,c.height);
for (i=0; i<segmentAmt; i++) {
for (j=0; j<segmentAmt; j++) {
if (set[i][j] == 0) {
ctx.fillStyle = "black";
} else if (set[i][j] >= 1) {
ctx.fillStyle = 'hsl(' + (25*(set[i][j]-1))**0.75 + ', 100%, 50%)';
}
convertCoords(i,j);
ctx.fillRect(xCoord,yCoord,segmentSize,segmentSize);
}
}
}
function convertCoords(var1,var2) {
xCoord = var1 * segmentSize;
yCoord = var2 * segmentSize;
}
c=document.getElementById(“画布”);
ctx=c.getContext(“2d”);
c、 宽度=4000;
c、 高度=4000;
申报表();
calculateShape();
drawShape();
函数declareVariables(){
Re=-2;
Im=-2;
输入=[Re,Im];
精度=提示(“输入精度(越高越好)”;
精度=1/(精度-精度%4);
分段amt=4/精度;
分段尺寸=c.宽度/分段金额;
迭代=提示(“输入测试量(越高越好)”;
集合=[];
对于(i=0;i而言,calculateShape()
中的这一行似乎有错误:
似乎您希望store
是z
的副本,但这只会导致store
和z
引用同一数组。下一行计算z[0]
,但当store
和z
引用同一数组时,store[0]
具有新值z[0]
而不是前一行。因此,在后一行中计算z[1]
是不正确的
将上面的行替换为
store = [z[0], z[1]];
或
这两行确保store
引用了与z
不同的数组,因此当您重新计算z[0]
时,store[0]
不受影响
store = [z[0], z[1]];
store = z.slice();