2016-12-10 23 views
2

我昨晚在Javascript中做了这个简单的Mandelbrot Set生成器,但它输出了一个非常奇怪的结构。我认为它看起来与mandelbrot集合相似,但奇怪地变形了。我不知道为什么它会这样扭曲,我一直在努力寻找整天。有谁知道是什么原因或如何解决这个问题?失败的JS Mandelbrot Set发生器输出奇数结构

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; 
} 

输出图像:

output image

回答

2

的错误似乎是在这条线在calculateShape()

   store = z; 

看来你想store作为复制z,但这只是结束于storez指的是相同的数组。下一行计算z[0],但是因为storez指的是相同的数组,因此store[0]具有新值z[0]而不是前一个。因此,在此之后的行中计算z[1]是不正确的。

与任一

   store = [z[0], z[1]]; 

   store = z.slice(); 

这两种线确保store指不同阵列z替换上面的行,所以当重新计算z[0]store[0]不受影响。