Hexagonadomegaly

It could happen to you.

Log in to post a comment.

Canvas.setpenopacity(-0.5);
const iters = 15000;
const size = 5;

let primes = [3, 5, 7, 11, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199];
const getPrime = () => primes.splice(Math.floor(Math.random()*primes.length), 1)[0];
const a = getPrime();
const b = getPrime();
const extent = 100;

let visited = [];
let free = [];
const visit = (point) => visited.push([...point]) && free.push([...point]);
const alreadyVisited = (point) => visited.find((op) => Math.round(op[0]) === Math.round(point[0]) && Math.round(op[1]) === Math.round(point[1]));

Turtle.prototype.checkBounds = function() {
    this.penup();
    const x = this.x();
    const y = this.y();
    if      (x > extent)  this.setx(-extent+(x-extent));
    else if (x < -extent) this.setx(extent-(-1*(x+extent)));
    if      (y > extent)  this.sety(-extent+(y-extent));
    else if (y < -extent) this.sety(extent-(-1*(y+extent)));
    this.pendown();
}

Turtle.prototype.testPoint = function() {
    const x = this.x();
    const y = this.y();
    return !alreadyVisited([x, y]);
}

Turtle.prototype.freeDir = function(recurse) {
    let dir = 1;
    this.penup();
    do {
        this.right(dir*60);
        this.forward(size);
        point = this.testPoint();
        if (!recurse && !this.freeDir(true)) {
            this.pendown();
        }
        this.backward(size);
        this.penup();
        this.left(dir*60);
        dir++;
    } while (!point && dir <= 6)
    return point ? dir*60 : false;
}

const turtle = new Turtle();
turtle.penup();
turtle.goto(0, 0);
turtle.pendown();

function walk(i) {
    turtle.penup();
    const ia = a+Math.floor(i/a);
    const ib = b+Math.floor(i/b);
    if (i%ia==0) turtle.left(60);
    if (i%ib==0) turtle.right(60);
    if ((i%ia==0 && i%ib==0) || (i%a==0 && i%b==0)) turtle.right(60);
    else if ((i%ia && i%ib) && (i%a && i%b)) turtle.left(60);
    turtle.forward(size);
    turtle.checkBounds();
    const x = turtle.x();
    const y = turtle.y();
    if (!alreadyVisited([x, y])) {
        turtle.backward(size);
        turtle.pendown();
        turtle.forward(size);
        visit([x, y]);
    } else {
        for (let p = 1; p < free.length-1; p++) {
            const last = free[(free.length-p-1)%free.length-1];
            turtle.penup();
            turtle.goto([...last]);
            const dir = turtle.freeDir();
            if (dir !== false) {
                turtle.right(dir);
                turtle.pendown();
                turtle.forward(size);
                break;
            } else {
                free = free.splice(free.length-p-1, 1);
            }
        }
    }
    return i < iters;
}