From e9a68408fc63dfd3a7f279a36a8db477ac928d09 Mon Sep 17 00:00:00 2001 From: Gabriel Csollei Date: Tue, 7 Jan 2025 19:32:10 +0000 Subject: [PATCH] add path-2d --- workspaces/tools/deno.json | 2 +- workspaces/tools/src/index.ts | 1 + workspaces/tools/src/path-2d.ts | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 workspaces/tools/src/path-2d.ts diff --git a/workspaces/tools/deno.json b/workspaces/tools/deno.json index d618d45d..da4c63c7 100644 --- a/workspaces/tools/deno.json +++ b/workspaces/tools/deno.json @@ -1,5 +1,5 @@ { "name": "@g43/tools", - "version": "0.0.1", + "version": "0.0.2", "exports": "./src/index.ts" } diff --git a/workspaces/tools/src/index.ts b/workspaces/tools/src/index.ts index 4eff2e90..ec713ebe 100644 --- a/workspaces/tools/src/index.ts +++ b/workspaces/tools/src/index.ts @@ -1 +1,2 @@ export * from "./g-map.ts"; +export * from "./path-2d.ts"; diff --git a/workspaces/tools/src/path-2d.ts b/workspaces/tools/src/path-2d.ts new file mode 100644 index 00000000..39ec845a --- /dev/null +++ b/workspaces/tools/src/path-2d.ts @@ -0,0 +1,33 @@ +import type { ReadonlySimpleVector2 } from "@g43/types"; + +export class Path2D { + public constructor(protected readonly points: readonly T[]) { + if (points.length < 2) { + throw new Error("Cannot create path with less than 2 points"); + } + } + + public getFirstN(count: number): readonly ReadonlySimpleVector2[] { + return this.points.slice(0, count); + } + + public getLastN(count: number): readonly ReadonlySimpleVector2[] { + return this.points.slice(this.points.length - count, this.points.length); + } + + public get length(): number { + return this.points.length; + } + + public get first(): T { + return this.points[0]; + } + + public get last(): T { + return this.points[this.points.length - 1]; + } + + public getPoint(index: number): T { + return this.points[index]; + } +}