From 71e55dba9f11414fdd60634e0c62931526a5268f Mon Sep 17 00:00:00 2001 From: sunub Date: Thu, 17 Apr 2025 11:09:44 +0900 Subject: [PATCH] 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. --- packages/math/src/types.ts | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/packages/math/src/types.ts b/packages/math/src/types.ts index a2a575bd7..c0b425055 100644 --- a/packages/math/src/types.ts +++ b/packages/math/src/types.ts @@ -27,6 +27,12 @@ export type InclusiveRange = [number, number] & { _brand: "excalimath_degree" }; // 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 * global coordinate. @@ -35,6 +41,14 @@ export type GlobalPoint = [x: number, y: number] & { _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 * 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. */ -export type Line

= [p: P, q: P] & { +export type Line

= [p: P, q: P] & { _brand: "excalimath_line"; }; @@ -57,7 +71,7 @@ export type Line

= [p: P, q: P] & { * line that is bounded by two distinct end points, and * contains every point on the line that is between its endpoints. */ -export type LineSegment

= [a: P, b: P] & { +export type LineSegment

= [a: P, b: P] & { _brand: "excalimath_linesegment"; }; @@ -77,18 +91,14 @@ export type Vector = [u: number, v: number] & { /** * A triangle represented by 3 points */ -export type Triangle

= [ - a: P, - b: P, - c: P, -] & { +export type Triangle

= [a: P, b: P, c: P] & { _brand: "excalimath__triangle"; }; /** * A rectangular shape represented by 4 points at its corners */ -export type Rectangle

= [a: P, b: P] & { +export type Rectangle

= [a: P, b: P] & { _brand: "excalimath__rectangle"; }; @@ -100,7 +110,7 @@ export type Rectangle

= [a: P, b: P] & { * A polygon is a closed shape by connecting the given points * rectangles and diamonds are modelled by polygons */ -export type Polygon = Point[] & { +export type Polygon = Point[] & { _brand: "excalimath_polygon"; }; @@ -111,12 +121,7 @@ export type Polygon = Point[] & { /** * Cubic bezier curve with four control points */ -export type Curve = [ - Point, - Point, - Point, - Point, -] & { +export type Curve = [Point, Point, Point, Point] & { _brand: "excalimath_curve"; }; @@ -131,7 +136,7 @@ export type PolarCoords = [ but for the sake of simplicity, we've used halfWidth and halfHeight instead in replace of semi major and semi minor axes */ -export type Ellipse = { +export type Ellipse = { center: Point; halfWidth: number; halfHeight: number;