Package org.djutils.draw.point
Class Point2d
java.lang.Object
org.djutils.draw.point.Point2d
- All Implemented Interfaces:
Serializable
,Iterable<Point2d>
,Drawable<Point2d>
,Drawable2d
,Point<Point2d>
- Direct Known Subclasses:
DirectedPoint2d
A Point2d is an immutable Point with an x and y coordinate, stored with double precision. It differs from many Point
implementations by being immutable.
Copyright (c) 2020-2025 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See DJUTILS License.
- Author:
- Alexander Verbraeck, Peter Knoppers, Wouter Schakel
- See Also:
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionPoint2d
(double[] xy) Create a new Point2d from a x and y coordinates provided as values in a double array.Point2d
(double x, double y) Create a new Point2d from x and y coordinates provided as double arguments.Create an new Point2d from x and y obtained from a java.awt.geom.Point2D. -
Method Summary
Modifier and TypeMethodDescriptionabs()
Return a new Point with absolute coordinate values.circleIntersections
(Point2d center1, double radius1, Point2d center2, double radius2) Return the zero, one or two intersections between two circles.final Point2d
closestPointOnLine
(double p1X, double p1Y, double p2X, double p2Y) Project a point on a line.closestPointOnLine
(double p1X, double p1Y, double p2X, double p2Y, Boolean lowLimitHandling, Boolean highLimitHandling) Compute the closest point on a line with optional limiting of the result on either end.closestPointOnLine
(Point2d linePoint1, Point2d linePoint2) Project a point on a line.final Point2d
closestPointOnSegment
(double p1X, double p1Y, double p2X, double p2Y) Project a point on a line segment.closestPointOnSegment
(Point2d segmentPoint1, Point2d segmentPoint2) Project a point on a line segment.double
directionTo
(Point2d otherPoint) Return the direction to another Point2d.double
Return the distance to another point.double
distanceSquared
(Point2d otherPoint) Return the squared distance between this point and the provided point.boolean
epsilonEquals
(Point2d otherPoint, double epsilon) A comparison with another point that returnstrue
of each of the coordinates is less than epsilon apart.boolean
double
fractionalPositionOnLine
(double p1X, double p1Y, double p2X, double p2Y, Boolean lowLimitHandling, Boolean highLimitHandling) Compute the fractional position of the closest point on a line with optional limiting of the result on either end.Retrieve the bounding rectangle of the object.final double
getX()
Return the x-coordinate.final double
getY()
Return the y-coordinate.int
hashCode()
interpolate
(Point2d otherPoint, double fraction) Interpolate towards another Point with a fraction.static Point2d
intersectionOfLines
(double line1P1X, double line1P1Y, double line1P2X, double line1P2Y, boolean lowLimitLine1, boolean highLimitLine1, double line2P1X, double line2P1Y, double line2P2X, double line2P2Y, boolean lowLimitLine2, boolean highLimitLine2) Compute the 2D intersection of two lines.static Point2d
intersectionOfLines
(double l1P1X, double l1P1Y, double l1P2X, double l1P2Y, double l2P1X, double l2P1Y, double l2P2X, double l2P2Y) Compute the 2D intersection of two lines.static Point2d
intersectionOfLines
(Point2d line1P1, Point2d line1P2, Point2d line2P1, Point2d line2P2) Compute the 2D intersection of two lines.static Point2d
intersectionOfLineSegments
(double line1P1X, double line1P1Y, double line1P2X, double line1P2Y, double line2P1X, double line2P1Y, double line2P2X, double line2P2Y) Compute the 2D intersection of two line segments.static Point2d
intersectionOfLineSegments
(LineSegment2d segment1, LineSegment2d segment2) Compute the 2D intersection of two line segments.static Point2d
intersectionOfLineSegments
(Point2d line1P1, Point2d line1P2, Point2d line2P1, Point2d line2P2) Compute the 2D intersection of two line segments.iterator()
neg()
Return a new Point with negated coordinate values.Return a new Point with a distance of 1 to the origin.scale
(double factor) Return a new Point with the coordinates of this point scaled by the provided factor.int
size()
Retrieve the number of points that make up the object.Return the coordinates as a java.awt.geom.Point2D.Double object.toString()
Produce a string describing the Drawable using default conversion for the (double) coordinate values.Produce a String describing the Drawable.translate
(double dX, double dY) Return a new Point2d with a translation by the provided dx and dy.translate
(double dX, double dY, double dZ) Return a new Point3d with a translation by the provided dx, dy and dz.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.djutils.draw.Drawable
getPointList, toString, toString
Methods inherited from interface org.djutils.draw.Drawable2d
getDimensions
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
x
public final double xThe x-coordinate. -
y
public final double yThe y-coordinate.
-
-
Constructor Details
-
Point2d
public Point2d(double x, double y) Create a new Point2d from x and y coordinates provided as double arguments.- Parameters:
x
- the x coordinatey
- the y coordinate- Throws:
IllegalArgumentException
- whenx
, ory
isNaN
-
Point2d
public Point2d(double[] xy) Create a new Point2d from a x and y coordinates provided as values in a double array.- Parameters:
xy
- the x and y coordinates- Throws:
NullPointerException
- whenxy
isnull
IllegalArgumentException
- when the length ofxy
is not 2ArithmeticException
- whenxy
contains aNaN
value
-
Point2d
Create an new Point2d from x and y obtained from a java.awt.geom.Point2D.- Parameters:
point
- a java.awt.geom.Point2D- Throws:
NullPointerException
- whenpoint
isnull
IllegalArgumentException
- whenpoint
has aNaN
coordinate
-
-
Method Details
-
getX
public final double getX()Description copied from interface:Point
Return the x-coordinate. -
getY
public final double getY()Description copied from interface:Point
Return the y-coordinate. -
distance
Description copied from interface:Point
Return the distance to another point. -
distanceSquared
Description copied from interface:Point
Return the squared distance between this point and the provided point.- Specified by:
distanceSquared
in interfacePoint<Point2d>
- Parameters:
otherPoint
- the other point- Returns:
- the squared distance between this point and the other point
- Throws:
NullPointerException
- whenotherPoint
isnull
-
size
public int size()Description copied from interface:Drawable
Retrieve the number of points that make up the object. -
iterator
-
translate
Return a new Point2d with a translation by the provided dx and dy.- Parameters:
dX
- the x translationdY
- the y translation- Returns:
- a new point with the translated coordinates
- Throws:
IllegalArgumentException
- whendX
, ordY
isNaN
-
translate
Return a new Point3d with a translation by the provided dx, dy and dz. If this is an OrientedPoint2d, then the result is an OrientedPoint3d with rotX copied from this and rotY and rotZ are set to 0.0.- Parameters:
dX
- the x translationdY
- the y translationdZ
- the z translation- Returns:
- a new point with the translated coordinates
- Throws:
IllegalArgumentException
- whendX
,dY
, ordZ
isNaN
-
scale
Description copied from interface:Point
Return a new Point with the coordinates of this point scaled by the provided factor. -
neg
Description copied from interface:Point
Return a new Point with negated coordinate values. If this is aDirectedPoint
,dirY
(in case this is aDirectedPoint3d
anddirZ
are negated. -
abs
Description copied from interface:Point
Return a new Point with absolute coordinate values. If this is aDirectedPoint
,dirY
(in case this is aDirectedPoint3d
anddirZ
are copied unchanged. -
normalize
Description copied from interface:Point
Return a new Point with a distance of 1 to the origin.- Specified by:
normalize
in interfacePoint<Point2d>
- Returns:
- the normalized point
- Throws:
DrawRuntimeException
- when point is the origin, and no length can be established for scaling
-
interpolate
Description copied from interface:Point
Interpolate towards another Point with a fraction. It is allowed for fraction to be less than zero or larger than 1. In that case the interpolation turns into an extrapolation.- Specified by:
interpolate
in interfacePoint<Point2d>
- Parameters:
otherPoint
- the other pointfraction
- the factor for interpolation towards the other point. When <code>fraction</code> is between 0 and 1, it is an interpolation, otherwise an extrapolation. Iffraction
is 0;this
Point is returned; iffraction
is 1, the otherpoint
is returned- Returns:
- the point that is
fraction
away on the line between this point and the other point
-
epsilonEquals
Description copied from interface:Point
A comparison with another point that returnstrue
of each of the coordinates is less than epsilon apart.- Specified by:
epsilonEquals
in interfacePoint<Point2d>
- Parameters:
otherPoint
- the point to compare withepsilon
- the upper bound of difference for one of the coordinates- Returns:
- boolean;
true
if both x, y and z (if a Point3d) are less than epsilon apart, otherwisefalse
-
getBounds
Description copied from interface:Drawable2d
Retrieve the bounding rectangle of the object.- Specified by:
getBounds
in interfaceDrawable2d
- Returns:
- the bounding box of the object
-
intersectionOfLines
public static Point2d intersectionOfLines(double line1P1X, double line1P1Y, double line1P2X, double line1P2Y, boolean lowLimitLine1, boolean highLimitLine1, double line2P1X, double line2P1Y, double line2P2X, double line2P2Y, boolean lowLimitLine2, boolean highLimitLine2) Compute the 2D intersection of two lines. Both lines are defined by two points (that should be distinct).- Parameters:
line1P1X
- x-coordinate of start point of line 1line1P1Y
- y-coordinate of start point of line 1line1P2X
- x-coordinate of end point of line 1line1P2Y
- y-coordinate of end point of line 1lowLimitLine1
- iftrue
; the intersection may not lie before the start point of line 1highLimitLine1
- iftrue
; the intersection may not lie beyond the end point of line 1line2P1X
- x-coordinate of start point of line 2line2P1Y
- y-coordinate of start point of line 2line2P2X
- x-coordinate of end point of line 2line2P2Y
- y-coordinate of end point of line 2lowLimitLine2
- iftrue
; the intersection may not lie before the start point of line 2highLimitLine2
- iftrue
; the intersection may not lie beyond the end point of line 2- Returns:
- the intersection of the two lines, or
null
if the lines are (almost) parallel, or the intersection point lies outside the permitted range - Throws:
ArithmeticException
- when any of the parameters isNaN
-
intersectionOfLines
public static Point2d intersectionOfLines(double l1P1X, double l1P1Y, double l1P2X, double l1P2Y, double l2P1X, double l2P1Y, double l2P2X, double l2P2Y) Compute the 2D intersection of two lines. Both lines are defined by two points (that should be distinct). The lines are considered to be infinitely long; so unless the lines are parallel; there is an intersection.- Parameters:
l1P1X
- x-coordinate of start point of line segment 1l1P1Y
- y-coordinate of start point of line segment 1l1P2X
- x-coordinate of end point of line segment 1l1P2Y
- y-coordinate of end point of line segment 1l2P1X
- x-coordinate of start point of line segment 2l2P1Y
- y-coordinate of start point of line segment 2l2P2X
- x-coordinate of end point of line segment 2l2P2Y
- y-coordinate of end point of line segment 2- Returns:
- the intersection of the two lines, or
null
if the lines are (almost) parallel - Throws:
ArithmeticException
- when any of the parameters isNaN
-
intersectionOfLines
public static Point2d intersectionOfLines(Point2d line1P1, Point2d line1P2, Point2d line2P1, Point2d line2P2) Compute the 2D intersection of two lines. Both lines are defined by two points (that should be distinct). The lines are considered to be infinitely long; so unless the lines are parallel; there is an intersection.- Parameters:
line1P1
- first point of line 1line1P2
- second point of line 1line2P1
- first point of line 2line2P2
- second point of line 2- Returns:
- the intersection of the two lines, or
null
if the lines are (almost) parallel - Throws:
NullPointerException
- when any of the points isnull
-
intersectionOfLineSegments
public static Point2d intersectionOfLineSegments(Point2d line1P1, Point2d line1P2, Point2d line2P1, Point2d line2P2) Compute the 2D intersection of two line segments. Both line segments are defined by two points (that should be distinct).- Parameters:
line1P1
- first point of line segment 1line1P2
- second point of line segment 1line2P1
- first point of line segment 2line2P2
- second point of line segment 2- Returns:
- the intersection of the two line segments, or
null
if the lines are parallel (within rounding error), or do not intersect - Throws:
NullPointerException
- when any of the points isnull
DrawRuntimeException
- when any of the line segments is ill-defined (begin point equals end point), or the two line segments are parallel or overlapping
-
intersectionOfLineSegments
public static Point2d intersectionOfLineSegments(double line1P1X, double line1P1Y, double line1P2X, double line1P2Y, double line2P1X, double line2P1Y, double line2P2X, double line2P2Y) Compute the 2D intersection of two line segments. Both line segments are defined by two points (that should be distinct).- Parameters:
line1P1X
- x coordinate of start point of first line segmentline1P1Y
- y coordinate of start point of first line segmentline1P2X
- x coordinate of end point of first line segmentline1P2Y
- y coordinate of end point of first line segmentline2P1X
- x coordinate of start point of second line segmentline2P1Y
- y coordinate of start point of second line segmentline2P2X
- x coordinate of end point of second line segmentline2P2Y
- y coordinate of end point of second line segment- Returns:
- the intersection of the two line segments, or
null
if the lines are parallel (within rounding error), or do not intersect - Throws:
ArithmeticException
- when any of the values isNaN
-
intersectionOfLineSegments
Compute the 2D intersection of two line segments.- Parameters:
segment1
- the first line segmentsegment2
- the other line segment- Returns:
- the intersection of the line segments, or
null
if the line segments do not intersect
-
closestPointOnSegment
Description copied from interface:Point
Project a point on a line segment. If the the projected points lies outside the line segment, the nearest end point of the line segment is returned. Otherwise the returned point lies between the end points of the line segment.
Adapted from example code provided by Paul Bourke.- Specified by:
closestPointOnSegment
in interfacePoint<Point2d>
- Parameters:
segmentPoint1
- start of line segmentsegmentPoint2
- end of line segment- Returns:
- either
segmentPoint1
, orsegmentPoint2
or a new Point2d that lies somewhere in between those two.
-
closestPointOnLine
public Point2d closestPointOnLine(double p1X, double p1Y, double p2X, double p2Y, Boolean lowLimitHandling, Boolean highLimitHandling) Compute the closest point on a line with optional limiting of the result on either end.- Parameters:
p1X
- the x coordinate of the first point on the linep1Y
- the y coordinate of the first point on the linep2X
- the x coordinate of the second point on the linep2Y
- the y coordinate of the second point on the linelowLimitHandling
- controls handling of results that lie before the first point of the line. Ifnull
; this method returnsnull
; else iftrue
; this method returns (p1X,p1Y); else (lowLimitHandling isfalse
); this method will return the closest point on the linehighLimitHandling
- controls the handling of results that lie beyond the second point of the line. Ifnull
; this method returnsnull
; else iftrue
; this method returns (p2X,p2Y); else (highLimitHandling isfalse
); this method will return the closest point on the line- Returns:
- the closest point on the line after applying the indicated limit handling; so the result can be
null
- Throws:
ArithmeticException
- when any of the arguments isNaN
DrawRuntimeException
- when the line is ill-defined (begin point coincides with end point)
-
fractionalPositionOnLine
public double fractionalPositionOnLine(double p1X, double p1Y, double p2X, double p2Y, Boolean lowLimitHandling, Boolean highLimitHandling) Compute the fractional position of the closest point on a line with optional limiting of the result on either end. If the line has length 0; this method returns 0.0.- Parameters:
p1X
- the x coordinate of the first point on the linep1Y
- the y coordinate of the first point on the linep2X
- the x coordinate of the second point on the linep2Y
- the y coordinate of the second point on the linelowLimitHandling
- controls handling of results that lie before the first point of the line. Ifnull
; this method returnsNaN
; else iftrue
; this method returns 0.0; else (lowLimitHandling isfalse
); this results < 0.0 are returnedhighLimitHandling
- controls the handling of results that lie beyond the second point of the line. Ifnull
; this method returnsNaN
; else iftrue
; this method returns 1.0; else (highLimitHandling isfalse
); results > 1.0 are returned- Returns:
- the fractional position of the closest point on the line. Results within the range 0.0 .. 1.0 are always returned as is.. A result < 0.0 is subject to lowLimitHandling. A result > 1.0 is subject to highLimitHandling
- Throws:
ArithmeticException
- when any of the arguments is NaNDrawRuntimeException
- when the line is ill-defined (begin point coincides with end point)
-
closestPointOnSegment
Project a point on a line segment. If the the projected points lies outside the line segment, the nearest end point of the line segment is returned. Otherwise the returned point lies between the end points of the line segment.
Adapted from example code provided by Paul Bourke.- Parameters:
p1X
- the x coordinate of the start point of the line segmentp1Y
- the y coordinate of the start point of the line segmentp2X
- the x coordinate of the end point of the line segmentp2Y
- the y coordinate of the end point of the line segment- Returns:
- either
segmentPoint1
, orsegmentPoint2
or a new Point2d that lies somewhere in between those two. - Throws:
DrawRuntimeException
- when the line is ill-defined (begin point coincides with end point)
-
closestPointOnLine
public Point2d closestPointOnLine(Point2d linePoint1, Point2d linePoint2) throws NullPointerException, DrawRuntimeException Description copied from interface:Point
Project a point on a line.
Adapted from example code provided by Paul Bourke.- Specified by:
closestPointOnLine
in interfacePoint<Point2d>
- Parameters:
linePoint1
- point on the linelinePoint2
- another point on the line- Returns:
- a point on the line that goes through
linePoint1
andlinePoint2
- Throws:
NullPointerException
- whenlinePoint1
isnull
, orlinePoint2
isnull
DrawRuntimeException
-
closestPointOnLine
Project a point on a line.
Adapted from example code provided by Paul Bourke.- Parameters:
p1X
- the x coordinate of a point of the line segmentp1Y
- the y coordinate of a point of the line segmentp2X
- the x coordinate of another point of the line segmentp2Y
- the y coordinate of another point of the line segment- Returns:
- a point on the line that goes through the points
- Throws:
IllegalArgumentException
- when the points on the line are identical
-
circleIntersections
public static final List<Point2d> circleIntersections(Point2d center1, double radius1, Point2d center2, double radius2) throws NullPointerException, IllegalArgumentException Return the zero, one or two intersections between two circles. The circles must be different. Derived from pseudo code by Paul Bourke and C implementation by Tim Voght .- Parameters:
center1
- the center of circle 1radius1
- the radius of circle 1center2
- the center of circle 2radius2
- the radius of circle 2- Returns:
- List<Point2d> a list of zero, one or two points
- Throws:
NullPointerException
- whencenter1
orcenter2
isnull
IllegalArgumentException
- when the two circles are identical, orradius1 < 0.0
, orradius2 < 0.0
-
directionTo
Return the direction to another Point2d.- Parameters:
otherPoint
- the other point- Returns:
- the direction to the other point in Radians (towards infinite X is 0; towards infinite Y is π / 2;
etc.). If the points are identical; this method returns
NaN
.
-
toPoint2D
Return the coordinates as a java.awt.geom.Point2D.Double object.- Returns:
- java.awt.geom.Point2D; the coordinates as a java.awt.geom.Point2D.Double object
-
toString
Description copied from interface:Drawable
Produce a string describing the Drawable using default conversion for the (double) coordinate values. Regrettably, it is not allowed to provide a default implementation here. -
toString
Description copied from interface:Drawable
Produce a String describing the Drawable.- Specified by:
toString
in interfaceDrawable<Point2d>
- Parameters:
doubleFormat
- a format string (something like "%6.3f") which will be used to render every coordinate value)doNotIncludeClassName
- iftrue
; the output oftoString
is not prefixed by the class name. This is useful for concatenating the textual representation of lots of Drawables (e.g. an array, or a List).- Returns:
- textual representation of the Drawable
-
hashCode
public int hashCode() -
equals
-