com.vividsolutions.jts.geom
Class LineSegment

java.lang.Object
  extended bycom.vividsolutions.jts.geom.LineSegment
All Implemented Interfaces:
java.lang.Comparable, java.io.Serializable

public class LineSegment
extends java.lang.Object
implements java.lang.Comparable, java.io.Serializable

Represents a line segment defined by two Coordinates. 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 Coordinates.

Version:
1.4.0
See Also:
Serialized Form

Field Summary
 Coordinate p0
           
 Coordinate p1
           
 
Constructor Summary
LineSegment()
           
LineSegment(Coordinate p0, Coordinate p1)
           
LineSegment(LineSegment ls)
           
 
Method Summary
 double angle()
           
 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 point.
 double distance(LineSegment ls)
          Computes the distance between this line segment and another one.
 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)
          Returns true if other has the same values for its points.
 boolean equalsTopo(LineSegment other)
          Returns true if other is topologically equal to this LineSegment (e.g.
 Coordinate getCoordinate(int i)
           
 double getLength()
          Computes the length of the line segment.
 Coordinate intersection(LineSegment line)
          Computes an intersection point between two segments, if there is one.
 boolean isHorizontal()
          Tests whether the segment is horizontal.
 boolean isVertical()
          Tests whether the segment is vertical.
 void normalize()
          Puts the line segment into a normalized form.
 int orientationIndex(LineSegment seg)
          Determines the orientation of a LineSegment relative to this segment.
 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)
          Compute the projection factor for the projection of the point p onto this LineSegment.
 void reverse()
          Reverses the direction of the line segment.
 void setCoordinates(Coordinate p0, Coordinate p1)
           
 void setCoordinates(LineSegment ls)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

p0

public Coordinate p0

p1

public Coordinate p1
Constructor Detail

LineSegment

public LineSegment(Coordinate p0,
                   Coordinate p1)

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)

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,

Parameters:
seg - the LineSegment to compare
Returns:
1 if seg is to the left of this segment

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.


angle

public double angle()
Returns:
the angle this segment makes with the x-axis (in radians)

distance

public double distance(LineSegment ls)
Computes the distance between this line segment and another one.


distance

public double distance(Coordinate p)
Computes the distance between this line segment and a point.


distancePerpendicular

public double distancePerpendicular(Coordinate p)
Computes the perpendicular distance between the (infinite) line defined by this line segment and a point.


projectionFactor

public double projectionFactor(Coordinate p)
Compute the projection factor for the projection of the point p onto this LineSegment. The projection factor is the constant k by which the vector for this segment must be multiplied to equal the vector for the projection of p.


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

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.

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 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, a single one is returned (chosen at the discretion of the algorithm). If more information is required about the details of the intersection, the RobustLineIntersector class should be used.

Parameters:
line -
Returns:
an intersection point, or null if there is none

equals

public boolean equals(java.lang.Object o)
Returns true if other has the same values for its points.

Returns:
true if other is a LineSegment with the same values for the x and y ordinates.

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 interface java.lang.Comparable
Parameters:
o - the LineSegment with which this LineSegment is being compared
Returns:
a negative integer, zero, or a positive integer as this LineSegment is less than, equal to, or greater than the specified LineSegment

equalsTopo

public boolean equalsTopo(LineSegment other)
Returns true if other is topologically equal to this LineSegment (e.g. irrespective of orientation).

Parameters:
other - a LineSegment with which to do the comparison.
Returns:
true if other is a LineSegment with the same values for the x and y ordinates.

toString

public java.lang.String toString()