Point
geometry/point.ts:20 Implements PointPrimitive
Defines a point in 2D space. Provides functionality for common operations between points and the parent space such as calculating distances and angles, and performing vector operations.
A Point also doubles as the engine's 2D vector type: methods like
Point.length, Point.angle, Point.normalize,
Point.dot and Point.cross treat the point as a vector
measured from the origin 0,0.
The engine uses screen-space coordinates: x increases to the right and y
increases downward. This is why Point.up is 0,-1 and
Point.down is 0,1.
Mutating methods modify this point in place and return this, so calls can
be chained. Methods prefixed with clone never mutate this point.
Constructors
Accessors
angle: number Calculates the angle of this point in radians, measured from 0,0 as
atan2(y, x). The result is in the range (-π, π].
length: number Calculates the length (magnitude) of this point measured as a vector from
0,0.
lengthSquared: number Calculates the squared length of this point measured from 0,0. Cheaper
than Point.length (no square root) and sufficient when only
comparing magnitudes.
Methods
add(x: number, y: number): this Adds the input values to this point's coordinates.
Parameters
-
xnumber -
ynumber
Returns
this This point, for chaining.
add(point: PointPrimitive): this Adds another point's coordinates to this point's coordinates.
Parameters
-
pointPointPrimitive
Returns
this This point, for chaining.
angleTo(target: PointPrimitive): number Calculate the angle from this point to a target point.
Parameters
-
targetPointPrimitive
Returns
number cloneToImmutablePrimitive(): Readonly Returns an immutable primitive clone of this point, where the x and y
values are frozen at runtime and marked readonly by the compiler.
Returns
Readonly cloneToTuple(): [number, number] Returns a tuple clone of this point expressed as [x, y].
Returns
[number, number] copyFrom(source: PointPrimitive): this Copies the coordinates from another point into this one.
Parameters
-
sourcePointPrimitive
Returns
this This point, for chaining.
cross(target: PointPrimitive): number Calculate the 2D cross product (the z-component of the 3D cross product)
between this point and a target point, treating both as vectors from the
origin 0,0. The sign indicates winding direction.
Parameters
-
targetPointPrimitive
Returns
number distanceSquaredTo(target: PointPrimitive): number Calculate the squared distance from this point to a target point. Cheaper than Point.distanceTo (no square root) and sufficient when only comparing distances.
Parameters
-
targetPointPrimitive
Returns
number distanceTo(target: PointPrimitive): number Calculate the distance from this point to a target point.
Parameters
-
targetPointPrimitive
Returns
number dot(target: PointPrimitive): number Calculate the dot product between this point and a target point, treating
both as vectors from the origin 0,0.
Parameters
-
targetPointPrimitive
Returns
number equals(target: PointPrimitive, precision: number): boolean Determines if this point is equal to a target point within a given
precision. The comparison is per-axis (Chebyshev): the point is considered
equal when both the x and y differences are within precision, rather
than comparing Euclidean distance.
Note the default precision of 1 is deliberately loose — suited to
coarse "close enough" gameplay checks. Pass 0 for an exact comparison.
Parameters
-
targetPointPrimitive -
precisionnumber
Returns
boolean forward(distance: number): this Move this point forward by a given distance along Point.angle —
i.e. along the ray from the origin 0,0 through this point, treating the
point as a vector.
Parameters
-
distancenumber
Returns
this This point, for chaining.
lerp(target: PointPrimitive, t: number): this Linearly interpolates this point toward a target point by a fraction t,
where 0 leaves this point unchanged and 1 moves it onto the target.
Values outside 0..1 extrapolate.
Parameters
-
targetPointPrimitive -
tnumber
Returns
this This point, for chaining.
moveInDirection(angle: number, distance: number): this Move this point in a given direction by a given distance.
Parameters
-
anglenumber -
distancenumber
Returns
this This point, for chaining.
moveTowards(target: PointPrimitive, distance: number): this Move this point toward a target point by a given distance. The point will
not overshoot: if the target is nearer than distance, this point lands
exactly on the target.
Parameters
-
targetPointPrimitive -
distancenumber
Returns
this This point, for chaining.
negate(): this Negates this point's coordinates, producing the opposing vector.
Returns
this This point, for chaining.
normalize(): this Normalizes this point in place so it becomes a unit vector pointing in the
same direction. Normalizing a zero-length point leaves it at 0,0.
Returns
this This point, for chaining.
rotate(radians: number, origin?: PointPrimitive): this Rotates this point around an origin by the given angle.
Parameters
-
radiansnumber -
origin?PointPrimitive
Returns
this This point, for chaining.
scale(x: number, y: number): this Scales this point's coordinates by the given factors. A single argument scales both axes uniformly.
Parameters
-
xnumber -
ynumber
Returns
this This point, for chaining.
set(x: number, y: number): this Sets the coordinates of this point.
Parameters
-
xnumber -
ynumber
Returns
this This point, for chaining.
snap(x: number, y: number): this Updates the coordinates of this point so the position is "snapped" to the
nearest cell corner in an abstract grid as defined by the input x and
y values. A spacing of 0 on an axis collapses that axis to 0.
Parameters
-
xnumber -
ynumber
Returns
this This point, for chaining.
subtract(x: number, y: number): this Subtracts the input values from this point's coordinates.
Parameters
-
xnumber -
ynumber
Returns
this This point, for chaining.
subtract(point: PointPrimitive): this Subtracts another point's coordinates from this point's coordinates.
Parameters
-
pointPointPrimitive
Returns
this This point, for chaining.
toString(): string Returns a string representation of this point.
Returns
string from(value: PointPrimitive | readonly [number, number]): Point Creates a new point from an existing point primitive or an [x, y] tuple.
Parameters
-
valuePointPrimitive | readonly [number, number]
Returns
Point