arcade2d
Class

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

#
constructor(initialX: number, initialY: number): Point

Creates a new point. Any non-finite input (NaN, Infinity, -Infinity) is rejected and replaced with 0.

Parameters

  • initialX number
  • initialY number

Accessors

readonly #
angle: number

Calculates the angle of this point in radians, measured from 0,0 as atan2(y, x). The result is in the range (-π, π].

readonly #
length: number

Calculates the length (magnitude) of this point measured as a vector from 0,0.

readonly #
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.

#
x: number

The x coordinate of this point.

#
y: number

The y coordinate of this point.

Methods

#
add(x: number, y: number): this

Adds the input values to this point's coordinates.

Parameters

  • x number
  • y number

Returns

this

This point, for chaining.

add(point: PointPrimitive): this

Adds another point's coordinates to this point's coordinates.

Parameters

Returns

this

This point, for chaining.

#
angleTo(target: PointPrimitive): number

Calculate the angle from this point to a target point.

Parameters

Returns

number
#
clone(): Point

Returns a clone of this point. Useful for producing a new point with displaced coordinates e.g.

const newPosition = originalPosition.clone().moveTowards(target, 10);

Returns

Point
#
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
#
cloneToNormalized(): Point

Produces a new point with normalized x and y values (a unit vector pointing in the same direction). Normalizing a zero-length point yields a new 0,0 point.

Returns

Point
#
cloneToPrimitive(): PointPrimitive

Returns a primitive clone of this point.

Returns

PointPrimitive
#
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

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

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

Returns

number
#
distanceTo(target: PointPrimitive): number

Calculate the distance from this point to a target point.

Parameters

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

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

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

  • distance number

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

Returns

this

This point, for chaining.

#
moveInDirection(angle: number, distance: number): this

Move this point in a given direction by a given distance.

Parameters

  • angle number
  • distance number

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

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

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

  • x number
  • y number

Returns

this

This point, for chaining.

#
set(x: number, y: number): this

Sets the coordinates of this point.

Parameters

  • x number
  • y number

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

  • x number
  • y number

Returns

this

This point, for chaining.

#
subtract(x: number, y: number): this

Subtracts the input values from this point's coordinates.

Parameters

  • x number
  • y number

Returns

this

This point, for chaining.

subtract(point: PointPrimitive): this

Subtracts another point's coordinates from this point's coordinates.

Parameters

Returns

this

This point, for chaining.

#
toString(): string

Returns a string representation of this point.

Returns

string
static #
angular(radians: number): Point

Creates a new point where the x and y values indicate a direction specified by input radians, starting from 1,0.

Parameters

  • radians number

Returns

Point
static #
down(): Point

Creates a new point of 0,1, representing a "downward facing" magnitude.

Returns

Point
static #
from(value: PointPrimitive | readonly [number, number]): Point

Creates a new point from an existing point primitive or an [x, y] tuple.

Parameters

Returns

Point
static #
left(): Point

Creates a new point of -1,0, representing a "left facing" magnitude.

Returns

Point
static #
up(): Point

Creates a new point of 0,-1, representing an "upward facing" magnitude.

Returns

Point
static #
zero(): Point

Creates a new point of 0,0.

Returns

Point
ESC