feat: Add Viewport Point and create a unified Generic Point

- Viewport Point was created using the same logic as existing Point types.
- Defined a Generic Point by combining GlobalPoint, LocalPoint, and ViewportPoint into a union type.
This commit is contained in:
sunub 2025-04-17 11:09:44 +09:00
parent dff69e9191
commit 71e55dba9f

View file

@ -27,6 +27,12 @@ export type InclusiveRange = [number, number] & { _brand: "excalimath_degree" };
// Point // Point
// //
/**
* Represents a 2D position in world or canvas space. A
* unintified glboal, viewport, or local coordinate.
*/
export type GenericPoint = GlobalPoint | ViewportPoint | LocalPoint;
/** /**
* Represents a 2D position in world or canvas space. A * Represents a 2D position in world or canvas space. A
* global coordinate. * global coordinate.
@ -35,6 +41,14 @@ export type GlobalPoint = [x: number, y: number] & {
_brand: "excalimath__globalpoint"; _brand: "excalimath__globalpoint";
}; };
/**
* Represents a 2D position in world or viewport space. A
* viewport coordinate.
*/
export type ViewportPoint = [x: number, y: number] & {
_brand: "excalimath__viewportpoint";
};
/** /**
* Represents a 2D position in whatever local space it's * Represents a 2D position in whatever local space it's
* needed. A local coordinate. * needed. A local coordinate.
@ -48,7 +62,7 @@ export type LocalPoint = [x: number, y: number] & {
/** /**
* A line is an infinitely long object with no width, depth, or curvature. * A line is an infinitely long object with no width, depth, or curvature.
*/ */
export type Line<P extends GlobalPoint | LocalPoint> = [p: P, q: P] & { export type Line<P extends GenericPoint> = [p: P, q: P] & {
_brand: "excalimath_line"; _brand: "excalimath_line";
}; };
@ -57,7 +71,7 @@ export type Line<P extends GlobalPoint | LocalPoint> = [p: P, q: P] & {
* line that is bounded by two distinct end points, and * line that is bounded by two distinct end points, and
* contains every point on the line that is between its endpoints. * contains every point on the line that is between its endpoints.
*/ */
export type LineSegment<P extends GlobalPoint | LocalPoint> = [a: P, b: P] & { export type LineSegment<P extends GenericPoint> = [a: P, b: P] & {
_brand: "excalimath_linesegment"; _brand: "excalimath_linesegment";
}; };
@ -77,18 +91,14 @@ export type Vector = [u: number, v: number] & {
/** /**
* A triangle represented by 3 points * A triangle represented by 3 points
*/ */
export type Triangle<P extends GlobalPoint | LocalPoint> = [ export type Triangle<P extends GenericPoint> = [a: P, b: P, c: P] & {
a: P,
b: P,
c: P,
] & {
_brand: "excalimath__triangle"; _brand: "excalimath__triangle";
}; };
/** /**
* A rectangular shape represented by 4 points at its corners * A rectangular shape represented by 4 points at its corners
*/ */
export type Rectangle<P extends GlobalPoint | LocalPoint> = [a: P, b: P] & { export type Rectangle<P extends GenericPoint> = [a: P, b: P] & {
_brand: "excalimath__rectangle"; _brand: "excalimath__rectangle";
}; };
@ -100,7 +110,7 @@ export type Rectangle<P extends GlobalPoint | LocalPoint> = [a: P, b: P] & {
* A polygon is a closed shape by connecting the given points * A polygon is a closed shape by connecting the given points
* rectangles and diamonds are modelled by polygons * rectangles and diamonds are modelled by polygons
*/ */
export type Polygon<Point extends GlobalPoint | LocalPoint> = Point[] & { export type Polygon<Point extends GenericPoint> = Point[] & {
_brand: "excalimath_polygon"; _brand: "excalimath_polygon";
}; };
@ -111,12 +121,7 @@ export type Polygon<Point extends GlobalPoint | LocalPoint> = Point[] & {
/** /**
* Cubic bezier curve with four control points * Cubic bezier curve with four control points
*/ */
export type Curve<Point extends GlobalPoint | LocalPoint> = [ export type Curve<Point extends GenericPoint> = [Point, Point, Point, Point] & {
Point,
Point,
Point,
Point,
] & {
_brand: "excalimath_curve"; _brand: "excalimath_curve";
}; };
@ -131,7 +136,7 @@ export type PolarCoords = [
but for the sake of simplicity, we've used halfWidth and halfHeight instead but for the sake of simplicity, we've used halfWidth and halfHeight instead
in replace of semi major and semi minor axes in replace of semi major and semi minor axes
*/ */
export type Ellipse<Point extends GlobalPoint | LocalPoint> = { export type Ellipse<Point extends GenericPoint> = {
center: Point; center: Point;
halfWidth: number; halfWidth: number;
halfHeight: number; halfHeight: number;