Alien art revisited

A look back at Alien art: (x ^ y) % 9 with an updated formula and a much bigger playfield.

Log in to post a comment.

// LL 2021

const power = 12; // min=6 max=14 step=1
const mod = 62; // min=1 max=1000 step=1
const result=0; // min=0 max=50 step=1

const style = 3; // min=1 max=3 step=1 (Dots,Lines,Both)
const rotate = 1; // min=0 max=1 step=1 (No,Yes)

const grid = 2 ** power;

Canvas.setpenopacity(1);
const turtle = new Turtle();

function walk(i, t) {
    const x = i % grid;
    const y = (i / grid) | 0;
    const r = 100 / grid;
    
    if (getValue(x, y)) {
        const xy = getXY(x, y);
        if (Math.abs(xy.x) <= 100 && Math.abs(xy.y) <= 100) {
            if (style & 2) {
                for (var dy=-1; dy<=1; dy++) {
                    for (var dx=-1; dx<=1; dx++) {
                        if (!dx && !dy) continue;
                        if (x+dx >= 0 && x+dx < grid && y+dy >= 0 && y+dy < grid) {
                            if (getValue(x+dx, y+dy)) {
                                const xy2 = getXY(x+dx, y+dy);
                                turtle.jump(xy.x, xy.y);
                                turtle.goto(xy2.x, xy2.y);
                            }
                        }
                    }
                }
            }

            if (style & 1) {    
                const steps = 4;
                turtle.up();
                for (var j=0; j<=steps; j++) {
                    const cx = xy.x + r * Math.cos(Math.PI*2 * j / steps + i * Math.PI/2);
                    const cy = xy.y + r * Math.sin(Math.PI*2 * j / steps + i * Math.PI/2);
                    turtle.goto(cx, cy);
                    turtle.down();
                }
            }
        }
    }

    return i < grid * grid;
}

function getValue(x, y) {
    return (((x*mod) ^ (y*mod)) % mod) == (result % mod);
}

function getXY(x, y) {
    const size = rotate ? 284 : 200;
    const px = (x / grid) * size - size / 2;
    const py = (y / grid) * size - size / 2;
    const rx = rotX(px, py, rotate * Math.PI/4);
    const ry = rotY(px, py, rotate * Math.PI/4);
    return { x:rx, y:ry };
}

function rotX(x, y, a) { return Math.cos(a) * x - Math.sin(a) * y; }
function rotY(x, y, a) { return Math.sin(a) * x + Math.cos(a) * y; }