Table of Contents

Class LineSegment

Namespace
NetTopologySuite.Geometries
Assembly
NetTopologySuite.dll

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.

public class LineSegment : IComparable<LineSegment>
Inheritance
LineSegment
Implements
Derived
Inherited Members

Constructors

LineSegment()

public LineSegment()

LineSegment(Coordinate, Coordinate)

Creates an instance of this class using two coordinates

public LineSegment(Coordinate p0, Coordinate p1)

Parameters

p0 Coordinate

The start-point

p1 Coordinate

The end-point

LineSegment(LineSegment)

Creates an instance of this class using another instance

public LineSegment(LineSegment ls)

Parameters

ls LineSegment

LineSegment(double, double, double, double)

Creates an instance of this class

public LineSegment(double x0, double y0, double x1, double y1)

Parameters

x0 double
y0 double
x1 double
y1 double

Properties

Angle

public double Angle { get; }

Property Value

double

The angle this segment makes with the x-axis (in radians).

IsHorizontal

Tests whether the segment is horizontal.

public bool IsHorizontal { get; }

Property Value

bool

true if the segment is horizontal.

IsVertical

Tests whether the segment is vertical.

public bool IsVertical { get; }

Property Value

bool

true if the segment is vertical.

Length

Computes the length of the line segment.

public double Length { get; }

Property Value

double

The length of the line segment.

MaxX

Gets the maximum X ordinate

public double MaxX { get; }

Property Value

double

MaxY

Gets the maximum Y ordinate

public double MaxY { get; }

Property Value

double

MidPoint

The midpoint of the segment

public Coordinate MidPoint { get; }

Property Value

Coordinate

MinX

Gets the minimum X ordinate

public double MinX { get; }

Property Value

double

MinY

Gets the minimum Y ordinate

public double MinY { get; }

Property Value

double

P0

The start-point

public Coordinate P0 { get; set; }

Property Value

Coordinate

P1

The end-point

public Coordinate P1 { get; set; }

Property Value

Coordinate

Methods

ClosestPoint(Coordinate)

Computes the closest point on this line segment to another point.

public Coordinate ClosestPoint(Coordinate p)

Parameters

p Coordinate

The point to find the closest point to.

Returns

Coordinate

A Coordinate which is the closest point on the line segment to the point p.

ClosestPoints(LineSegment)

Computes the closest points on a line segment.

public Coordinate[] ClosestPoints(LineSegment line)

Parameters

line LineSegment

Returns

Coordinate[]

A pair of Coordinates which are the closest points on the line segments.

CompareTo(LineSegment)

Compares this object with the specified object for order. Uses the standard lexicographic ordering for the points in the LineSegment.

public int CompareTo(LineSegment other)

Parameters

other LineSegment

The LineSegment with which this LineSegment is being compared.

Returns

int

A negative integer, zero, or a positive integer as this LineSegment is less than, equal to, or greater than the specified LineSegment.

Distance(Coordinate)

Computes the distance between this line segment and a point.

public double Distance(Coordinate p)

Parameters

p Coordinate

Returns

double

Distance(LineSegment)

Computes the distance between this line segment and another one.

public double Distance(LineSegment ls)

Parameters

ls LineSegment

Returns

double

DistancePerpendicular(Coordinate)

Computes the perpendicular distance between the (infinite) line defined by this line segment and a point.

public double DistancePerpendicular(Coordinate p)

Parameters

p Coordinate

Returns

double

Equals(object)

Returns true if o has the same values for its points.

public override bool Equals(object o)

Parameters

o object

A LineSegment with which to do the comparison.

Returns

bool

true if o is a LineSegment with the same values for the x and y ordinates.

EqualsTopologically(LineSegment)

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

public bool EqualsTopologically(LineSegment other)

Parameters

other LineSegment

A LineSegment with which to do the comparison.

Returns

bool

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

GetCoordinate(int)

public Coordinate GetCoordinate(int i)

Parameters

i int

Returns

Coordinate

GetHashCode()

Return HashCode.

public override int GetHashCode()

Returns

int

Intersection(LineSegment)

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.

public Coordinate Intersection(LineSegment line)

Parameters

line LineSegment

A line segment

Returns

