Table of Contents

Struct LengthSegment2

Namespace
Thunder.UnitsNET.Vectors.Geometry
Assembly
Thunder.UnitsNET.Vectors.Geometry.dll

Represents a directed line segment in 2D space defined by a start point and an end point.

A segment is the simplest linear primitive: it has a finite length, a direction, and two fixed endpoints. It is the building block for polygon edges, pathfinding corridors, and collision detection.

All properties are computed once at construction and stored. The type is immutable — use the secondary constructor or FromMeters(double, double, double, double) to create segments with different values.

public readonly record struct LengthSegment2 : IEquatable<LengthSegment2>
Implements
Inherited Members
Extension Methods

Examples

var a = LengthPoint2.FromMeters(0, 0);
var b = LengthPoint2.FromMeters(3, 4);
var segment = new LengthSegment2(a, b);
// segment.Magnitude ≈ 5 m, segment.Direction ≈ 53.13°

Constructors

LengthSegment2(LengthPoint2, Direction2, Length)

Creates a LengthSegment2 from a start position, a direction, and a magnitude. The end is computed as start + direction.Direction * magnitude.

public LengthSegment2(LengthPoint2 start, Direction2 direction, Length magnitude)

Parameters

start LengthPoint2

The start position.

direction Direction2

The direction the segment points.

magnitude Length

The length of the segment. Must be non-negative.

Examples

var seg = new LengthSegment2(LengthPoint2.Origin, Direction2.FromDegrees(45), Length.FromMeters(Math.Sqrt(2)));
// seg.End ≈ (1, 1)

LengthSegment2(LengthPoint2, LengthPoint2)

Creates a LengthSegment2 from two positions. All derived properties (Displacement, Magnitude, Direction) are computed during construction.

public LengthSegment2(LengthPoint2 start, LengthPoint2 end)

Parameters

start LengthPoint2

The start position of the segment.

end LengthPoint2

The end position of the segment.

Examples

var seg = new LengthSegment2(LengthPoint2.FromMeters(0, 0), LengthPoint2.FromMeters(4, 0));
// seg.Magnitude == 4 m, seg.Direction.Angle == 0°

Properties

Direction

Gets the direction the segment points — a unit vector from Start towards End. Returns null when the segment has zero length.

public Direction2? Direction { get; init; }

Property Value

Direction2?

Displacement

Gets the displacement vector from Start to End. Equivalent to End - Start.

public LengthVector2 Displacement { get; init; }

Property Value

LengthVector2

End

Gets the end position of the segment.

public LengthPoint2 End { get; init; }

Property Value

LengthPoint2

Magnitude

Gets the Euclidean length of the segment (the distance from Start to End).

public Length Magnitude { get; init; }

Property Value

Length

Start

Gets the start position of the segment.

public LengthPoint2 Start { get; init; }

Property Value

LengthPoint2

Methods

AngleBetween(LengthSegment2)

Returns the smallest angle between this segment and other, in [0°, 90°].

Uses bidirectional comparison — anti-parallel segments (one pointing left, one right) return 0°, not 180°, because they represent the same undirected line.

public Angle AngleBetween(LengthSegment2 other)

Parameters

other LengthSegment2

The segment to compare against.

Returns

Angle

The bidirectional angle between the two segments, in [0°, 90°].

Examples

var horiz   = LengthSegment2.FromMeters(0, 0, 4, 0);
var vertical = LengthSegment2.FromMeters(0, 0, 0, 4);
Angle a = horiz.AngleBetween(vertical); // 90°

AsLine()

Converts this segment to an infinite LengthLine2 passing through Start in the direction of Direction.

public LengthLine2 AsLine()

Returns

LengthLine2

The infinite line that contains this segment.

Examples

var seg  = LengthSegment2.FromMeters(0, 0, 4, 0);
var line = seg.AsLine(); // horizontal line through origin

AsRayFromEndToStart()

Returns a LengthRay2 starting at End and pointing back towards Start — the reverse ray along the segment.

