Represents a point in two-dimensional Cartesian coordinates.
ImplementsInheritance Hierarchy

Remarks

In addition to being used to model points Point also includes a number of useful properties and methods that allow it to be treated as a vector.

Furthermore, Point provides various methods for analyzing and manipulating geometry in general, such as:

This is a convertible type that can be used with the following notation(s) in parameter lists, parameter objects or setters.

It is possible to specify an Array or plain Object to define the Point:

[5, 5] // x, y
{ x: 5, y: 5 }

Examples

The following example shows a few ways to use Point:
// Create a new point
const p1 = new Point(-5, 2)
// Create another point somewhere else
const p2 = new Point(23, 42)

console.log(
  `Point 1 is at (${p1.x},${p1.y}), point 2 is at (${p2.x},${p2.y})`,
)

// Subtract one point from the other to get a vector between them
// which is also represented as a Point
const v = p2.subtract(p1)

// Point offers a few handy properties when interpreting it as a vector
console.log(`VectorLength: ${v.vectorLength}`)
// VectorLength in this case is equivalent to p1.distanceTo(p2):
console.log(`Distance: ${p1.distanceTo(p2)}`)

console.log(`IsHorizontalVector: ${v.isHorizontalVector}`)
console.log(`IsVerticalVector: ${v.isVerticalVector}`)
console.log(`Normalized: ${v.normalized}`)

const halfLength = v.multiply(0.2).vectorLength
const doubleLength = v.multiply(2).vectorLength

console.log(`Original vector: ${v.vectorLength}`)
console.log(`Half-length vector: ${halfLength}`)
console.log(`Double-length vector: ${doubleLength}`)

See Also

Developer's Guide

Members

Show:

Constructors

Creates a new instance using the given values for the x and y properties.

Parameters

x: number
The x-coordinate.
y: number
The y-coordinate.

Properties

Gets whether this instance is a horizontally oriented vector.
Horizontal in this case does not mean exactly horizontal, but rather that the horizontal component is larger than the vertical one, i.e. the vector is more horizontal than vertical.
readonlyfinal

Property Value

true iff Math.Abs(X) > Math.Abs(Y).
Gets whether this instance is a vertically oriented vector.
Vertical in this case does not mean exactly vertical, but rather that the vertical component is larger than the horizontal one, i.e. the vector is more vertical than horizontal.
readonlyfinal

Property Value

true iff Math.Abs(Y) > Math.Abs(X).
Creates a normalized version of this vector.
The vector is normalized by dividing both x and y by its length. The following image illustrates a few vectors and their respective normalized vectors: As can be seen, the direction remains the same, but the length is normalized to 1.
readonlyfinal

Property Value

The normalized version of this vector or (1,0) if this vector has zero length.
Gets the squared length of the vector that has x and y as its components.
readonlyfinal

Property Value

The squared length of the vector which is X*X + Y*Y.

See Also

API
vectorLength
Gets the length of the vector that has x and y as its components.
If the vector length should be compared to another length, e.g. to determine whether the point is close to something else, it's faster to compare squaredVectorLength with the squared other length, as this avoids a costly square root operation.
readonlyfinal

Property Value

The length of the vector which is the square root of X*X + Y*Y.

See Also

API
squaredVectorLength
Gets the x-coordinate of the point.
readonlyfinal

Property Value

The x-coordinate.

Implements

IPoint.x
Gets the y-coordinate of the point.
readonlyfinal

Property Value

The y-coordinate.

Implements

IPoint.y

Methods

Adds the given vector to this point.
This operator is applied componentwise to x and y.
final

Parameters

point: Point
The vector.

Return Value

Point
The result of the vector addition.

Examples

Using the + and - operators
const p1 = new Point(2, 5)
const p2 = new Point(17, -6)

// Translate p1 by the vector p2
const p3 = p1.add(p2)
console.log(`p1 + p2 = ${p3}`) // (19, -1)

// Subtract p1 from p3 to get p2 again
const p4 = p3.subtract(p1)
console.log(p2.equals(p4)) // true

// Reverse the direction of p2
const reversed = p2.multiply(-1)
console.log(`p2 reversed: ${reversed}`) // (-17, 6)
Comparable implementation.
Points are ordered by ascending x-coordinates. If the x-coordinates of two points are equal, then these points are ordered by ascending y-coordinates.
final

Parameters

o: any
The object to compare with the current instance.

Return Value

number
A value that indicates the relative order of the objects being compared. The return value has these meanings:
  • -1 if this object's x- or y-coordinate is less than the compared object's x- or y-coordinate.
  • 1 if this object's x- or y-coordinate is greater than the compared object's x- or y-coordinate.
  • 0 if both the x- and y-coordinates are equal.
