2016-12-10 1 views
2

어젯밤 Javascript에서이 간단한 만델 브로트 생성기를 만들었지 만 정말 이상한 구조를 나타냅니다. 나는 그것이 mandelbrot 세트와 비슷해 보이지만 이상하게 변형 된 것 같아요. 나는 왜 그것이 왜 이렇게 왜곡되는 것을 전혀 모릅니다. 그리고 나는 하루 종일 알아 내려고 노력했습니다. 누구나 원인이 무엇인지 또는이를 해결하는 방법을 알고 있습니까?실패한 JS 만델 브로 세트 생성기가 홀수 구조를 출력합니다

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

출력 이미지 :

오류가 calculateShape()이 줄 것으로 보인다 output image

답변

2

: 것 같다

   store = z; 

당신이 사본으로 store을 원한다 z이지만, 이것은 같은 배열을 참조하는 storez으로 끝납니다. 다음 줄은 z[0]을 계산하지만 storez은 같은 배열을 참조하므로 store[0]의 값은 이전보다 새로운 값 z[0]입니다. 따라서 그 이후의 행에있는 z[1]의 계산이 올바르지 않습니다. 당신이 z[0]를 다시 계산하면

store[0]은 영향을받지 않습니다, 그래서,이 라인의

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

또는

   store = z.slice(); 

모두 storez에 다른 배열을 의미 있도록 중 하나에 위의 라인을 교체합니다.

관련 문제