squares

Yet another Vera Molnar homage

Log in to post a comment.

// You can find the Turtle API reference here: https://turtletoy.net/syntax
Canvas.setpenopacity(1);

// Global code will be evaluated once.
const turtle = new Turtle();

//adding some random jitter
const skew = 0; //min = 0, max = 100, step = 2

//how often to try packing large squares
const iterations = 20; //min = 0, max = 100, step = 1

//how dense to fill the squares
const sparseness = 2; //min = 1, max = 100, step = 0.1

//largest allowable square
const maxSize = 19; //min = 1, max = 19, step = 1



function randomNumber(min, max) {
    return Math.floor(Math.random() * (max - min) + min);
}

function drawSquare (size) {

    turtle.setheading(0);
    turtle.right((skew*Math.random()-skew/2)/size);    
    turtle.forward(size);
    turtle.right(90);
    turtle.forward(size);
    turtle.right(90);
    turtle.forward(size);
    turtle.right(90);
    turtle.forward(size);

}

function filledSquares(size) {
    currentSize = size;
    startPos = turtle.position();
    drawSquare(currentSize);
    for (let i = 0; i < randomNumber(1,size*size/sparseness); i++) {
        offset = randomNumber(0,(size-1)/2);
        currentSize = size - (2*offset);
        turtle.setheading(0);
        turtle.penup();
        turtle.forward(offset);
        turtle.right(90);
        turtle.forward(offset);
        turtle.pendown();
        drawSquare(currentSize);
        turtle.jump(startPos);
    }    
    
}

var arr = [];
function initGrid() {
    for(let i = 0; i < 20; i++) {
        arr[i]=[];
            for(let j = 0; j < 20; j++) {
                arr[i][j]=0;
            }    
    }
}

//create larger squares
function squarePack() {
    for (let i=0; i<iterations;i++) {
        squareSize = randomNumber(2,5)
        squarex = randomNumber(0,19);
        squarey = randomNumber(0,19);
        squareOkay = true;

        for (let x=0; x<squareSize; x++) {
            for (let y=0; y<squareSize; y++) {
                if (squarex+x > 19 || squarey+y >19) {
                    squareOkay = false;
                }
                else if (arr[squarex+x][squarey+y]!=0) {
                    squareOkay = false;
                }
            }
        }

        if (squareOkay=true) {

            for (let x=0; x<squareSize; x++) {
                for (let y=0; y<squareSize; y++) {
                    posx = squarex+x;
                    posy = squarey+y;
                    console.log(posx + " " + posy);
                    arr[posx][posy] = 1;
                }
            }
            
            turtle.jump((squarex*10)-100,(squarey*10)-100); 
            filledSquares((squareSize * 10)-1);              
        }
        
    }
}
    



// The walk function will be called until it returns false.
function walk(r) {
    initGrid();

// pack larger squares
    for (let i=0; i<iterations;i++) {
        squareSize = randomNumber(2,maxSize)
        squarex = randomNumber(0,19);
        squarey = randomNumber(0,19);
        squareOkay = true;

        for (let x=0; x<squareSize; x++) {
            for (let y=0; y<squareSize; y++) {
                if (squarex+x > 19 || squarey+y >19) {
                    squareOkay = false;
                }
                else if (arr[squarex+x][squarey+y]!=0) {
                    squareOkay = false;
                }
            }
        }

        if (squareOkay==true) {

            for (let x=0; x<squareSize; x++) {
                for (let y=0; y<squareSize; y++) {
                    posx = squarex+x;
                    posy = squarey+y;
                    arr[posx][posy] = 1;
                }
            }
            
            turtle.jump((squarex*10)-100,(squarey*10)-100); 
            filledSquares((squareSize * 10)-1);              
        }
        
    }

//fill in blank spots with little squares
    for(let x = 0; x < 20; x++) {
        for(let y = 0; y < 20; y++) {
            if (arr[x][y]==0) {
                turtle.jump((x*10)-100,(y*10)-100);

                filledSquares(9)
            }

        }
    }


    return r = false;
}