Calculates the Euclidean distance between two points.

Parameters

point2: IPoint
The second point.

Return Value

number
The distance between the two points.
Returns the euclidean distance between this point and a given point.

Parameters

x: number
the x coordinate of the second point
y: number
the y coordinate of the second point

Return Value

number
the Euclidean distance between this point and the point (x,y).
Determines the distance between this point and a line segment.
final

Parameters

start: Point
The coordinates of the first point of the line.
end: Point
The coordinates of the second point of the line.

Return Value

number
The distance between this point and the closest point on the line segment.
Indicates whether this instance and a specified object are equal.
final

Parameters

other: any
Another object to compare to.

Return Value

boolean
true if other and this instance are the same type and represent the same value; otherwise, false.

Examples

Determining whether two points are equal
const p1 = new Point(2, 5)
const p2 = new Point(23, 52)
const p3 = new Point(2, 5)

// You can call the equals method to determine equality of two points
console.log(p1.equals(p2)) // false
console.log(p1.equals(p3)) // true

// In case you want to determine whether two Point instances are approximately equal
// you can use equalsEps

const almostP1 = new Point(2.000001, 4.99999)
console.log(p1.equalsEps(almostP1, 1e-4)) // true
console.log(p1.equalsEps(almostP1, 1e-8)) // false
Determines whether the two given points have the same coordinates with respect to a certain given eps.
final

Parameters

other: Point
The other point to check for equality against this point.
eps: number
A positive value allows for fuzzy equality testing. If the other point's coordinates are within epsilon of this point's coordinates, the points are considered equal.

Return Value

boolean
Whether both coordinates are equal with respect to the given epsilon.

Throws

Exception ({ name: 'ArgumentError' })
Thrown if the value of the given epsilon is negative.

Examples

Determining whether two points are equal
const p1 = new Point(2, 5)
const p2 = new Point(23, 52)
const p3 = new Point(2, 5)

// You can call the equals method to determine equality of two points
console.log(p1.equals(p2)) // false
console.log(p1.equals(p3)) // true

// In case you want to determine whether two Point instances are approximately equal
// you can use equalsEps

const almostP1 = new Point(2.000001, 4.99999)
console.log(p1.equalsEps(almostP1, 1e-4)) // true
console.log(p1.equalsEps(almostP1, 1e-8)) // false
Create a constrained copy of this instance that lies within the given non-empty rectangle.

If the point lies outside the rectangle, its projection on the closest border will be returned.

If the given rectangle is EMPTY, the return value is unchanged.

final

Parameters

rectangle: Rect
The rectangle to constrain this instance by.

Return Value

Point
A constrained copy of this instance.
Calculates the projection of this point onto a line.
final

Parameters

anchor: Point
An anchor for the infinite line.
direction: Point
A direction vector for the infinite line.

Return Value

Point
The point on the line that is closest to this point.
Calculates the projection of this point onto a ray.
If the perpendicular projection onto the line is outside of the ray ("behind" the rayStart), the rayStart is returned instead.
final

Parameters

rayStart: Point
The start of the segment.
direction: Point
The direction of the ray.

Return Value

Point
The point on the ray that is closest to this point.
Calculates the projection of this point onto a segment.
If the perpendicular projection onto the line is outside of the segment, the nearest segment endpoint is returned.
final

Parameters

start: Point
The start of the segment.
end: Point
The end of the segment.

Return Value

Point
The point on the segment that is closest to this point.
Returns the hash code for this instance that is calculated using the x and y values.
final

Return Value

number
A 32-bit signed integer that is the hash code for this instance.
Determines if the point lies close to this point given an epsilon.
final

Parameters

other: Point
The coordinates of the other point.
hitTestRadius: number
The hit test epsilon.

Return Value

boolean
Whether the distance between the two points is smaller than hitTestRadius.
Determines whether this point hits the line segment with respect to a given radius.
final

Parameters

start: Point
The starting point of the line segment to test.
end: Point
The ending point of the line segment to test.
radius: number
The hit test radius.

Return Value

boolean
Whether this point hits the given line segment within the radius.
Determines whether a polygonal line is hit by this point given an epsilon.
final

Parameters

points: IEnumerable<IPoint>
The list of points that is treated as a polygon
radius: number
A positive value allows for fuzzy hit testing. If the point lies outside the given object but its distance is less than or equal to that value, it will be considered a hit.

Return Value

boolean
Whether the point hits the polygon.
Multiplies the given factor with this instance using scalar multiplication and returns the result as new instance.
This factor is applied componentwise to x and y.
final

