Represents a point in two-dimensional Cartesian coordinates.
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
// 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}`)
Type Details
- yFiles module
- core
See Also
Constructors
Properties
Gets whether this instance is a horizontally oriented vector.
Remarks
Property Value
true
iff Math.Abs(X) > Math.Abs(Y)
.Gets whether this instance is a vertically oriented vector.
Remarks
Property Value
true
iff Math.Abs(Y) > Math.Abs(X)
.Creates a normalized version of this vector.
Remarks
Property Value
(1,0)
if this vector has zero length.Gets the length of the vector that has x and y as its components.
Remarks
Property Value
X*X + Y*Y
.See Also
Gets the x-coordinate of the point.
Gets the y-coordinate of the point.
Methods
Adds the given vector to this point.
Remarks
Parameters
A map of options to pass to the method.
- point - Point
- The vector.
Returns
- ↪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)
Comparable implementation.
Remarks
Parameters
A map of options to pass to the method.
- o - any
- The object to compare with the current instance.
Returns
- ↪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
Returns the euclidean distance between this point and a given point.
Parameters
A map of options to pass to the method.
- x - number
- the x coordinate of the second point
- y - number
- the y coordinate of the second point
Returns
- ↪number
- the Euclidean distance between this point and the point (x,y).
Defined in
Determines the distance between this point and a line segment.
Indicates whether this instance and a specified object are equal.
Parameters
A map of options to pass to the method.
- other - any
- Another object to compare to.
Returns
- ↪boolean
true
ifother
and 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)) // false
Determines whether the two given points have the same coordinates with respect to a certain given eps
.
Parameters
A map of options to pass to the method.
- 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.
Returns
- ↪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)) // false
Create a constrained copy of this instance that lies within the given non-empty rectangle
.
Remarks
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.
Parameters
A map of options to pass to the method.
- rectangle - Rect
- The rectangle to constrain this instance by.
Returns
- ↪Point
- A constrained copy of this instance.
Calculates the projection of this point onto a ray.
Remarks
rayStart
), the rayStart
is returned instead.Parameters
A map of options to pass to the method.
Returns
- ↪Point
- The point on the ray that is closest to this point.
Calculates the projection of this point onto a segment.
Remarks
Parameters
A map of options to pass to the method.
Returns
- ↪Point
- The point on the segment that is closest to this point.
Determines if the point lies close to this point given an epsilon.
Parameters
A map of options to pass to the method.
- other - Point
- The coordinates of the other point.
- hitTestRadius - number
- The hit test epsilon.
Returns
- ↪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
.
Determines whether a polygonal line is hit by this point given an epsilon.
Parameters
A map of options to pass to the method.
- 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.
Returns
- ↪boolean
- Whether the point hits the polygon.
Multiplies the given factor with this instance using scalar multiplication and returns the result as new instance.
Remarks
Parameters
A map of options to pass to the method.
- factor - number
- The factor to scale the components by.
Returns
- ↪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)
Subtracts the given vector from this instance and returns the result as new instance.
Remarks
Parameters
A map of options to pass to the method.
- point - Point
- The vector.
Returns
- ↪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.
Returns
- ↪MutablePoint
- A MutablePoint with the same coordinates.
Examples
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)
Static Methods
Returns the Euclidean distance between two points.
Parameters
A map of options to pass to the method.
- 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
Returns
- ↪number
- the Euclidean distance between the first and second point
Calculates a linear interpolation between two points.
Remarks
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.Parameters
A map of options to pass to the method.
- point1 - Point
- The point where
t
is0
. - point2 - Point
- The point where
t
is1
. - t - number
- The parameter that defines where to estimate the value on the interpolated line.
Returns
- ↪Point
- The interpolated value.
Returns a point that geometrically lies in the middle of the line formed by the given points.