Grid

A simple grid generator based on two line rows

Log in to post a comment.

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

const degrees_to_radians = deg => (deg * Math.PI) / 180.0;

const deg1 = 60; //min=0 max=90 step=1
const distance1 = 4.6; // min=1 max=100 step=0.1

const deg2 = 0; //min=0 max=90 step=1
const distance2 = 17.1; // min=1 max=100 step=0.1

const drawBox = 1; //min=0 max=1 step=1 (No, Yes)

const sizeX = 190; //min=0 max=200 step=1 
const sizeY = 190; //min=0 max=200 step=1 

const offset = 5; // min=0 max=200 step=1
const canvas = { ox: -100 + offset, oy: -100 + offset }
    
// Global code will be evaluated once.
const turtle = new Turtle();


function box() {
    for (let i = 0; i < 4; i++) {
        turtle.forward(i % 2 ? sizeY : sizeX);
        turtle.right(90)
    }
}

function drawGrid(sx, sy, d, deg, offset) {
    const { ox, oy } = offset;
    const rad =  degrees_to_radians(deg);
    const [cx, cy] = [d / Math.sin(rad), d / Math.cos(rad)];
    const [stepsX, stepsY] = [Math.ceil(sx / cx), Math.ceil(sy / cy)];
    
    turtle.setheading(deg);
    
    for (let i = 0; i < stepsX; i++) { 
        turtle.jump(ox + i * cx, oy);
        
        turtle.forward(Math.min(sy / Math.sin(rad), (sx - (i * cx)) / Math.cos(rad)));
    }

    for (let i = 0; i < stepsY; i++) { 
        turtle.jump(ox, oy + i * cy);
        
        turtle.forward(Math.min(sx / Math.cos(rad) ,(sy - (i * cy)) / Math.sin(rad)));
    }
}

// The walk function will be called until it returns false.
function walk(i) {
    turtle.jump(canvas.ox, canvas.oy);
    drawBox && box(canvas);

    drawGrid(sizeX, sizeY, distance1, deg1, canvas);
    drawGrid(sizeX, sizeY, distance2, deg2, canvas);

    return false;
}