Parameters

factor: number
The factor to scale the components by.

Return Value

Point
The result of the scalar multiplication.

Examples

Using the multiply methods
const v = new Point(2, 5)

// Make the vector three times as long
const longer = v.multiply(3)
console.log(longer) // (6, 15)

// Make the vector half as long
const shorter = v.multiply(0.5)
console.log(shorter) // (1, 2.5)

// Translate and scale the vector via a matrix
const matrix = new Matrix()
matrix.scale(2, 1.5)
matrix.translate(new Point(5, -3))

// ... or the transform method on Matrix
const p = new Point(7, 2)
const transformed = matrix.transform(p)
console.log(transformed) // (24, -1.5)
Negates the coordinates of this instance.
This negation is applied componentwise to x and y.
final

Return Value

Point
The result of the negation.
Calculates the scalar product of this and the given vector.
final

Parameters

other: Point
The other vector.

Return Value

number
The scalar product (X*other.X + Y*other.Y)
Subtracts the given vector from this instance and returns the result as new instance.
This operation is applied componentwise to x and y.
final

Parameters

point: Point
The vector.

Return Value

Point
The result of the vector subtraction.

Examples

Using the + and - operators
const p1 = new Point(2, 5)
const p2 = new Point(17, -6)

// Translate p1 by the vector p2
const p3 = p1.add(p2)
console.log(`p1 + p2 = ${p3}`) // (19, -1)

// Subtract p1 from p3 to get p2 again
const p4 = p3.subtract(p1)
console.log(p2.equals(p4)) // true

// Reverse the direction of p2
const reversed = p2.multiply(-1)
console.log(`p2 reversed: ${reversed}`) // (-17, 6)
Creates a MutablePoint that has the same coordinates as this instance.
final

Return Value

MutablePoint
A MutablePoint with the same coordinates.

Examples

The following example shows how to convert a Point to a MutablePoint and back:
const point = new Point(10, 15)

console.log(`Initial point: ${point}`) // (10, 15)

const mPoint = point.toMutablePoint()
// The following also works, due to an implicit conversion:
const mPoint2 = point

mPoint.x = 42
const point2 = mPoint.toPoint()
// The following also works, due an explicit conversion:
const point3 = mPoint

console.log(`Changed point: ${point2}`) // (42, 15)
Creates a new Point instance with the same coordinates as this point.
This method is useful to obtain a copy of the state and for making use of the various utility methods that are provided by Point.

Return Value

Point
The current values of the coordinates of the point.

See Also

API
toMutablePoint

Defined in

IPoint.toPoint
Returns a that describes this instance.
final

Return Value

string
A that describes this instance.

Constants

Gets a Point instance that has both x and y set to 0.

Static Methods

Returns the Euclidean distance between two points.
static

Parameters

p1: IPoint
An arbitrary point
p2: IPoint
An arbitrary point

Return Value

number
The Euclidean distance between p1 and p2.
Returns the Euclidean distance between two points.
static

Parameters

x1: number
x-coordinate of the first point
y1: number
y-coordinate of the first point
x2: number
x-coordinate of the second point
y2: number
y-coordinate of the second point

Return Value

number
the Euclidean distance between the first and second point
Creates a Point instance from the given point-like object by performing automatic type conversion.
static

Parameters

pointLike: Point
The object to convert to a Point.

Return Value

Point
The given pointLike if it is already a Point, or a new instance initialized to the values found in pointLike.

See Also

Developer's Guide
Calculates a linear interpolation between two points.
The points are simply joined by a straight line. The parameter t defines where to estimate the value on the interpolated line. It is 0 at point1 and 1 at point2. For interpolated values between the two points t ranges between 0 and 1. Values of t outside this range result in extrapolation.
static

Parameters

point1: Point
The point where t is 0.
point2: Point
The point where t is 1.
t: number
The parameter that defines where to estimate the value on the interpolated line.

Return Value

Point
The interpolated value.
Returns a point that geometrically lies in the middle of the line formed by the given points.
static

Parameters

p1: Point
an arbitrary instance of Point.
p2: Point
an arbitrary instance of Point.

Return Value

Point
a point that geometrically lies in the middle of the line formed by the given points.
Calculates the scalar product of the two given points.
static

Parameters

point1: Point
The first point.
point2: Point
The second point.

Return Value

number
The scalar product of the two points.
Returns a copy of the given point with exchanged x- and y-coordinates.
static

Parameters

p: Point
An arbitrary instance of Point.

Return Value

Point
A copy of the given point with exchanged x- and y-coordinates.