Represents a point in two-dimensional Cartesian coordinates.
Implements
- I
- I
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:
- Distance measurements – distanceTo, distanceToSegment
- Projections – getProjectionOnSegment, getProjectionOnLine, getProjectionOnRay
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
Properties
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).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.
Creates a normalized version of this vector.
readonlyfinal
Property Value
The normalized version of this vector or
(1,0) if this vector has zero length.readonlyfinal
Property Value
The squared length of the vector which is
X*X + Y*Y.See Also
API
- vectorLength
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
Methods
Parameters
- point: Point
- The vector.
Return Value
- Point
- The result of the vector addition.
Examples
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)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.
Implements
IComparable.compareToCalculates the Euclidean distance between two points.
Calculates the Euclidean distance between two points.
Parameters
- point2: IPoint
- The second point.
Return Value
- number
- The distance between the two points.
Defined in
IPoint.distanceToParameters
- 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).
Defined in
IPoint.distanceTofinal
Parameters
- other: any
- Another object to compare to.
Return Value
- boolean
trueifotherand this instance are the same type and represent the same value; otherwise,false.
Examples
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)) // falseDetermines whether the two given points have the same coordinates with respect to a certain given eps.
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
otherpoint'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
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)) // falsefinal
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.
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 a polygonal line is hit by this point given an epsilon.
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.
Multiplies the given factor with this instance using scalar multiplication and returns the result as new instance.
Parameters
- factor: number
- The factor to scale the components by.
Return Value
- Point
- The result of the scalar multiplication.
Examples
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.
Negates the coordinates of this instance.
Calculates the scalar product of this and the given vector.
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)
Parameters
- point: Point
- The vector.
Return Value
- Point
- The result of the vector subtraction.
Examples
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.
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)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
Defined in
IPoint.toPointConstants
Static Methods
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
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
tis0. - point2: Point
- The point where
tis1. - t: number
- The parameter that defines where to estimate the value on the interpolated line.
Return Value
- Point
- The interpolated value.