Newer
Older
fractals / src / Polygon.ts
export class Point
{
    public x:number;
    public y:number;
    constructor(x:number, y:number)
    {
        this.x = x;
        this.y = y;
    }
}

export class Polygon extends Array<Point>
{
    public translate(x:number, y:number): Polygon
    {
        const newCorners = new Polygon();

        for(const corner of this)
        {
            newCorners.push(new Point(corner.x + x, corner.y + y));
        }

        return newCorners;
    }
}

export function inwardSpiral(input:Polygon, scalar:number): Polygon
{
    const output = new Polygon();

    let a:Point;
    let b:Point;
    let cx: number;
    let cy: number;
    for(let i=0;i<input.length;i++)
    {
        a = input[i];
        b = input[(i+1)%input.length];

        cx = scalar * (b.x - a.x) + a.x;
        cy = scalar * (b.y - a.y) + a.y;

        output.push(new Point(cx, cy));
    }

    return output;
}