Tangram ⬛

A lot of parameters to set all pieces, but you can make all Tangram puzzles. en.wikipedia.org/wiki/tangram

Log in to post a comment.

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

const scale = 150 // min = 1, max = 200, step = 1
const loadConfIgnoreRest = 0 // min = 0, max = 1, step = 1
const bigOneX = 0 //min = -90, max = 90, step = .5
const bigOneY = 0 //min = -90, max = 90, step = .5
const bigOneOrientation = 225 //min = 0, max = 359, step = 1
const bigTwoX = 0 //min = -90, max = 90, step = .5
const bigTwoY = 0 //min = -90, max = 90, step = .5
const bigTwoOrientation = 135 //min = 0, max = 359, step = 1
const squareX = 0 //min = -90, max = 90, step = .5
const squareY = 0 //min = -90, max = 90, step = .5
const squareOrientation = 315 //min = 0, max = 359, step = 1
const littleOneX = 0 //min = -90, max = 90, step = .5
const littleOneY = 0 //min = -90, max = 90, step = .5
const littleOneOrientation = 45 //min = 0, max = 359, step = 1
const littleTwoX = 37.5 //min = -90, max = 90, step = .5
const littleTwoY = -37.5 //min = -90, max = 90, step = .5
const littleTwoOrientation = 315 //min = 0, max = 359, step = 1
const parallelogramX = -75 //min = -90, max = 90, step = .5
const parallelogramY = 75 //min = -90, max = 90, step = .5
const parallelogramOrientation = 0 //min = 0, max = 359, step = 1
const parallelogramMirrored = 0 //min = 0, max = 1, step = 1
const remainingOneX = 75 //min = -90, max = 90, step = .5
const remainingOneY = 75 //min = -90, max = 90, step = .5
const remainingOneOrientation = 180 //min = 0, max = 359, step = 1

const fill = 0 // min = 0, max = 10, step = 1

const root2 = Math.sqrt(2);

class TangramPiece {
    constructor(location, orientation, size) {
        this.l = location;
        this.o = orientation;
        this.s = size;
    }
}

class TangramTriangle extends TangramPiece {
    draw(turtle) {
        for(let ss = this.s; ss > 0; ss-=.2 * fill) {
            turtle.jump(this.l[0], this.l[1]);
            turtle.setheading(this.o);
            turtle.forward(ss);
            turtle.right(135);
            turtle.forward(ss * root2);
            turtle.right(135);
            turtle.forward(ss);
            if(fill == 0) {
                return;
            }
        }
    }
}

class TangramParallelogram extends TangramPiece {
    constructor(location, orientation, size, isMirrored) {
        super(location, orientation, size);
        this.m = isMirrored;
    }
    draw(turtle) {
        for(let ss = this.s; ss > 0; ss-=.2 * fill) {
            turtle.jump(this.l[0], this.l[1]);
            turtle.setheading(this.o);
            turtle.forward(this.s);
            turtle.left(this.m == 1? -45: 45);
            turtle.forward(ss / 2 * root2);
            turtle.left(this.m == 1? -135: 135);
            turtle.forward(this.s);
            turtle.left(this.m == 1? -45: 45);
            turtle.forward(ss / 2 * root2);
            if(fill == 0) {
                return;
            }
        }
    }
}

class TangramSquare extends TangramPiece {
    draw(turtle) {
        for(let ss = this.s; ss > 0; ss-=.2 * fill) {
            turtle.jump(this.l[0], this.l[1]);
            turtle.setheading(this.o);
            for(let i = 0; i < 4; i++) {
                turtle.forward(i % 2 == 0? ss:this.s);
                turtle.right(90);
            }
            if(fill == 0) {
                return;
            }
        }
    }
}

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

// The walk function will be called until it returns false.
function walk(i) {
    let piece = [
        //bigOne
        new TangramTriangle([bigOneX, bigOneY], bigOneOrientation,  root2 * scale / 2),
        //bigTwo
        new TangramTriangle([bigTwoX, bigTwoY], bigTwoOrientation, root2 * scale / 2),
        //square
        new TangramSquare([squareX, squareY], squareOrientation, root2 * scale / 4),
        //littleOne
        new TangramTriangle([littleOneX, littleOneY], littleOneOrientation, root2 * scale / 4),
        //littleTwo
        new TangramTriangle([littleTwoX, littleTwoY], littleTwoOrientation, root2 * scale / 4),
        //parallelogram
        new TangramParallelogram([parallelogramX, parallelogramY], parallelogramOrientation, scale / 2, parallelogramMirrored),
        //remainingOne
        new TangramTriangle([remainingOneX, remainingOneY], remainingOneOrientation, scale / 2),
    ];
    
    if(loadConfIgnoreRest > 0) {
        piece = getConfig(loadConfIgnoreRest);
    }
    
    for(let j = 0; j < piece.length; j++) {
        piece[j].draw(turtle);
    }
    
    return false;
}

function getConfig(cfg) {
    return ([null,
        [
            //bigOne
            new TangramTriangle([0, -90], 0, 90),
            //bigTwo
            new TangramTriangle([0, -90], 90, 90),
            //square
            new TangramSquare([0, 0], 135, 45),
            //littleOne
            new TangramTriangle([90 / root2, 0], 135, 45),
            //littleTwo
            new TangramTriangle([-90 / root2 / 2, 90 / root2 / 2], 45, 45),
            //parallelogram
            new TangramParallelogram([-90 / root2 / 2, 90 / root2 / 2], 0, 90 / root2, 1),
            //remainingOne
            new TangramTriangle([90 / root2 / 2, 90 / root2 / 2], 180, 90 / root2),
        ]
        //todo: add more presets
    ])[cfg];  
}