Newer
Older
fractals / src / BaseShapes.ts
import { Polygon, Point } from "./Polygon";
import { zip } from "./utils";

export function createHexagonalBase(sideLength: number): Polygon[] {
    const max = sideLength - 1;
    const center = new Point(max / 2, max / 2);
    const hexagon = createHexagonInSquare(max);
    const lines = zip(hexagon, hexagon, 1);
    const hexagonTriangles = lines.map(([a, b]) => [a, b, center]);

    const corner1 = new Point(max, 0);
    const corner2 = new Point(0, 0);
    const corner3 = new Point(0, max);
    const corner4 = new Point(max, max);
    const cornerTriangles = [
        [hexagon[0], corner1, hexagon[1]],
        [hexagon[2], corner2, hexagon[3]],
        [hexagon[3], corner3, hexagon[4]],
        [hexagon[5], corner4, hexagon[0]],
    ];
    return [
        cornerTriangles[0],
        hexagonTriangles[0],
        hexagonTriangles[1],
        hexagonTriangles[2],
        cornerTriangles[1],
        cornerTriangles[2],
        hexagonTriangles[3],
        hexagonTriangles[4],
        hexagonTriangles[5],
        cornerTriangles[3],
    ];
}

export function createSquare(sideLength:number): Polygon
{
    const max = sideLength - 1;
    const square:Polygon = [];
    square.push(new Point(0,0));
    square.push(new Point(0,max));
    square.push(new Point(max,max));
    square.push(new Point(max,0));
    return square;
}

function createHexagonInSquare(sideLength:number): Polygon
{
    const max = sideLength - 1;
    return [
        new Point(max, max/2),
        new Point(3 * max / 4, 0),
        new Point(max / 4, 0),
        new Point(0, max / 2),
        new Point(max/4, max),
        new Point(3*max/4, max),
    ];
}

export function createTriangle(sideLength:number): Polygon
{
    return [
        new Point(sideLength/2, 0),
        new Point(0,sideLength),
        new Point(sideLength, sideLength),
    ];
}