Struct LengthLine2
Represents an infinite line in 2D space, defined by any point on the line and its direction.
Unlike a LengthSegment2, a line extends infinitely in both directions from its defining point. It is useful for intersection queries, perpendicular projections, and computing whether a point is to the left or right of a boundary.
public readonly record struct LengthLine2 : IEquatable<LengthLine2>
- Implements
- Inherited Members
- Extension Methods
Examples
// Horizontal line through the origin
var line = new LengthLine2(LengthPoint2.Origin, Direction2.FromDegrees(0));
Constructors
LengthLine2(LengthPoint2, Direction2)
Represents an infinite line in 2D space, defined by any point on the line and its direction.
Unlike a LengthSegment2, a line extends infinitely in both directions from its defining point. It is useful for intersection queries, perpendicular projections, and computing whether a point is to the left or right of a boundary.
public LengthLine2(LengthPoint2 Point, Direction2 Direction)
Parameters
PointLengthPoint2Any point that lies on the line.
DirectionDirection2The direction the line points. The line also extends in the opposite direction.
Examples
// Horizontal line through the origin
var line = new LengthLine2(LengthPoint2.Origin, Direction2.FromDegrees(0));
LengthLine2(LengthPoint2, LengthPoint2)
Creates a LengthLine2 passing through two distinct points.
The direction is computed from point1 towards
point2.
public LengthLine2(LengthPoint2 point1, LengthPoint2 point2)
Parameters
point1LengthPoint2The first point on the line.
point2LengthPoint2The second point on the line. Must be distinct from
point1.
Examples
var line = new LengthLine2(LengthPoint2.FromMeters(0, 0), LengthPoint2.FromMeters(4, 0));
// line.Direction.Angle ≈ 0° (rightward)
Properties
Direction
The direction the line points. The line also extends in the opposite direction.
public Direction2 Direction { get; init; }
Property Value
Point
Any point that lies on the line.
public LengthPoint2 Point { get; init; }
Property Value
Methods
Contains(LengthPoint2, Length)
Returns true when point lies on this infinite line
within tolerance (perpendicular distance ≤ tolerance).
This delegates to HasPoint(LengthPoint2, Length).
public bool Contains(LengthPoint2 point, Length tolerance)
Parameters
pointLengthPoint2The point to test.
toleranceLengthMaximum perpendicular distance for the point to be "on" the line.
Returns
Examples
var line = new LengthLine2(LengthPoint2.Origin, Direction2.East);
line.Contains(LengthPoint2.FromMeters(5, 0), Length.FromMeters(0.001)); // true
line.Contains(LengthPoint2.FromMeters(5, 0.5), Length.FromMeters(0.001)); // false
DistanceTo(LengthPoint2)
Returns the perpendicular distance from point to this infinite line.
public Length DistanceTo(LengthPoint2 point)
Parameters
pointLengthPoint2The world-space query point.
Returns
- Length
The perpendicular distance from the point to the line.
Examples
var line = new LengthLine2(LengthPoint2.Origin, Direction2.East);
line.DistanceTo(LengthPoint2.FromMeters(3, 4)); // 4 m
FindClosestPositionToPoint(LengthPoint2)
Returns the point on this line that is closest to point —
i.e. the perpendicular projection of point onto the line.
This is computed by projecting the vector from Point to
point onto the line's direction and adding the result back to
Point. Because the line is infinite, the result is never clamped.
public LengthPoint2 FindClosestPositionToPoint(LengthPoint2 point)
Parameters
pointLengthPoint2The query point.
Returns
- LengthPoint2
The nearest point on the infinite line to
point.
Examples
var line = new LengthLine2(LengthPoint2.FromMeters(0, 1), Direction2.FromDegrees(0)); // y=1
var result = line.FindClosestPositionToPoint(LengthPoint2.FromMeters(3, 5));
// result ≈ (3, 1) — projects straight down to the line
GetClosestPoint(LengthPoint2)
Returns the point on this infinite line nearest to point —
the foot of the perpendicular from the point to the line.
This delegates to FindClosestPositionToPoint(LengthPoint2).
public LengthPoint2 GetClosestPoint(LengthPoint2 point)
Parameters
pointLengthPoint2The world-space query point.
Returns
- LengthPoint2
The nearest point on the infinite line.
Examples
var line = new LengthLine2(LengthPoint2.Origin, Direction2.East);
var closest = line.GetClosestPoint(LengthPoint2.FromMeters(3, 4)); // (3, 0)
GetIntersection(LengthLine2)
Returns the intersection point of this line with other, or
null when the lines are parallel (or coincident).
Uses the determinant method: for lines with direction vectors d₁ and d₂, the
parameter is t = cross(delta, d₂) / cross(d₁, d₂), where
delta = other.Point − this.Point. Parallel lines have
cross(d₁, d₂) ≈ 0.
public LengthPoint2? GetIntersection(LengthLine2 other)
Parameters
otherLengthLine2The other infinite line to intersect with.
Returns
- LengthPoint2?
The intersection LengthPoint2, or null if the lines are parallel.
Examples
var a = new LengthLine2(LengthPoint2.Origin, Direction2.FromDegrees(0)); // y=0
var b = new LengthLine2(LengthPoint2.FromMeters(2, 2), Direction2.FromDegrees(90)); // x=2
LengthPoint2? pt = a.GetIntersection(b); // (2, 0)
GetPerpendicularLineAt(LengthPoint2)
Returns a new LengthLine2 that passes through
position and is perpendicular to this line.
public LengthLine2 GetPerpendicularLineAt(LengthPoint2 position)
Parameters
positionLengthPoint2The point through which the perpendicular line passes.
Returns
- LengthLine2
A LengthLine2 through
position, rotated 90° counter-clockwise from this line's direction.
Examples
var horiz = new LengthLine2(LengthPoint2.Origin, Direction2.FromDegrees(0));
var perp = horiz.GetPerpendicularLineAt(LengthPoint2.FromMeters(3, 0));
// perp.Direction.Angle ≈ 90° (vertical through x=3)
HasPoint(LengthPoint2, Length)
Returns true when point lies on this line
within tolerance.
Checks whether the distance from point to its projection on
the line is less than or equal to tolerance.
public bool HasPoint(LengthPoint2 point, Length tolerance)
Parameters
pointLengthPoint2The point to test.
toleranceLengthThe maximum perpendicular distance from the line for a point to be considered "on" the line.
Returns
Examples
var line = new LengthLine2(LengthPoint2.Origin, Direction2.FromDegrees(0));
bool on = line.HasPoint(LengthPoint2.FromMeters(5, 0), Length.FromMeters(0.001)); // true
bool off = line.HasPoint(LengthPoint2.FromMeters(5, 0.5), Length.FromMeters(0.001)); // false