失败的JS Mandelbrot Set生成器输出奇数结构

时间:2016-12-10 18:56:52

标签: javascript mandelbrot

我昨晚在Javascript中制作了这个简单的Mandelbrot Set生成器,但它输出了一个非常奇怪的结构。我认为它看起来类似于mandelbrot集,但奇怪的变形。我不知道为什么它会像这样扭曲,我一直试图找出一整天。有谁知道导致这种情况的原因或如何解决这个问题?

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

1 个答案:

答案 0 :(得分: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]时,BOXContentClient *contentClient = [BOXContentClient defaultClient]; BOXFolderItemsRequest *listAllInRoot = [contentClient folderItemsRequestWithID:BOXAPIFolderIDRoot]; [listAllInRoot performRequestWithCompletion:^(NSArray *items, NSError *error) { //Do something with the results here } // Wait here for the completion handler to finish before moving on 不受影响。