Package org.djutils.draw.point
Class Point2d
- java.lang.Object
-
- org.djutils.draw.point.Point2d
-
- All Implemented Interfaces:
Serializable
,Drawable<Point2d,Space2d>
,Drawable2d
,Point<Point2d,Space2d>
- Direct Known Subclasses:
OrientedPoint2d
,Ray2d
public class Point2d extends Object implements Drawable2d, Point<Point2d,Space2d>
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-2021 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
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description Point2d(double[] xy)
Create a new Point with just an x and y coordinate, stored with double precision.Point2d(double x, double y)
Create a new Point with just an x and y coordinate, stored with double precision.Point2d(Point2D point)
Create an immutable point with just two values, x and y, stored with double precision from an AWT Point2D.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Point2d
abs()
Return a new Point with absolute coordinate values.static List<Point2d>
circleIntersections(Point2d center1, double radius1, Point2d center2, double radius2)
Return the zero, one or two intersections between two circles.Point2d
closestPointOnLine(Ray2d ray)
Closest point on a ray.Point2d
closestPointOnLine(Point2d linePoint1, Point2d linePoint2)
Project a point on a line.Point2d
closestPointOnSegment(Point2d segmentPoint1, Point2d segmentPoint2)
Project a point on a line segment.double
directionTo(Point2d otherPoint)
Return the direction to another Point2d.double
distance(Point2d otherPoint)
Return the distance to another point.double
distanceSquared(Point2d otherPoint)
Return the squared distance between this point and the provided point.boolean
epsilonEquals(Point2d other, double epsilon)
A comparison with another point that returns true of each of the coordinates is less than epsilon apart.boolean
equals(Object obj)
Bounds2d
getBounds()
Retrieve the bounding rectangle of the object.Iterator<? extends Point2d>
getPoints()
Retrieve, or generate all points that make up the object.double
getX()
Return the x-coordinate.double
getY()
Return the y-coordinate.int
hashCode()
Point2d
interpolate(Point2d point, double fraction)
Interpolate towards another Point with a fraction.static Point2d
intersectionOfLines(Point2d line1P1, Point2d line1P2, Point2d line2P1, Point2d line2P2)
Compute the 2D intersection of two lines.static Point2d
intersectionOfLineSegments(Point2d line1P1, Point2d line1P2, Point2d line2P1, Point2d line2P2)
Compute the 2D intersection of two line segments.Point2d
neg()
Return a new Point with negated coordinate values.Point2d
normalize()
Return a new Point with a distance of 1 to the origin.Point2d
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.Point2D
toPoint2D()
Return the coordinates as an AWT Point2D.Double object.String
toString()
Return a string representation of the point.String
toString(int fractionDigits)
Return a string representation of the point with a certain number of fraction digits for the coordinates.Point2d
translate(double dx, double dy)
Return a new Point with a translation by the provided dx and dy.Point3d
translate(double dx, double dy, double dz)
Return a new Point3d with a translation by the provided delta-x, delta-y and deltaZ.
-
-
-
Constructor Detail
-
Point2d
public Point2d(double x, double y) throws IllegalArgumentException
Create a new Point with just an x and y coordinate, stored with double precision.- Parameters:
x
- double; the x coordinatey
- double; the y coordinate- Throws:
IllegalArgumentException
- when x or y is NaN
-
Point2d
public Point2d(double[] xy) throws NullPointerException, IllegalArgumentException
Create a new Point with just an x and y coordinate, stored with double precision.- Parameters:
xy
- double[]; the x and y coordinate- Throws:
NullPointerException
- when xy is nullIllegalArgumentException
- when the dimension of xy is not 2, or a coordinate is NaN
-
Point2d
public Point2d(Point2D point) throws NullPointerException, IllegalArgumentException
Create an immutable point with just two values, x and y, stored with double precision from an AWT Point2D.- Parameters:
point
- Point2D; an AWT Point2D- Throws:
NullPointerException
- when point is nullIllegalArgumentException
- when point has a NaN coordinate
-
-
Method Detail
-
getX
public final double getX()
Return the x-coordinate. When the point is not in Cartesian space, a calculation to Cartesian space has to be made.
-
getY
public final double getY()
Return the y-coordinate. When the point is not in Cartesian space, a calculation to Cartesian space has to be made.
-
distance
public double distance(Point2d otherPoint)
Return the distance to another point.
-
distanceSquared
public double distanceSquared(Point2d otherPoint) throws NullPointerException
Return the squared distance between this point and the provided point.- Specified by:
distanceSquared
in interfacePoint<Point2d,Space2d>
- Parameters:
otherPoint
- P; the other point- Returns:
- double; the squared distance between this point and the other point
- Throws:
NullPointerException
- when otherPoint is null
-
size
public int size()
Retrieve the number of points that make up the object.
-
getPoints
public Iterator<? extends Point2d> getPoints()
Retrieve, or generate all points that make up the object.
-
translate
public Point2d translate(double dx, double dy)
Return a new Point with a translation by the provided dx and dy.- Parameters:
dx
- double; the horizontal translationdy
- double; the vertical translation- Returns:
- P; a new point with the translated coordinates
- Throws:
IllegalArgumentException
- when dx, or dy is NaN
-
translate
public Point3d translate(double dx, double dy, double dz)
Return a new Point3d with a translation by the provided delta-x, delta-y and deltaZ.- Parameters:
dx
- double; the x translationdy
- double; the y translationdz
- double; the z translation- Returns:
- Point2d; a new point with the translated coordinates
- Throws:
IllegalArgumentException
- when dx, dy, or dz is NaN
-
scale
public Point2d scale(double factor)
Return a new Point with the coordinates of this point scaled by the provided factor.
-
neg
public Point2d neg()
Return a new Point with negated coordinate values.
-
abs
public Point2d abs()
Return a new Point with absolute coordinate values.
-
normalize
public Point2d normalize() throws DrawRuntimeException
Return a new Point with a distance of 1 to the origin.- Specified by:
normalize
in interfacePoint<Point2d,Space2d>
- Returns:
- Point; the normalized point
- Throws:
DrawRuntimeException
- when point is the origin, and no length can be established for scaling
-
interpolate
public Point2d interpolate(Point2d point, double fraction)
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,Space2d>
- Parameters:
point
- P; the other pointfraction
- double; 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:
- P; the point that is
fraction
away on the line between this point and the other point
-
epsilonEquals
public boolean epsilonEquals(Point2d other, double epsilon)
A comparison with another point that returns true of each of the coordinates is less than epsilon apart.- Specified by:
epsilonEquals
in interfacePoint<Point2d,Space2d>
- Parameters:
other
- P; the point to compare withepsilon
- double; 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, otherwise false
-
getBounds
public Bounds2d getBounds()
Retrieve the bounding rectangle of the object.- Specified by:
getBounds
in interfaceDrawable2d
- Returns:
- Bounds2d; the bounding box of the object
-
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).- Parameters:
line1P1
- Point2d; first point of line 1line1P2
- Point2d; second point of line 1line2P1
- Point2d; first point of line 2line2P2
- Point2d; second point of line 2- Returns:
- Point2d; the intersection of the two lines, or null if the lines are (almost) parallel
-
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
- Point2d; first point of line segment 1line1P2
- Point2d; second point of line segment 1line2P1
- Point2d; first point of line segment 2line2P2
- Point2d; second point of line segment 2- Returns:
- Point2d; the intersection of the two line segments, or null if the lines are (almost) parallel, or do not intersect
-
closestPointOnSegment
public final Point2d closestPointOnSegment(Point2d segmentPoint1, Point2d segmentPoint2)
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,Space2d>
- Parameters:
segmentPoint1
- P; start of line segmentsegmentPoint2
- P; end of line segment- Returns:
- P; either segmentPoint1, or segmentPoint2 or a new Point2d that lies somewhere in between those two.
-
closestPointOnLine
public final Point2d closestPointOnLine(Point2d linePoint1, Point2d linePoint2) throws DrawRuntimeException
Project a point on a line.
Adapted from example code provided by Paul Bourke.- Specified by:
closestPointOnLine
in interfacePoint<Point2d,Space2d>
- Parameters:
linePoint1
- P; point on the linelinePoint2
- P; another point on the line- Returns:
- Point2d; a point on the line that goes through linePoint1 and linePoint2
- Throws:
DrawRuntimeException
- when linePoint1 is at the same location as linePoint2
-
closestPointOnLine
public final Point2d closestPointOnLine(Ray2d ray)
Closest point on a ray.- Parameters:
ray
- Ray2d; the ray- Returns:
- Point2d; the point on the ray that is closest to this
-
circleIntersections
public static final List<Point2d> circleIntersections(Point2d center1, double radius1, Point2d center2, double radius2) throws NullPointerException, DrawRuntimeException
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
- Point2d; the center of circle 1radius1
- double; the radius of circle 1center2
- Point2d; the center of circle 2radius2
- double; the radius of circle 2- Returns:
- List<Point2d> a list of zero, one or two points
- Throws:
NullPointerException
- when center1 or center2 is nullDrawRuntimeException
- when the two circles are identical, or radius1 < 0 or radius2 < 0
-
directionTo
public double directionTo(Point2d otherPoint)
Return the direction to another Point2d.- Parameters:
otherPoint
- Point2d; the other point- Returns:
- double; 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
public Point2D toPoint2D()
Return the coordinates as an AWT Point2D.Double object.- Returns:
- Point2D; the coordinates as an AWT Point2D.Double object
-
toString
public String toString(int fractionDigits)
Return a string representation of the point with a certain number of fraction digits for the coordinates.
-
toString
public String toString()
Return a string representation of the point.
-
-