public LengthRay2 AsRayFromEndToStart()

Returns

LengthRay2

A ray from End in the direction opposite to Direction.

Examples

var seg = LengthSegment2.FromMeters(0, 0, 4, 0);
var ray = seg.AsRayFromEndToStart(); // ray from (4,0) pointing left

AsRayFromStartToEnd()

Returns a LengthRay2 starting at Start and pointing towards End — the forward ray along the segment.

public LengthRay2 AsRayFromStartToEnd()

Returns

LengthRay2

A ray from Start in the direction of Direction.

Examples

var seg = LengthSegment2.FromMeters(0, 0, 4, 0);
var ray = seg.AsRayFromStartToEnd(); // ray from (0,0) pointing right

Contains(LengthPoint2, Length)

Determines whether point lies on this segment within tolerance.

A point is considered to be on the segment when the sum of its distances to Start and End equals Magnitude to within tolerance. This avoids the numerical sensitivity of projecting onto a unit vector.

public bool Contains(LengthPoint2 point, Length tolerance)

Parameters

point LengthPoint2

The point to test.

tolerance Length

The maximum allowable difference between dist(point, Start) + dist(point, End) and Magnitude. Larger values accept points further off the segment.

Returns

bool

true if point is on the segment within tolerance; false otherwise.

Examples

var seg = LengthSegment2.FromMeters(0, 0, 4, 0);
bool onSeg = seg.Contains(LengthPoint2.FromMeters(2, 0), Length.FromMeters(0.001)); // true
bool past  = seg.Contains(LengthPoint2.FromMeters(5, 0), Length.FromMeters(0.001)); // false

FromMeters(double, double, double, double)

Creates a LengthSegment2 from raw metre coordinates, avoiding the verbosity of constructing LengthVector2 values explicitly.

public static LengthSegment2 FromMeters(double x1, double y1, double x2, double y2)

Parameters

x1 double

The X component of the start point, in metres.

y1 double

The Y component of the start point, in metres.

x2 double

The X component of the end point, in metres.

y2 double

The Y component of the end point, in metres.

Returns

LengthSegment2

A LengthSegment2 between the two specified points.

Examples

var seg = LengthSegment2.FromMeters(0, 0, 3, 4); // 5 m segment

GetClosestPoint(LengthPoint2)

Returns the point on this segment that is closest to point.

The result is the perpendicular projection of point onto the infinite line through the segment, clamped to the segment's endpoints. If point is beyond End, End is returned; if it is before Start, Start is returned.

public LengthPoint2 GetClosestPoint(LengthPoint2 point)

Parameters

point LengthPoint2

The query point.

Returns

LengthPoint2

The nearest point on the segment to point.

Examples

var seg = LengthSegment2.FromMeters(0, 0, 4, 0);
LengthPoint2 closest = seg.GetClosestPoint(LengthPoint2.FromMeters(2, 5)); // (2, 0)

GetMidPoint()

Returns the midpoint of the segment — the point equidistant from both Start and End.

public LengthPoint2 GetMidPoint()

Returns

LengthPoint2

The midpoint position.

Examples

var seg = new LengthSegment2(LengthPoint2.FromMeters(0, 0), LengthPoint2.FromMeters(4, 0));
LengthPoint2 mid = seg.GetMidPoint(); // (2, 0)

Intersects(LengthSegment2)

Determines whether this segment intersects other.

Two segments intersect when the infinite lines they define cross at a point that lies within both segments' bounds. Anti-parallel or collinear segments that do not overlap return false.

public bool Intersects(LengthSegment2 other)

Parameters

other LengthSegment2

The segment to test for intersection.

Returns

bool

true if the two segments intersect; false if they are parallel, non-overlapping, or only intersect when extended.

Examples

var a = LengthSegment2.FromMeters(0, 0, 4, 4);
var b = LengthSegment2.FromMeters(0, 4, 4, 0);
bool cross = a.Intersects(b); // true — they cross at (2, 2)
v0.7.0 ▼