Package org.djutils.draw.line
Class LineSegment2d
- java.lang.Object
-
- org.djutils.draw.line.LineSegment2d
-
- All Implemented Interfaces:
Serializable
,Drawable<Point2d,Space2d>
,Drawable2d
,LineSegment<Point2d,Ray2d,Space2d>
public class LineSegment2d extends Object implements Drawable2d, LineSegment<Point2d,Ray2d,Space2d>
LineSegment2d is a line segment bound by 2 end points in 2D-space. A line segment stores the order in which it has been created, so the end points are known as 'start' and 'end'.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 LineSegment2d(double startX, double startY, double endX, double endY)
Construct a new LineSegment2d start four coordinates.LineSegment2d(double startX, double startY, Point2d end)
Construct a new LineSegment2d start two coordinates and a Point2d.LineSegment2d(Point2d start, double endX, double endY)
Construct a new LineSegment2d start a Point2d and two coordinates.LineSegment2d(Point2d start, Point2d end)
Construct a new LineSegment2d start two Point2d objects.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Point2d
closestPointOnSegment(Point2d point)
Project a Point on this LineSegment.boolean
equals(Object obj)
Bounds2d
getBounds()
Retrieve the bounding rectangle of the object.Point2d
getEndPoint()
Get the end point of this LineSegment.double
getLength()
Get the length (distance from start point to end point) of this LineSegment.Ray2d
getLocationExtended(double position)
Create a Ray on a specified point on this LineSegment.Iterator<? extends Point2d>
getPoints()
Retrieve, or generate all points that make up the object.Point2d
getStartPoint()
Get the start point of this LineSegment.int
hashCode()
int
size()
Retrieve the number of points that make up the object.String
toString()
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.djutils.draw.line.LineSegment
getLocation
-
-
-
-
Constructor Detail
-
LineSegment2d
public LineSegment2d(double startX, double startY, double endX, double endY) throws DrawRuntimeException
Construct a new LineSegment2d start four coordinates.- Parameters:
startX
- double; the x-coordinate of the start pointstartY
- double; the y-coordinate of the start pointendX
- double; the x-coordinate of the end pointendY
- double; the y-coordinate of the end point- Throws:
DrawRuntimeException
- when (startX,startY) is equals end (endX,endY)
-
LineSegment2d
public LineSegment2d(Point2d start, double endX, double endY) throws NullPointerException, DrawRuntimeException
Construct a new LineSegment2d start a Point2d and two coordinates.- Parameters:
start
- Point2d; the start pointendX
- double; the x-coordinate of the end pointendY
- double; the y-coordinate of the end point- Throws:
NullPointerException
- when start is nullDrawRuntimeException
- when start has the exact coordinates endX, endY
-
LineSegment2d
public LineSegment2d(double startX, double startY, Point2d end) throws NullPointerException, DrawRuntimeException
Construct a new LineSegment2d start two coordinates and a Point2d.- Parameters:
startX
- double; the x-coordinate of the start pointstartY
- double; the y-coordinate of the start pointend
- Point2d; the end point- Throws:
NullPointerException
- when end is nullDrawRuntimeException
- when end has the exact coordinates startX, startY
-
LineSegment2d
public LineSegment2d(Point2d start, Point2d end) throws NullPointerException, DrawRuntimeException
Construct a new LineSegment2d start two Point2d objects.- Parameters:
start
- Point2d; the start pointend
- Point2d; the end point- Throws:
NullPointerException
- when start is nullDrawRuntimeException
- when start has the exact coordinates endX, endY
-
-
Method Detail
-
getStartPoint
public Point2d getStartPoint()
Get the start point of this LineSegment.- Specified by:
getStartPoint
in interfaceLineSegment<Point2d,Ray2d,Space2d>
- Returns:
- P; the start point of the LineSegment
-
getEndPoint
public Point2d getEndPoint()
Get the end point of this LineSegment.- Specified by:
getEndPoint
in interfaceLineSegment<Point2d,Ray2d,Space2d>
- Returns:
- P; the end point of the LineSegment
-
getLength
public double getLength()
Get the length (distance from start point to end point) of this LineSegment.- Specified by:
getLength
in interfaceLineSegment<Point2d,Ray2d,Space2d>
- Returns:
- double; (distance from start point to end point) of this LineSegment
-
getPoints
public Iterator<? extends Point2d> getPoints()
Retrieve, or generate all points that make up the object.
-
size
public int size()
Retrieve the number of points that make up the object.
-
getBounds
public Bounds2d getBounds()
Retrieve the bounding rectangle of the object.- Specified by:
getBounds
in interfaceDrawable2d
- Returns:
- Bounds2d; the bounding box of the object
-
getLocationExtended
public Ray2d getLocationExtended(double position) throws DrawRuntimeException
Create a Ray on a specified point on this LineSegment.- Specified by:
getLocationExtended
in interfaceLineSegment<Point2d,Ray2d,Space2d>
- Parameters:
position
- double; the distance from the start point of this LineSegment.- Returns:
- R; a ray beginning at the specified position
- Throws:
DrawRuntimeException
- when position is NaN or infinite
-
closestPointOnSegment
public Point2d closestPointOnSegment(Point2d point)
Project a Point on this LineSegment. 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 interfaceLineSegment<Point2d,Ray2d,Space2d>
- Parameters:
point
- P; the point to project onto the segment- Returns:
- P; either the start point, or the end point of the segment or a Point2d that lies somewhere in between those two.
-
-