Class Point
- All Implemented Interfaces:
Comparable<Point>
Point in multidimensional space with real coordinates. Represented as an array of double numbers.
This class is immutable and thread-safe: there are no ways to modify settings of the created instance.
- Author:
- Daniel Alievsky
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionaddToAllCoordinates
(double increment) Adds the given value to all coordinates of this point and returns the resulting point: every coordinate #i in the result is thisInstance.coord(i)
+increment.int
Compares points lexicographically.int
Compares points lexicographically alikecompareTo(Point)
method, but with the cyclical shift of all indexes of coordinates: the coordinate #firstCoordIndex instead of x, #firstCoordIndex+1 instead of y, etc.double
coord
(int coordIndex) Returns the coordinate #coordIndex: x-coordinate for coordIndex=0, y-coordinate for coordIndex=1, etc.int
Returns the number of dimensions of this point.double[]
Returns all coordinates of this point.double[]
coordinates
(double[] result) Copies all coordinates of this point into result array.double
distanceFrom
(Collection<Point> points) Returns the minimal distance between this point and any point from the passed collection.double
Returns the distance between this point and the origin of coordinates.boolean
Indicates whether some other point is equal to this instance, that is the number of coordinates is the same and all corresponding coordinates are equal.int
hashCode()
Returns the hash code of this point.boolean
Returns true if and only if this point is really integer, that is if for any its coordinate xi=coord
(i) the Java expression xi==(long)xi is true.boolean
isOrigin()
Returns true if this point is the origin of coordinates.static Point
maxValue
(int coordCount) Returns the "maximal" point in n-dimensional space, where n=coordCount is the argument of this method, that is the point with all coordinates are equal to Double.POSITIVE_INFINITY.static Point
minValue
(int coordCount) Returns the "minimal" point in n-dimensional space, where n=coordCount is the argument of this method, that is the point with all coordinates are equal to Double.NEGATIVE_INFINITY.multiply
(double multiplier) Returns the product of this point and the given scalar multiplier: every coordinate #i in the result is thisInstance.coord(i)
*multiplier.static Point
origin
(int coordCount) Returns the origin of coordinates in n-dimensional space, where n=coordCount is the argument of this method.projectionAlongAxis
(int coordIndex) Returns the projection of this point along the given axis with the number of coordinates, decreased by 1.double
scalarProduct
(Point point) Returns the scalar product of this and given point.scale
(double... multipliers) Returns new point, each coordinate #i of which is thisInstance.coord(i)
*multipliers[i].void
scaleAndShift
(double[] resultCoordinates, double[] multipliers, Point shift) More efficient version ofscaleAndShift(double[], Point)
method, which stores the coordinates of the result in the passed Java array instead of creating new instance of this class.scaleAndShift
(double[] multipliers, Point shift) shiftAlongAxis
(int coordIndex, double shift) Returns this point shifted by the passed shift along the axis #coordIndex.Returns the symmetric point relatively the origin of coordinates.Equivalent toIPoint.valueOf
(thisInstance).Equivalent toIPoint.roundOf
(thisInstance).toString()
Returns a brief string description of this object.static Point
valueOf
(double x) Returns a new 1-dimensional point with the given coordinate.static Point
valueOf
(double... coordinates) Returns a new point with the given set of coordinates: x, y, z, ...static Point
valueOf
(double x, double y) Returns a new 2-dimensional point with the given coordinates.static Point
valueOf
(double x, double y, double z) Returns a new 3-dimensional point with the given coordinates.static Point
Returns a new point with the same coordinates as the given integer point.static Point
valueOfEqualCoordinates
(int coordCount, double filler) Returns a new point in n-dimensional space, where n=coordCount and all coordinates of the point are equal to the given value filler.double
x()
Returns the x-coordinate: equivalent tocoord(0)
.double
y()
Returns y-coordinate: equivalent tocoord(1)
.double
z()
Returns z-coordinate: equivalent tocoord(2)
.
-
Method Details
-
valueOf
Returns a new point with the given set of coordinates: x, y, z, ... For example, valueOf(x,y) returns the 2D point with given coordinates.The coordinates array must contain at least 1 element.
The passed coordinates array is cloned by this method: no references to it are maintained by the created object.
- Parameters:
coordinates
- cartesian coordinates of the point.- Returns:
- the point with the given coordinates.
- Throws:
NullPointerException
- if the passed array is null.IllegalArgumentException
- if the passed array is empty (no coordinates are passed).
-
valueOf
Returns a new 1-dimensional point with the given coordinate.- Parameters:
x
- cartesian coordinate of the point.- Returns:
- point with the given coordinates.
-
valueOf
Returns a new 2-dimensional point with the given coordinates.- Parameters:
x
- cartesian x-coordinate of the point.y
- cartesian y-coordinate of the point.- Returns:
- point with the given coordinates.
-
valueOf
Returns a new 3-dimensional point with the given coordinates.- Parameters:
x
- cartesian x-coordinate of the point.y
- cartesian y-coordinate of the point.z
- cartesian z-coordinate of the point.- Returns:
- point with the given coordinates.
-
valueOfEqualCoordinates
Returns a new point in n-dimensional space, where n=coordCount and all coordinates of the point are equal to the given value filler. For example,valueOfEqualCoordinates(3, 1) returns the 3D point(1,1,1) . If filler==0, this method is equivalent toorigin(coordCount)
.- Parameters:
coordCount
- the number of dimensions.filler
- the value of each coordinate of the created point.- Returns:
- the point with equal coordinates.
- Throws:
IllegalArgumentException
- if the passed number of dimensions is zero or negative.
-
origin
Returns the origin of coordinates in n-dimensional space, where n=coordCount is the argument of this method.- Parameters:
coordCount
- the number of dimensions.- Returns:
- the origin of coordinates in n-dimensional space.
- Throws:
IllegalArgumentException
- if the passed number of dimensions is zero or negative.
-
minValue
Returns the "minimal" point in n-dimensional space, where n=coordCount is the argument of this method, that is the point with all coordinates are equal to Double.NEGATIVE_INFINITY.- Parameters:
coordCount
- the number of dimensions.- Returns:
- the "minimal" point in n-dimensional space.
- Throws:
IllegalArgumentException
- if the passed number of dimensions is zero or negative.
-
maxValue
Returns the "maximal" point in n-dimensional space, where n=coordCount is the argument of this method, that is the point with all coordinates are equal to Double.POSITIVE_INFINITY.- Parameters:
coordCount
- the number of dimensions.- Returns:
- the "maximal" point in n-dimensional space.
- Throws:
IllegalArgumentException
- if the passed number of dimensions is zero or negative.
-
valueOf
Returns a new point with the same coordinates as the given integer point. All long coordinates of the passed integer point are converted to double coordinates of the returned point by standard Java typecast (double)longValue.- Parameters:
iPoint
- the integer point.- Returns:
- the real point with same coordinates.
- Throws:
NullPointerException
- if the passed integer point is null.
-
coordCount
public int coordCount()Returns the number of dimensions of this point. Equivalent tocoordinates()
.length, but works faster.The result of this method is always positive (>0).
- Returns:
- the number of dimensions of this point.
-
coordinates
public double[] coordinates()Returns all coordinates of this point. The element #0 of the returned array is x-coordinate, the element #1 is y-coordinate, etc. The length of the returned array is the number of dimensions of this point.The returned array is a clone of the internal coordinates array stored in this object. The returned array is never empty (its length >0 always).
- Returns:
- all coordinates of this point.
-
coordinates
public double[] coordinates(double[] result) Copies all coordinates of this point into result array. The element #0 of this array will contain x-coordinate, the element #1 will contain y-coordinate, etc. The length of the passed array must be not less than the number of dimensions of this point.- Parameters:
result
- the array where you want to store results.- Returns:
- a reference to the passed result array.
- Throws:
NullPointerException
- if result argument is null.IllegalArgumentException
- if result.length<coordCount()
.
-
coord
public double coord(int coordIndex) Returns the coordinate #coordIndex: x-coordinate for coordIndex=0, y-coordinate for coordIndex=1, etc.- Parameters:
coordIndex
- the index of the coordinate.- Returns:
- the coordinate.
- Throws:
IndexOutOfBoundsException
- if coordIndex<0 or coordIndex>=coordCount()
.
-
x
public double x()Returns the x-coordinate: equivalent tocoord(0)
.- Returns:
- x-coordinate.
-
y
public double y()Returns y-coordinate: equivalent tocoord(1)
. The only difference: in a case of 1-dimensional point (coordCount()
<2), this method throws IllegalStateException instead of IndexOutOfBoundsException.- Returns:
- y-coordinate.
- Throws:
IllegalStateException
- ifcoordCount()
<2.
-
z
public double z()Returns z-coordinate: equivalent tocoord(2)
. The only difference: in a case of 1- or 2-dimensional point (coordCount()
<3), this method throws IllegalStateException instead of IndexOutOfBoundsException.- Returns:
- z-coordinate.
- Throws:
IllegalStateException
- ifcoordCount()
<3.
-
isOrigin
public boolean isOrigin()Returns true if this point is the origin of coordinates. In other words, returns true if all coordinates of this point are zero.- Returns:
- true if this point is the origin of coordinates.
-
distanceFromOrigin
public double distanceFromOrigin()Returns the distance between this point and the origin of coordinates.- Returns:
- the distance between this point and the origin of coordinates.
-
distanceFrom
Returns the minimal distance between this point and any point from the passed collection. If is also called the Hausdorff distance between the point and the point set. If the passed collection is empty, returns Double.POSITIVE_INFINITY.- Parameters:
points
- some collection of points.- Returns:
- the Hausdorff distance from this point to the set of points, passed via the collection.
- Throws:
NullPointerException
- if the argument is null or if some elements of the passed collection are null.IllegalArgumentException
- if thenumbers of dimensions
in this point and in some of the given points are different.
-
add
Returns the vector sum of this and given point: every coordinate #i in the result is thisInstance.coord(i)
+point.coord(i)
.- Parameters:
point
- the added point.- Returns:
- the vector sum of this and given point.
- Throws:
NullPointerException
- if the argument is null.IllegalArgumentException
- if thenumbers of dimensions
in this and given points are different.
-
subtract
Returns the vector difference of this and given point: every coordinate #i in the result is thisInstance.coord(i)
-point.coord(i)
.- Parameters:
point
- the subtracted point.- Returns:
- the vector difference of this and given point.
- Throws:
NullPointerException
- if the argument is null.IllegalArgumentException
- if thenumbers of dimensions
in this and given points are different.
-
min
Returns the coordinate-wise minimum of this and given point: every coordinate #i in the result is Math.min(thisInstance.coord(i)
,point.coord(i)
).- Parameters:
point
- the compared point.- Returns:
- the coordinate-wise minimum this and given point.
- Throws:
NullPointerException
- if the argument is null.IllegalArgumentException
- if thenumbers of dimensions
in this and given points are different.
-
max
Returns the coordinate-wise maximum of this and given point: every coordinate #i in the result is Math.max(thisInstance.coord(i)
,point.coord(i)
).- Parameters:
point
- the compared point.- Returns:
- the coordinate-wise maximum this and given point.
- Throws:
NullPointerException
- if the argument is null.IllegalArgumentException
- if thenumbers of dimensions
in this and given points are different.
-
addToAllCoordinates
Adds the given value to all coordinates of this point and returns the resulting point: every coordinate #i in the result is thisInstance.coord(i)
+increment. In other words, shifts this point along all axes by the given value.Equivalent to
add
(Point.valueOfEqualCoordinates
(n,increment)), where n=coordCount()
.- Parameters:
increment
- the value, which will be added to all coordinates of this point.- Returns:
- this resulting point.
-
multiply
Returns the product of this point and the given scalar multiplier: every coordinate #i in the result is thisInstance.coord(i)
*multiplier.Equivalent to
scale(double...multipliers)
, where allcoordCount()
arguments of that method are equal to multiplier.- Parameters:
multiplier
- the multiplier.- Returns:
- the product of this point and the given scalar multiplier.
-
scale
Returns new point, each coordinate #i of which is thisInstance.coord(i)
*multipliers[i]. The length of multipliers array must be equal tocoordCount()
.Note: this method does not perform actual multiplication to multipliers, equal to 1.0, 0.0 and −1.0. If the condition multipliers[k]==1.0 is true for some k, then the coordinate #k is just copied from this point into the result. If the condition multipliers[k]==0.0 is true for some k, then the coordinate #k in the result will be 0.0 (always +0.0, regardless of the sign of this coordinate in the source point). If the condition multipliers[k]==-1.0 is true for some k, then the coordinate #k in the result will be equal to -
coord
(k).- Parameters:
multipliers
- the multipliers for all coordinates.- Returns:
- the point, each coordinate #i of which is product of the corresponding coordinate #i of this one and the corresponding multiplier multiplier[i].
- Throws:
NullPointerException
- if the argument is null.IllegalArgumentException
- if thenumbers of dimensions
is not equal to multipliers.length.
-
scaleAndShift
Returns new point, each coordinate #i of which is shift.coord(i)
+thisInstance.coord(i)
*multipliers[i]. The length of multipliers array must be equal tocoordCount()
.Note: this method does not perform actual multiplication to multipliers, equal to 1.0, 0.0 and −1.0. If the condition multipliers[k]==1.0 is true for some k, then the coordinate #k will be equal to shift.
coord
(k)+thisInstance.coord
(k). If the condition multipliers[k]==0.0 is true for some k, then the coordinate #k in the result will be equal to shift.coord
(k). If the condition multipliers[k]==-1.0 is true for some k, then the coordinate #k in the result will be equal to shift.coord
(k)-thisInstance.coord
(k).- Parameters:
multipliers
- the multipliers for all coordinates.shift
- the shift along all coordinates.- Returns:
- the point, each coordinate #i of which is product of the corresponding coordinate
#i of this one and the corresponding multiplier multiplier[i],
incremented by the corresponding coordinate
shift.
coord(i)
. - Throws:
NullPointerException
- if one of the arguments is null.IllegalArgumentException
- if thenumbers of dimensions
is not equal to multipliers.length.
-
scaleAndShift
More efficient version ofscaleAndShift(double[], Point)
method, which stores the coordinates of the result in the passed Java array instead of creating new instance of this class. Equivalent to the following call: , but works little faster.scaleAndShift
(multipliers,shift).coordinates
(resultCoordinates)- Parameters:
resultCoordinates
- Java array for storing results.multipliers
- the multipliers for all coordinates.shift
- the shift along all coordinates.- Throws:
NullPointerException
- if one of the arguments is null.IllegalArgumentException
- if thenumbers of dimensions
is greater than resultCoordinates.length or is not equal to multipliers.length.
-
shiftAlongAxis
Returns this point shifted by the passed shift along the axis #coordIndex. Equivalent toadd(p)
, where p is the point with coordinates p0, p1, ..., pk=0 for k!=coordIndex, pk=shift for k=coordIndex.- Parameters:
coordIndex
- the index of the axis.shift
- the shift along this axis.- Returns:
- this point shifted along this axis.
- Throws:
IndexOutOfBoundsException
- if coordIndex<0 or coordIndex>=coordCount()
.
-
scalarProduct
Returns the scalar product of this and given point. The result is the sum of all products thisInstance.coord(i)
*point.coord(i)
for all coordinate indexes i.- Parameters:
point
- another point.- Returns:
- the scalar product of this and given point.
- Throws:
NullPointerException
- if the argument is null.IllegalArgumentException
- if thenumbers of dimensions
in this and given points are different.
-
symmetric
Returns the symmetric point relatively the origin of coordinates. Equivalent tomultiply(-1.0)
.- Returns:
- the symmetric point relatively the origin of coordinates.
-
projectionAlongAxis
Returns the projection of this point along the given axis with the number of coordinates, decreased by 1. Namely, the resulting point P hascoordCount()
−1 coordinates, equal toP. , i'=i for i<coordIndex or i'=i+1 for i≥coordIndex.coord
(i)=thisInstance.coord
(i')- Parameters:
coordIndex
- the number of coordinate, along which the projecting is performed.- Returns:
- the projection of this point along the coordinates axis #coordIndex.
- Throws:
IndexOutOfBoundsException
- if coordIndex<0 or coordIndex>=coordCount()
.IllegalStateException
- if this point is 1-dimensional (coordCount()
==1).
-
compareTo
Compares points lexicographically.More precisely, let xi is thisInstance.
coord
(i) for 0<=i<thisInstance.coordCount()
and xi=0 for i>=thisInstance.coordCount()
. Then, let yi is o.coord
(i) for 0<=i<o.coordCount()
and yi=0 for i>=o.coordCount()
. This method returns a negative integer if there is such index k that xk<yk and xi=yi for all i>k; this method returns a positive integer if there is such index k that xk>yk and xi=yi for all i>k. If all xi=yi, this method returns a negative integer, 0 or a positive integer if thisInstance.coordCount()
is less than, equal to, or greater than o.coordCount()
.- Specified by:
compareTo
in interfaceComparable<Point>
- Parameters:
o
- the point to be compared.- Returns:
- negative integer, zero, or a positive integer as this point is lexicographically less than, equal to, or greater than o.
- Throws:
NullPointerException
- if the argument is null.
-
compareTo
Compares points lexicographically alikecompareTo(Point)
method, but with the cyclical shift of all indexes of coordinates: the coordinate #firstCoordIndex instead of x, #firstCoordIndex+1 instead of y, etc.More precisely, let n=max(thisInstance.
coordCount()
,o.coordCount()
), xi is thisInstance.coord
((i+firstCoordIndex)%n), yi is o.coord
((i+firstCoordIndex)%n). As incompareTo(Point)
method, we suppose here that all coordinatescoord(k)
with k>=coordCount()
are zero. This method returns a negative integer if there is such index k that xk<yk and xi=yi for all i>k; this method returns a positive integer if there is such index k that xk>yk and xi=yi for all i>k. If all xi=yi, this method returns a negative integer, 0 or a positive integer if thisInstance.coordCount()
is less than, equal to, or greater than o.coordCount()
.- Parameters:
o
- the point to be compared.firstCoordIndex
- the index of "first" coordinate, that is compared after all other coordinates.- Returns:
- negative integer, zero, or a positive integer as this point is lexicographically less than, equal to, or greater than o.
- Throws:
NullPointerException
- if the o argument is null.IllegalArgumentException
- if firstCoordIndex is negative.
-
toString
Returns a brief string description of this object.The result of this method may depend on implementation and usually contains a list of all point coordinates.
-
hashCode
public int hashCode()Returns the hash code of this point. The result depends on allcoordinates
. -
equals
Indicates whether some other point is equal to this instance, that is the number of coordinates is the same and all corresponding coordinates are equal. The corresponding coordinates are compared as in Double.equals method, i.e. they are converted to long values by Double.doubleToLongBits method and the results are compared. -
isInteger
public boolean isInteger()Returns true if and only if this point is really integer, that is if for any its coordinate xi=coord
(i) the Java expression xi==(long)xi is true.- Returns:
- true if all coordinates of this point are integer numbers.
-
toIntegerPoint
Equivalent toIPoint.valueOf
(thisInstance).- Returns:
- the integer point with same (cast) coordinates.
-
toRoundedPoint
Equivalent toIPoint.roundOf
(thisInstance).- Returns:
- the integer point with same (rounded) coordinates.
-