Class LineSegment
- java.lang.Object
-
- org.locationtech.jts.geom.LineSegment
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Comparable
public class LineSegment extends java.lang.Object implements java.lang.Comparable, java.io.Serializable
Represents a line segment defined by twoCoordinate
s. Provides methods to compute various geometric properties and relationships of line segments.This class is designed to be easily mutable (to the extent of having its contained points public). This supports a common pattern of reusing a single LineSegment object as a way of computing segment properties on the segments defined by arrays or lists of
Coordinate
s.- Version:
- 1.7
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description Coordinate
p0
Coordinate
p1
-
Constructor Summary
Constructors Constructor Description LineSegment()
LineSegment(double x0, double y0, double x1, double y1)
LineSegment(Coordinate p0, Coordinate p1)
LineSegment(LineSegment ls)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description double
angle()
Computes the angle that the vector defined by this segment makes with the X-axis.Coordinate
closestPoint(Coordinate p)
Computes the closest point on this line segment to another point.Coordinate[]
closestPoints(LineSegment line)
Computes the closest points on two line segments.int
compareTo(java.lang.Object o)
Compares this object with the specified object for order.double
distance(Coordinate p)
Computes the distance between this line segment and a given point.double
distance(LineSegment ls)
Computes the distance between this line segment and another segment.double
distancePerpendicular(Coordinate p)
Computes the perpendicular distance between the (infinite) line defined by this line segment and a point.boolean
equals(java.lang.Object o)
Returnstrue
ifother
has the same values for its points.boolean
equalsTopo(LineSegment other)
Returnstrue
ifother
is topologically equal to this LineSegment (e.g.Coordinate
getCoordinate(int i)
double
getLength()
Computes the length of the line segment.int
hashCode()
Gets a hashcode for this object.Coordinate
intersection(LineSegment line)
Computes an intersection point between two line segments, if there is one.boolean
isHorizontal()
Tests whether the segment is horizontal.boolean
isVertical()
Tests whether the segment is vertical.Coordinate
lineIntersection(LineSegment line)
Computes the intersection point of the lines of infinite extent defined by two line segments (if there is one).double
maxX()
Gets the maximum X ordinate.double
maxY()
Gets the maximum Y ordinate.Coordinate
midPoint()
Computes the midpoint of the segmentstatic Coordinate
midPoint(Coordinate p0, Coordinate p1)
Computes the midpoint of a segmentdouble
minX()
Gets the minimum X ordinate.double
minY()
Gets the minimum Y ordinate.void
normalize()
Puts the line segment into a normalized form.LineSegment
offset(double offsetDistance)
Computes theLineSegment
that is offset from the segment by a given distance.int
OLDhashCode()
int
orientationIndex(Coordinate p)
Determines the orientation index of aCoordinate
relative to this segment.int
orientationIndex(LineSegment seg)
Determines the orientation of a LineSegment relative to this segment.Coordinate
pointAlong(double segmentLengthFraction)
Computes theCoordinate
that lies a given fraction along the line defined by this segment.Coordinate
pointAlongOffset(double segmentLengthFraction, double offsetDistance)
Computes theCoordinate
that lies a given fraction along the line defined by this segment and offset from the segment by a given distance.Coordinate
project(Coordinate p)
Compute the projection of a point onto the line determined by this line segment.LineSegment
project(LineSegment seg)
Project a line segment onto this line segment and return the resulting line segment.double
projectionFactor(Coordinate p)
Computes the Projection Factor for the projection of the point p onto this LineSegment.Coordinate
reflect(Coordinate p)
Computes the reflection of a point in the line defined by this line segment.void
reverse()
Reverses the direction of the line segment.double
segmentFraction(Coordinate inputPt)
Computes the fraction of distance (in [0.0, 1.0]) that the projection of a point occurs along this line segment.void
setCoordinates(Coordinate p0, Coordinate p1)
void
setCoordinates(LineSegment ls)
LineString
toGeometry(GeometryFactory geomFactory)
Creates a LineString with the same coordinates as this segmentjava.lang.String
toString()
-
-
-
Field Detail
-
p0
public Coordinate p0
-
p1
public Coordinate p1
-
-
Constructor Detail
-
LineSegment
public LineSegment(Coordinate p0, Coordinate p1)
-
LineSegment
public LineSegment(double x0, double y0, double x1, double y1)
-
LineSegment
public LineSegment(LineSegment ls)
-
LineSegment
public LineSegment()
-
-
Method Detail
-
getCoordinate
public Coordinate getCoordinate(int i)
-
setCoordinates
public void setCoordinates(LineSegment ls)
-
setCoordinates
public void setCoordinates(Coordinate p0, Coordinate p1)
-
minX
public double minX()
Gets the minimum X ordinate.- Returns:
- the minimum X ordinate
-
maxX
public double maxX()
Gets the maximum X ordinate.- Returns:
- the maximum X ordinate
-
minY
public double minY()
Gets the minimum Y ordinate.- Returns:
- the minimum Y ordinate
-
maxY
public double maxY()
Gets the maximum Y ordinate.- Returns:
- the maximum Y ordinate
-
getLength
public double getLength()
Computes the length of the line segment.- Returns:
- the length of the line segment
-
isHorizontal
public boolean isHorizontal()
Tests whether the segment is horizontal.- Returns:
true
if the segment is horizontal
-
isVertical
public boolean isVertical()
Tests whether the segment is vertical.- Returns:
true
if the segment is vertical
-
orientationIndex
public int orientationIndex(LineSegment seg)
Determines the orientation of a LineSegment relative to this segment. The concept of orientation is specified as follows: Given two line segments A and L,- A is to the left of a segment L if A lies wholly in the closed half-plane lying to the left of L
- A is to the right of a segment L if A lies wholly in the closed half-plane lying to the right of L
- otherwise, A has indeterminate orientation relative to L. This happens if A is collinear with L or if A crosses the line determined by L.
- Parameters:
seg
- the LineSegment to compare- Returns:
- 1 if
seg
is to the left of this segment
-
orientationIndex
public int orientationIndex(Coordinate p)
Determines the orientation index of aCoordinate
relative to this segment. The orientation index is as defined inOrientation.index(Coordinate, Coordinate, Coordinate)
.- Parameters:
p
- the coordinate to compare- Returns:
- 1 (LEFT) if
p
is to the left of this segment - See Also:
Orientation.index(Coordinate, Coordinate, Coordinate)
-
reverse
public void reverse()
Reverses the direction of the line segment.
-
normalize
public void normalize()
Puts the line segment into a normalized form. This is useful for using line segments in maps and indexes when topological equality rather than exact equality is desired. A segment in normalized form has the first point smaller than the second (according to the standard ordering onCoordinate
).
-
angle
public double angle()
Computes the angle that the vector defined by this segment makes with the X-axis. The angle will be in the range [ -PI, PI ] radians.- Returns:
- the angle this segment makes with the X-axis (in radians)
-
midPoint
public Coordinate midPoint()
Computes the midpoint of the segment- Returns:
- the midpoint of the segment
-
midPoint
public static Coordinate midPoint(Coordinate p0, Coordinate p1)
Computes the midpoint of a segment- Returns:
- the midpoint of the segment
-
distance
public double distance(LineSegment ls)
Computes the distance between this line segment and another segment.- Returns:
- the distance to the other segment
-
distance
public double distance(Coordinate p)
Computes the distance between this line segment and a given point.- Returns:
- the distance from this segment to the given point
-
distancePerpendicular
public double distancePerpendicular(Coordinate p)
Computes the perpendicular distance between the (infinite) line defined by this line segment and a point.- Returns:
- the perpendicular distance between the defined line and the given point
-
pointAlong
public Coordinate pointAlong(double segmentLengthFraction)
Computes theCoordinate
that lies a given fraction along the line defined by this segment. A fraction of0.0
returns the start point of the segment; a fraction of1.0
returns the end point of the segment. If the fraction is < 0.0 or > 1.0 the point returned will lie before the start or beyond the end of the segment.- Parameters:
segmentLengthFraction
- the fraction of the segment length along the line- Returns:
- the point at that distance
-
pointAlongOffset
public Coordinate pointAlongOffset(double segmentLengthFraction, double offsetDistance)
Computes theCoordinate
that lies a given fraction along the line defined by this segment and offset from the segment by a given distance. A fraction of0.0
offsets from the start point of the segment; a fraction of1.0
offsets from the end point of the segment. The computed point is offset to the left of the line if the offset distance is positive, to the right if negative.- Parameters:
segmentLengthFraction
- the fraction of the segment length along the lineoffsetDistance
- the distance the point is offset from the segment (positive is to the left, negative is to the right)- Returns:
- the point at that distance and offset
- Throws:
java.lang.IllegalStateException
- if the segment has zero length
-
projectionFactor
public double projectionFactor(Coordinate p)
Computes the Projection Factor for the projection of the point p onto this LineSegment. The Projection Factor is the constant r by which the vector for this segment must be multiplied to equal the vector for the projection of p on the line defined by this segment.The projection factor will lie in the range (-inf, +inf), or be
NaN
if the line segment has zero length..- Parameters:
p
- the point to compute the factor for- Returns:
- the projection factor for the point
-
segmentFraction
public double segmentFraction(Coordinate inputPt)
Computes the fraction of distance (in [0.0, 1.0]) that the projection of a point occurs along this line segment. If the point is beyond either ends of the line segment, the closest fractional value (0.0 or 1.0) is returned.Essentially, this is the
projectionFactor(org.locationtech.jts.geom.Coordinate)
clamped to the range [0.0, 1.0]. If the segment has zero length, 1.0 is returned.- Parameters:
inputPt
- the point- Returns:
- the fraction along the line segment the projection of the point occurs
-
project
public Coordinate project(Coordinate p)
Compute the projection of a point onto the line determined by this line segment.Note that the projected point may lie outside the line segment. If this is the case, the projection factor will lie outside the range [0.0, 1.0].
-
project
public LineSegment project(LineSegment seg)
Project a line segment onto this line segment and return the resulting line segment. The returned line segment will be a subset of the target line line segment. This subset may be null, if the segments are oriented in such a way that there is no projection.Note that the returned line may have zero length (i.e. the same endpoints). This can happen for instance if the lines are perpendicular to one another.
- Parameters:
seg
- the line segment to project- Returns:
- the projected line segment, or
null
if there is no overlap
-
offset
public LineSegment offset(double offsetDistance)
Computes theLineSegment
that is offset from the segment by a given distance. The computed segment is offset to the left of the line if the offset distance is positive, to the right if negative.- Parameters:
offsetDistance
- the distance the point is offset from the segment (positive is to the left, negative is to the right)- Returns:
- a line segment offset by the specified distance
- Throws:
java.lang.IllegalStateException
- if the segment has zero length
-
reflect
public Coordinate reflect(Coordinate p)
Computes the reflection of a point in the line defined by this line segment.- Parameters:
p
- the point to reflect- Returns:
- the reflected point
-
closestPoint
public Coordinate closestPoint(Coordinate p)
Computes the closest point on this line segment to another point.- Parameters:
p
- the point to find the closest point to- Returns:
- a Coordinate which is the closest point on the line segment to the point p
-
closestPoints
public Coordinate[] closestPoints(LineSegment line)
Computes the closest points on two line segments.- Parameters:
line
- the segment to find the closest point to- Returns:
- a pair of Coordinates which are the closest points on the line segments
-
intersection
public Coordinate intersection(LineSegment line)
Computes an intersection point between two line segments, if there is one. There may be 0, 1 or many intersection points between two segments. If there are 0, null is returned. If there is 1 or more, exactly one of them is returned (chosen at the discretion of the algorithm). If more information is required about the details of the intersection, theRobustLineIntersector
class should be used.- Parameters:
line
- a line segment- Returns:
- an intersection point, or
null
if there is none - See Also:
RobustLineIntersector
-
lineIntersection
public Coordinate lineIntersection(LineSegment line)
Computes the intersection point of the lines of infinite extent defined by two line segments (if there is one). There may be 0, 1 or an infinite number of intersection points between two lines. If there is a unique intersection point, it is returned. Otherwise, null is returned. If more information is required about the details of the intersection, theRobustLineIntersector
class should be used.- Parameters:
line
- a line segment defining an straight line with infinite extent- Returns:
- an intersection point,
or
null
if there is no point of intersection or an infinite number of intersection points - See Also:
RobustLineIntersector
-
toGeometry
public LineString toGeometry(GeometryFactory geomFactory)
Creates a LineString with the same coordinates as this segment- Parameters:
geomFactory
- the geometry factory to use- Returns:
- a LineString with the same geometry as this segment
-
equals
public boolean equals(java.lang.Object o)
Returnstrue
ifother
has the same values for its points.- Overrides:
equals
in classjava.lang.Object
- Parameters:
o
- aLineSegment
with which to do the comparison.- Returns:
true
ifother
is aLineSegment
with the same values for the x and y ordinates.
-
hashCode
public int hashCode()
Gets a hashcode for this object.- Overrides:
hashCode
in classjava.lang.Object
- Returns:
- a hashcode for this object
-
OLDhashCode
public int OLDhashCode()
-
compareTo
public int compareTo(java.lang.Object o)
Compares this object with the specified object for order. Uses the standard lexicographic ordering for the points in the LineSegment.- Specified by:
compareTo
in interfacejava.lang.Comparable
- Parameters:
o
- theLineSegment
with which thisLineSegment
is being compared- Returns:
- a negative integer, zero, or a positive integer as this
LineSegment
is less than, equal to, or greater than the specifiedLineSegment
-
equalsTopo
public boolean equalsTopo(LineSegment other)
Returnstrue
ifother
is topologically equal to this LineSegment (e.g. irrespective of orientation).- Parameters:
other
- aLineSegment
with which to do the comparison.- Returns:
true
ifother
is aLineSegment
with the same values for the x and y ordinates.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-