Class LineSegment
Represents a line segment defined by two Coordinate
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.
Implements
Inherited Members
Namespace: NetTopologySuite.Geometries
Assembly: NetTopologySuite.dll
Syntax
[Serializable]
public class LineSegment : IComparable<LineSegment>
Constructors
| Improve this Doc View SourceLineSegment()
Declaration
public LineSegment()
LineSegment(Coordinate, Coordinate)
Creates an instance of this class using two coordinates
Declaration
public LineSegment(Coordinate p0, Coordinate p1)
Parameters
Type | Name | Description |
---|---|---|
Coordinate | p0 | The start-point |
Coordinate | p1 | The end-point |
LineSegment(LineSegment)
Creates an instance of this class using another instance
Declaration
public LineSegment(LineSegment ls)
Parameters
Type | Name | Description |
---|---|---|
LineSegment | ls |
LineSegment(Double, Double, Double, Double)
Creates an instance of this class
Declaration
public LineSegment(double x0, double y0, double x1, double y1)
Parameters
Type | Name | Description |
---|---|---|
Double | x0 | |
Double | y0 | |
Double | x1 | |
Double | y1 |
Properties
| Improve this Doc View SourceAngle
Declaration
public double Angle { get; }
Property Value
Type | Description |
---|---|
Double | The angle this segment makes with the x-axis (in radians). |
IsHorizontal
Tests whether the segment is horizontal.
Declaration
public bool IsHorizontal { get; }
Property Value
Type | Description |
---|---|
Boolean |
|
IsVertical
Tests whether the segment is vertical.
Declaration
public bool IsVertical { get; }
Property Value
Type | Description |
---|---|
Boolean |
|
Length
Computes the length of the line segment.
Declaration
public double Length { get; }
Property Value
Type | Description |
---|---|
Double | The length of the line segment. |
MaxX
Gets the maximum X ordinate
Declaration
public double MaxX { get; }
Property Value
Type | Description |
---|---|
Double |
MaxY
Gets the maximum Y ordinate
Declaration
public double MaxY { get; }
Property Value
Type | Description |
---|---|
Double |
MidPoint
The midpoint of the segment
Declaration
public Coordinate MidPoint { get; }
Property Value
Type | Description |
---|---|
Coordinate |
MinX
Gets the minimum X ordinate
Declaration
public double MinX { get; }
Property Value
Type | Description |
---|---|
Double |
MinY
Gets the minimum Y ordinate
Declaration
public double MinY { get; }
Property Value
Type | Description |
---|---|
Double |
P0
The start-point
Declaration
public Coordinate P0 { get; set; }
Property Value
Type | Description |
---|---|
Coordinate |
P1
The end-point
Declaration
public Coordinate P1 { get; set; }
Property Value
Type | Description |
---|---|
Coordinate |
Methods
| Improve this Doc View SourceClosestPoint(Coordinate)
Computes the closest point on this line segment to another point.
Declaration
public Coordinate ClosestPoint(Coordinate p)
Parameters
Type | Name | Description |
---|---|---|
Coordinate | p | The point to find the closest point to. |
Returns
Type | Description |
---|---|
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.
Declaration
public Coordinate[] ClosestPoints(LineSegment line)
Parameters
Type | Name | Description |
---|---|---|
LineSegment | line |
Returns
Type | Description |
---|---|
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.
Declaration
public int CompareTo(LineSegment other)
Parameters
Type | Name | Description |
---|---|---|
LineSegment | other | The |
Returns
Type | Description |
---|---|
Int32 | A negative integer, zero, or a positive integer as this |
Distance(Coordinate)
Computes the distance between this line segment and a point.
Declaration
public double Distance(Coordinate p)
Parameters
Type | Name | Description |
---|---|---|
Coordinate | p |
Returns
Type | Description |
---|---|
Double |
Distance(LineSegment)
Computes the distance between this line segment and another one.
Declaration
public double Distance(LineSegment ls)
Parameters
Type | Name | Description |
---|---|---|
LineSegment | ls |
Returns
Type | Description |
---|---|
Double |
DistancePerpendicular(Coordinate)
Computes the perpendicular distance between the (infinite) line defined by this line segment and a point.
Declaration
public double DistancePerpendicular(Coordinate p)
Parameters
Type | Name | Description |
---|---|---|
Coordinate | p |
Returns
Type | Description |
---|---|
Double |
Equals(Object)
Returns true
if o
has the same values for its points.
Declaration
public override bool Equals(object o)
Parameters
Type | Name | Description |
---|---|---|
Object | o | A |
Returns
Type | Description |
---|---|
Boolean |
|
Overrides
| Improve this Doc View SourceEqualsTopologically(LineSegment)
Returns true
if other
is
topologically equal to this LineSegment (e.g. irrespective
of orientation).
Declaration
public bool EqualsTopologically(LineSegment other)
Parameters
Type | Name | Description |
---|---|---|
LineSegment | other | A |
Returns
Type | Description |
---|---|
Boolean |
|
GetCoordinate(Int32)
Declaration
public Coordinate GetCoordinate(int i)
Parameters
Type | Name | Description |
---|---|---|
Int32 | i |
Returns
Type | Description |
---|---|
Coordinate |
GetHashCode()
Return HashCode.
Declaration
public override int GetHashCode()
Returns
Type | Description |
---|---|
Int32 |
Overrides
| Improve this Doc View SourceIntersection(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.
Declaration
public Coordinate Intersection(LineSegment line)
Parameters
Type | Name | Description |
---|---|---|
LineSegment | line | A line segment |
Returns
Type | Description |
---|---|
Coordinate | An intersection point, or |
LineIntersection(LineSegment)
Computes the intersection point of the lines defined by two segments, if there is one.
Declaration
public Coordinate LineIntersection(LineSegment line)
Parameters
Type | Name | Description |
---|---|---|
LineSegment | line | A line segment defining a straight line |
Returns
Type | Description |
---|---|
Coordinate | An intersection point, or |
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
| Improve this Doc View SourceNormalize()
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.
Declaration
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.
Declaration
public LineSegment Offset(double offsetDistance)
Parameters
Type | Name | Description |
---|---|---|
Double | offsetDistance | The distance the point is offset from the segment (positive is to the left, negative is to the right) |
Returns
Type | Description |
---|---|
LineSegment | A line segment offset by the specified distance |
Exceptions
Type | Condition |
---|---|
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).
Declaration
public int OrientationIndex(Coordinate p)
Parameters
Type | Name | Description |
---|---|---|
Coordinate | p |
Returns
Type | Description | ||||||
---|---|---|---|---|---|---|---|
Int32 |
|
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.
Declaration
public int OrientationIndex(LineSegment seg)
Parameters
Type | Name | Description |
---|---|---|
LineSegment | seg | The |
Returns
Type | Description |
---|---|
Int32 | 1 if |
PointAlong(Double)
Computes the Coordinate that lies a given fraction along the line defined by this segment.
Declaration
public Coordinate PointAlong(double segmentLengthFraction)
Parameters
Type | Name | Description |
---|---|---|
Double | segmentLengthFraction | the fraction of the segment length along the line |
Returns
Type | Description |
---|---|
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
Declaration
public Coordinate PointAlongOffset(double segmentLengthFraction, double offsetDistance)
Parameters
Type | Name | Description |
---|---|---|
Double | segmentLengthFraction | the fraction of the segment length along the line |
Double | offsetDistance | the distance the point is offset from the segment |
Returns
Type | Description |
---|---|
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
Type | Condition |
---|---|
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].
Declaration
public Coordinate Project(Coordinate p)
Parameters
Type | Name | Description |
---|---|---|
Coordinate | p |
Returns
Type | Description |
---|---|
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.
Declaration
public LineSegment Project(LineSegment seg)
Parameters
Type | Name | Description |
---|---|---|
LineSegment | seg | The line segment to project. |
Returns
Type | Description |
---|---|
LineSegment | The projected line segment, or |
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.
Declaration
public double ProjectionFactor(Coordinate p)
Parameters
Type | Name | Description |
---|---|---|
Coordinate | p | The point to compute the factor for |
Returns
Type | Description |
---|---|
Double | The projection factor for the point |
Reflect(Coordinate)
Computes the reflection of a point in the line defined by this line segment.
Declaration
public Coordinate Reflect(Coordinate p)
Parameters
Type | Name | Description |
---|---|---|
Coordinate | p | The point to reflect |
Returns
Type | Description |
---|---|
Coordinate | The reflected point |
Reverse()
Reverses the direction of the line segment.
Declaration
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.
Declaration
public double SegmentFraction(Coordinate inputPt)
Parameters
Type | Name | Description |
---|---|---|
Coordinate | inputPt | the point |
Returns
Type | Description |
---|---|
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)
Declaration
public void SetCoordinates(Coordinate p0, Coordinate p1)
Parameters
Type | Name | Description |
---|---|---|
Coordinate | p0 | |
Coordinate | p1 |
SetCoordinates(LineSegment)
Declaration
public void SetCoordinates(LineSegment ls)
Parameters
Type | Name | Description |
---|---|---|
LineSegment | ls |
ToGeometry(GeometryFactory)
Creates a LineString with the same coordinates as this segment
Declaration
public LineString ToGeometry(GeometryFactory geomFactory)
Parameters
Type | Name | Description |
---|---|---|
GeometryFactory | geomFactory | the geometry factory to use |
Returns
Type | Description |
---|---|
LineString | A LineString with the same geometry as this segment |
ToString()
Declaration
public override string ToString()
Returns
Type | Description |
---|---|
String |
Overrides
Operators
| Improve this Doc View SourceEquality(LineSegment, LineSegment)
Declaration
public static bool operator ==(LineSegment obj1, LineSegment obj2)
Parameters
Type | Name | Description |
---|---|---|
LineSegment | obj1 | |
LineSegment | obj2 |
Returns
Type | Description |
---|---|
Boolean |
Inequality(LineSegment, LineSegment)
Declaration
public static bool operator !=(LineSegment obj1, LineSegment obj2)
Parameters
Type | Name | Description |
---|---|---|
LineSegment | obj1 | |
LineSegment | obj2 |
Returns
Type | Description |
---|---|
Boolean |