Class: LineSegment

jsts.geom.LineSegment

new LineSegment(p0, p1)

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.

Parameters:
Name Type Description
p0 Coordinate
p1 Coordinate
Source:

Members

p0 :Coordinate

Type:
  • Coordinate
Source:

p1 :Coordinate

Type:
  • Coordinate
Source:

Methods

(static) midPoint(p0, p1) → {jsts.geom.Coordinate}

Computes the midpoint of a segment
Parameters:
Name Type Description
p0 jsts.geom.Coordinate
p1 jsts.geom.Coordinate
Source:
Returns:
the midpoint of the segment
Type
jsts.geom.Coordinate

angle() → {number}

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.
Source:
Returns:
the angle this segment makes with the X-axis (in radians)
Type
number

closestPoint(p) → {Coordinate}

Computes the closest point on this line segment to another point.
Parameters:
Name Type Description
p Coordinate the point to find the closest point to.
Source:
Returns:
a Coordinate which is the closest point on the line segment to the point p.
Type
Coordinate

closestPoints(line)

Computes the closest points on two line segments.
Parameters:
Name Type Description
line LineSegment the segment to find the closest point to.
Source:
Returns:

compareTo(o) → {number}

Compares this object with the specified object for order. Uses the standard lexicographic ordering for the points in the LineSegment.
Parameters:
Name Type Description
o Object the LineSegment with which this LineSegment is being compared
Source:
Returns:
a negative integer, zero, or a positive integer as this LineSegment is less than, equal to, or greater than the specified LineSegment
Type
number

distance1(ls) → {number}

Computes the distance between this line segment and another segment.
Parameters:
Name Type Description
ls jsts.geom.LineSegment
Source:
Returns:
the distance to the other segment
Type
number

distance2(p) → {number}

Computes the distance between this line segment and a given point.
Parameters:
Name Type Description
p jsts.geom.Coordinate the coordinate.
Source:
Returns:
the distance from this segment to the given point.
Type
number

distancePerpendicular(p) → {number}

Computes the perpendicular distance between the (infinite) line defined by this line segment and a point.
Parameters:
Name Type Description
p jsts.geom.Coordinate the coordinate
Source:
Returns:
the perpendicular distance between the defined line and the given point
Type
number

equals(o) → {boolean}

Returns true if other has the same values for its points.
Parameters:
Name Type Description
o Object a LineSegment with which to do the comparison.
Source:
Returns:
true if other is a LineSegment with the same values for the x and y ordinates.
Type
boolean

equalsTopo(other) → {boolean}

Returns true if other is topologically equal to this LineSegment (e.g. irrespective of orientation).
Parameters:
Name Type Description
other jsts.geom.LineSegment a LineSegment with which to do the comparison.
Source:
Returns:
true if other is a LineSegment with the same values for the x and y ordinates.
Type
boolean

getCoordinate(i) → {jsts.geom.Coordinate}

Parameters:
Name Type Description
i number
Source:
Returns:
Type
jsts.geom.Coordinate

getLength() → {number}

Computes the length of the line segment.
Source:
Returns:
the length of the line segment.
Type
number

intersection(line) → {Coordinate}

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, the RobustLineIntersector class should be used.
Parameters:
Name Type Description
line LineSegment a line segment.
Source:
See:
  • RobustLineIntersector
Returns:
an intersection point, or null if there is none.
Type
Coordinate

isHorizontal() → {boolean}

Tests whether the segment is horizontal.
Source:
Returns:
true if the segment is horizontal.
Type
boolean

isVertical() → {boolean}

Tests whether the segment is vertical.
Source:
Returns:
true if the segment is vertical.
Type
boolean

lineIntersection(line) → {jsts.geom.Coordinate}

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, the RobustLineIntersector class should be used.
Parameters:
Name Type Description
line jsts.geom.LineSegment a line segment defining an straight line with infinite extent
Source:
See:
  • RobustLineIntersector
Returns:
an intersection point, or null if there is no point of intersection or an infinite number of intersection points
Type
jsts.geom.Coordinate

midPoint() → {jsts.geom.Coordinate}

Computes the midpoint of the segment
Source:
Returns:
the midpoint of the segment
Type
jsts.geom.Coordinate

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 on Coordinate).
Source:

orientationIndex1(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:
Name Type Description
seg jsts.geom.LineSegment the LineSegment to compare
Source:
Returns:
1 if seg is to the left of this segment
-1 if seg is to the right of this segment
0 if seg has indeterminate orientation relative to this segment

orientationIndex2(p)

Determines the orientation index of a Coordinate relative to this segment. The orientation index is as defined in CGAlgorithms#computeOrientation.
Parameters:
Name Type Description
p jsts.geom.Coordinate the coordinate to compare
Source:
See:
  • CGAlgorithms#computeOrientation(Coordinate, Coordinate, Coordinate)
Returns:
  • 1 (LEFT) if p is to the left of this segment
  • -1 (RIGHT) if p is to the right of this segment
  • 0 (COLLINEAR) if p is collinear with this segment

pointAlong(segmentLengthFraction) → {jsts.geom.Coordinate}

Computes the Coordinate that lies a given fraction along the line defined by this segment. A fraction of 0.0 returns the start point of the segment; a fraction of 1.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:
Name Type Description
segmentLengthFraction number the fraction of the segment length along the line
Source:
Returns:
the point at that distance
Type
jsts.geom.Coordinate

pointAlongOffset(segmentLengthFraction, offsetDistance) → {jsts.geom.Coordinate}

Computes the Coordinate that lies a given fraction along the line defined by this segment and offset from the segment by a given distance. A fraction of 0.0 offsets from the start point of the segment; a fraction of 1.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:
Name Type Description
segmentLengthFraction number the fraction of the segment length along the line
offsetDistance number the distance the point is offset from the segment (positive is to the left, negative is to the right)
Source:
Returns:
the point at that distance and offset
Type
jsts.geom.Coordinate

project1(p) → {jsts.geom.Coordinate}

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].

Parameters:
Name Type Description
p jsts.geom.Coordinate
Source:
Returns:
Type
jsts.geom.Coordinate

project2(seg) → {jsts.geom.LineSegment}

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:
Name Type Description
seg jsts.geom.LineSegment the line segment to project
Source:
Returns:
the projected line segment, or null if there is no overlap
Type
jsts.geom.LineSegment

projectionFactor(p) → {double}

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 returned will be in the range (-inf, +inf).

Parameters:
Name Type Description
p Coordinate the point to compute the factor for.
Source:
Returns:
the projection factor for the point.
Type
double

reverse()

Reverses the direction of the line segment.
Source:

segmentFraction(inputPt) → {number}

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 clamped to the range [0.0, 1.0]. If the segment has zero length, 1.0 is returned.

Parameters:
Name Type Description
inputPt jsts.geom.Coordinate the point
Source:
Returns:
the fraction along the line segment the projection of the point occurs
Type
number

toGeometry(geomFactory) → {jsts.geom.LineString}

Creates a LineString with the same coordinates as this segment
Parameters:
Name Type Description
geomFactory jsts.geom.GeometryFactory the geometery factory to use
Source:
Returns:
a LineString with the same geometry as this segment
Type
jsts.geom.LineString