Hexagon Tiling

Can you find them all?

Log in to post a comment.

    class Point {
        constructor(x, y) {
            this.x = x;
            this.y = y;
        }

        translate(dx, dy) {
            return new Point(this.x + dx, this.y + dy);
        }

        rotateAboutOrigin(theta) {
            let thetaRadians = theta * Math.PI / 180;
            let newX = this.x * Math.cos(thetaRadians) - this.y * Math.sin(thetaRadians);
            let newY = this.y * Math.cos(thetaRadians) + this.x * Math.sin(thetaRadians);
            return new Point(newX, newY);
        }

        scale(n) {
            return new Point(n * this.x, n * this.y);
        }

        eq(other) {
            return ((this.x == other.x) && (this.y == other.y));
        }

        toArray() {
            return [this.x, this.y];
        }

        toString() {
            return `Point(${this.x}, ${this.y})`;
        }
    }


    class LineSegment {
        constructor(p1, p2) {
            this.p1 = p1;
            this.p2 = p2;
        }

        translate(dx, dy) {
            return new LineSegment(this.p1.translate(dx, dy), this.p2.translate(dx, dy));
        }

        rotateAboutOrigin(theta) {
            return new LineSegment(this.p1.rotateAboutOrigin(theta), this.p2.rotateAboutOrigin(theta));
        }

        scale(n) {
            return new LineSegment(this.p1.scale(n), this.p2.scale(n));
        }

        eq(other) {
            if ((this.p1.eq(other.p1)) && (this.p2.eq(other.p2))) { return true; }
            if ((this.p1.eq(other.p2)) && (this.p2.eq(other.p1))) { return true; }
            return false;
        }

        draw() {
            turtle.goto(this.p1.toArray());
            turtle.pendown();
            turtle.goto(this.p2.toArray());
            turtle.penup();
        }

        toString() {
            return `LineSegment(${this.p1.toString()}, ${this.p2.toString()})`;
        }
    }


    class Polygon {
        constructor(edges) {
            this.edges = edges;
        }

        translate(dx, dy) {
            return new Polygon(this.edges.map(edge => edge.translate(dx, dy)));
        }

        rotateAboutOrigin(theta) {
            return new Polygon(this.edges.map(edge => edge.rotateAboutOrigin(theta)));
        }

        scale(n) {
            return new Polygon(this.edges.map(edge => edge.scale(n)));
        }

        draw() {
            this.edges.map(edge => edge.draw());
        }

        toString() {
            return `Polygon[${this.edges.map(edge => edge.toString())}]`;
        }
    }


    function makePolygonFromVertices(vertices) {
        let edges = [];

        for (var i = 0; i < vertices.length - 1; i++) {
            edges.push(new LineSegment(vertices[i], vertices[i+1]));
        }

        edges.push(new LineSegment(vertices[vertices.length - 1], vertices[0]));
        return new Polygon(edges);
    }

    // Parameters
    Canvas.setpenopacity(1);
    const step = (Math.sqrt(3) - 1) * Math.sqrt(2);
    const scale = 1;


    const turtle = new Turtle();
    turtle.penup();
    const rt2 = Math.sqrt(2);
    const rt3 = Math.sqrt(3);


    function makeStar() {
        let vertices = [
            new Point(rt3 + 1, rt3 - 1),
            new Point(rt3, 1),
            new Point(2, 2),
            new Point(1, rt3),
            new Point(rt3 - 1, rt3 + 1),
            new Point(0, 2),
            new Point(1 - rt3, rt3 + 1),
            new Point(-1, rt3),
            new Point(-2, 2),
            new Point(-rt3, 1),
            new Point(-rt3 - 1, rt3 - 1),
            new Point(-2, 0),
            new Point(-rt3 - 1, 1 - rt3,),
            new Point(-rt3, -1),
            new Point(-2, -2),
            new Point(-1, -rt3),
            new Point(1 - rt3, -rt3 - 1),
            new Point(0, -2),
            new Point(rt3 - 1, -rt3 - 1),
            new Point(1, -rt3),
            new Point(2, -2),
            new Point(rt3, -1),
            new Point(1 + rt3, 1 - rt3),
            new Point(2, 0)
        ];

        return makePolygonFromVertices(vertices);
    }

    function makePoint() {
        let vertices = [
            new Point(1 - rt3, 1 + rt3),
            new Point(0, 2 + 2 * rt3),
            new Point(rt3 - 1, 1 + rt3),
            new Point(0, 2)
        ];

        return makePolygonFromVertices(vertices);
    }

    function makeWedge() {
        let vertices = [
            new Point(rt2, (rt2 / (2 - rt3)) + (4 / (1 + rt3))),
            new Point(rt2, rt2 / (2 - rt3)),
            new Point(0, 2 * rt2),
            new Point(-rt2, rt2 / (2 - rt3)),
            new Point(-rt2, (rt2 / (2 - rt3)) + (4 / (1 + rt3))),
            new Point(0, (rt2 / (2 - rt3)) + (4 / (1 + rt3)) + (2 - rt3)*rt2)
        ];

        return makePolygonFromVertices(vertices);
    }

    function makeTip() {
        let vertices = [
            new Point(2 + 2 * rt3, 0),
            new Point(2 + rt2 + 2 * rt3, (2 - rt3) * rt2),
            new Point(2 + rt2 + 2 * rt3, ((2 - rt3) * rt2) + (4 / (1 + rt3))),
            new Point(2 * (2 + rt2 + 2 * rt3) * rt3 / 3, 0),
            new Point(2 + rt2 + 2 * rt3, -((2 - rt3) * rt2) - (4 / (1 + rt3))),
            new Point(2 + rt2 + 2 * rt3, -(2 - rt3) * rt2),
        ];

        return makePolygonFromVertices(vertices);
    }

    function makeHex() {
        let p = new Point(0, (2 + rt2 + 2 * rt3) * (((2 * rt3) - 3) / 3) * (2 * rt3 / 3));
        let vertices = [];

        for (var i = 0; i < 6; i++) {
            vertices.push(p.rotateAboutOrigin(i * 60));
        }

        return makePolygonFromVertices(vertices);
    }

    function walk(i) {
        let hexTile = [];
        let d = (2 + rt2 + 2 * rt3) * scale * 2 * 2 / rt3;
        let e = (2 + rt2 + 2 * rt3) * (((2 * rt3) - 3) / 3) * scale;

        let star = makeStar().scale(scale);
        hexTile.push(star);

        for (var i = 0; i < 12; i++) {
            let point = makePoint().rotateAboutOrigin(30 * i).scale(scale);
            hexTile.push(point);
        }    

        for (var i = 0; i < 12; i++) {
            let wedge = makeWedge().rotateAboutOrigin(30 * i - 15).scale(scale);
            hexTile.push(wedge);
        }

        for (var i = 0; i < 12; i++) {
            let tip = makeTip().rotateAboutOrigin(30 * i).scale(scale);
            hexTile.push(tip);
        }

        let miniHex = makeHex().scale(scale);
        hexTile.push(miniHex.translate(0, d/2 + e));
        hexTile.push(miniHex.translate(0, -d/2 - e));

        for (var i = -7; i <= 6; i++) {
            for (var j = -4; j <= 3; j++) {
                hexTile.map(poly => poly.translate(d * i, d * rt3 * j).draw());
                hexTile.map(poly => poly.translate(d * i, d * rt3 * j).translate(d/2, d*rt3/2).draw());
            }
        }

        return false;
    }