Coordinate

An intersection point, or null if there is none.

LineIntersection(LineSegment)

Computes the intersection point of the lines defined by two segments, if there is one.

public Coordinate LineIntersection(LineSegment line)

Parameters

line LineSegment

A line segment defining a straight line

Returns

Coordinate

An intersection point, or null if there is none or an infinite number

Remarks

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.

See Also

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.

public void Normalize()

Offset(double)

Computes the LineSegment 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.

public LineSegment Offset(double offsetDistance)

Parameters

offsetDistance double

The distance the point is offset from the segment (positive is to the left, negative is to the right)

Returns

LineSegment

A line segment offset by the specified distance

Exceptions

ApplicationException

Thrown if the segment has zero length

OrientationIndex(Coordinate)

Determines the orientation index of a Coordinate relative to this segment. The orientation index is as defined in Index(Coordinate, Coordinate, Coordinate).

public int OrientationIndex(Coordinate p)

Parameters

p Coordinate

Returns

int
1if p is to the left of this segment
-1if p is to the right of this segment
0if p is collinear with this segment
"

OrientationIndex(LineSegment)

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.

public int OrientationIndex(LineSegment seg)

Parameters

seg LineSegment

The LineSegment to compare.

Returns

int

1 if seg is to the left of this segment, -1 if seg is to the right of this segment, 0 if seg is collinear to or crosses this segment.

PointAlong(double)

Computes the Coordinate that lies a given fraction along the line defined by this segment.

public Coordinate PointAlong(double segmentLengthFraction)

Parameters

segmentLengthFraction double

the fraction of the segment length along the line

Returns

Coordinate

the point at that distance

Remarks

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.

PointAlongOffset(double, double)

Computes the Coordinate that lies a given

public Coordinate PointAlongOffset(double segmentLengthFraction, double offsetDistance)

Parameters

segmentLengthFraction double

the fraction of the segment length along the line

offsetDistance double

the distance the point is offset from the segment

Returns

Coordinate

the point at that distance and offset

Remarks

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

Exceptions

ApplicationException

if the segment has zero length

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

public Coordinate Project(Coordinate p)

Parameters

p Coordinate

Returns

Coordinate

Project(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.

public LineSegment Project(LineSegment seg)

Parameters

seg LineSegment

The line segment to project.

Returns

LineSegment

The projected line segment, or null if there is no overlap.

ProjectionFactor(Coordinate)

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.
public double ProjectionFactor(Coordinate p)

Parameters

p Coordinate

The point to compute the factor for

Returns

double

The projection factor for the point

Reflect(Coordinate)

Computes the reflection of a point in the line defined by this line segment.

public Coordinate Reflect(Coordinate p)

Parameters

p Coordinate

The point to reflect

Returns

Coordinate

The reflected point

Reverse()

Reverses the direction of the line segment.

public void Reverse()

SegmentFraction(Coordinate)

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.

public double SegmentFraction(Coordinate inputPt)

Parameters

inputPt Coordinate

the point

Returns

double

the fraction along the line segment the projection of the point occurs

Remarks

Essentially, this is the ProjectionFactor(Coordinate) clamped to the range [0.0, 1.0].

SetCoordinates(Coordinate, Coordinate)

public void SetCoordinates(Coordinate p0, Coordinate p1)

Parameters

p0 Coordinate
p1 Coordinate

SetCoordinates(LineSegment)

public void SetCoordinates(LineSegment ls)

Parameters

ls LineSegment

ToGeometry(GeometryFactory)

Creates a LineString with the same coordinates as this segment

public LineString ToGeometry(GeometryFactory geomFactory)

Parameters

geomFactory GeometryFactory

the geometry factory to use

Returns

LineString

A LineString with the same geometry as this segment

ToString()

public override string ToString()

Returns

string

Operators

operator ==(LineSegment, LineSegment)

public static bool operator ==(LineSegment obj1, LineSegment obj2)

Parameters

obj1 LineSegment
obj2 LineSegment

Returns

bool

operator !=(LineSegment, LineSegment)

public static bool operator !=(LineSegment obj1, LineSegment obj2)

Parameters

obj1 LineSegment
obj2 LineSegment

Returns

bool