Skip to content

Commit

Permalink
add path-2d
Browse files Browse the repository at this point in the history
  • Loading branch information
G43riko committed Jan 7, 2025
1 parent 35f8ec0 commit e9a6840
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion workspaces/tools/deno.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@g43/tools",
"version": "0.0.1",
"version": "0.0.2",
"exports": "./src/index.ts"
}
1 change: 1 addition & 0 deletions workspaces/tools/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./g-map.ts";
export * from "./path-2d.ts";
33 changes: 33 additions & 0 deletions workspaces/tools/src/path-2d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type { ReadonlySimpleVector2 } from "@g43/types";

export class Path2D<T extends ReadonlySimpleVector2> {
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];
}
}

0 comments on commit e9a6840

Please sign in to comment.