Struct LengthRay2
Represents a ray in 2D space — a half-line starting at a point (Position) and extending infinitely in one direction (Direction).
A ray is useful when direction and starting point matter but there is no fixed endpoint: projectile trajectories, line-of-sight checks, sensor cones, and raycasting all fit naturally into this type.
A ray can be converted to a finite LengthSegment2 via WithMagnitude(Length) and to an infinite LengthLine2 via AsLine().
public readonly record struct LengthRay2 : IEquatable<LengthRay2>
- Implements
- Inherited Members
- Extension Methods
Examples
// Ray starting at (1, 0), pointing right
var ray = new LengthRay2(LengthPoint2.FromMeters(1, 0), Direction2.FromDegrees(0));
Constructors
LengthRay2(LengthPoint2, Direction2)
Represents a ray in 2D space — a half-line starting at a point (Position) and extending infinitely in one direction (Direction).
A ray is useful when direction and starting point matter but there is no fixed endpoint: projectile trajectories, line-of-sight checks, sensor cones, and raycasting all fit naturally into this type.
A ray can be converted to a finite LengthSegment2 via WithMagnitude(Length) and to an infinite LengthLine2 via AsLine().
public LengthRay2(LengthPoint2 Position, Direction2 Direction)
Parameters
PositionLengthPoint2The origin of the ray.
DirectionDirection2The direction in which the ray extends. The ray does NOT extend in the opposite direction.
Examples
// Ray starting at (1, 0), pointing right
var ray = new LengthRay2(LengthPoint2.FromMeters(1, 0), Direction2.FromDegrees(0));
Properties
Direction
The direction in which the ray extends. The ray does NOT extend in the opposite direction.
public Direction2 Direction { get; init; }
Property Value
Position
The origin of the ray.
public LengthPoint2 Position { get; init; }
Property Value
Methods
AsLine()
Returns the infinite LengthLine2 that contains this ray.
public LengthLine2 AsLine()
Returns
- LengthLine2
A line through Position with the same Direction as this ray.
Examples
var ray = new LengthRay2(LengthPoint2.FromMeters(1, 2), Direction2.FromDegrees(45));
var line = ray.AsLine();
// line extends infinitely in both the 45° and 225° directions through (1, 2)
Contains(LengthPoint2, Length)
Returns true when point lies on this ray
within tolerance. A point is "on the ray" only if it is
in front of the origin (non-negative parameter) and within the tolerance distance.
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 ray.
Returns
Examples
var ray = new LengthRay2(LengthPoint2.Origin, Direction2.East);
ray.Contains(LengthPoint2.FromMeters(3, 0), Length.FromMeters(0.001)); // true
ray.Contains(LengthPoint2.FromMeters(-1, 0), Length.FromMeters(0.001)); // false — behind origin
DistanceTo(LengthPoint2)
Returns the shortest distance from point to this ray.
If the nearest point is behind the origin, the distance to the origin is returned.
public Length DistanceTo(LengthPoint2 point)
Parameters
pointLengthPoint2The world-space query point.
Returns
- Length
The shortest distance from the point to the ray.
Examples
var ray = new LengthRay2(LengthPoint2.Origin, Direction2.East);
ray.DistanceTo(LengthPoint2.FromMeters( 3, 4)); // 4 m
ray.DistanceTo(LengthPoint2.FromMeters(-3, 4)); // 5 m (distance to origin)
GetClosestPoint(LengthPoint2)
Returns the point on this ray nearest to point.
Projects the point onto the ray's direction; if the projection is behind the origin
(negative parameter), the origin is returned instead.
public LengthPoint2 GetClosestPoint(LengthPoint2 point)
Parameters
pointLengthPoint2The world-space query point.
Returns
- LengthPoint2
The nearest point on the ray, clamped to the origin if behind it.
Examples
var ray = new LengthRay2(LengthPoint2.Origin, Direction2.East);
ray.GetClosestPoint(LengthPoint2.FromMeters( 3, 4)); // (3, 0)
ray.GetClosestPoint(LengthPoint2.FromMeters(-3, 0)); // (0, 0) — clamped to origin
GetIntersection(LengthRay2)
Returns the intersection point of this ray with other, or
null when they do not intersect in front of both origins.
"In front" means the intersection occurs at a non-negative parameter value along each ray — i.e. the intersection is not behind either origin. Parallel rays and rays whose lines intersect only behind at least one origin both return null.
public LengthPoint2? GetIntersection(LengthRay2 other)
Parameters
otherLengthRay2The other ray to test against.
Returns
- LengthPoint2?
The intersection point, or null if the rays don't meet in front of both origins.
Examples
var a = new LengthRay2(LengthPoint2.Origin, Direction2.FromDegrees(0)); // right
var b = new LengthRay2(LengthPoint2.FromMeters(2, 2), Direction2.FromDegrees(270)); // down
LengthPoint2? pt = a.GetIntersection(b); // (2, 0)
GetPointAtMagnitude(Length)
Returns the point on the ray at the given distance from the origin.
public LengthPoint2 GetPointAtMagnitude(Length magnitude)
Parameters
magnitudeLengthThe distance along the ray from Position. Negative values travel behind the origin, which is technically valid but not typical ray usage.
Returns
Examples
var ray = new LengthRay2(LengthPoint2.FromMeters(1, 0), Direction2.FromDegrees(0));
var pt = ray.GetPointAtMagnitude(Length.FromMeters(3)); // (4, 0)
HasPoint(LengthPoint2, Length)
Returns true when point lies on the ray
within tolerance.
A point is "on the ray" when it is perpendicular-close to the underlying infinite line and its projection parameter is non-negative (i.e. the point is not behind the origin).
public bool HasPoint(LengthPoint2 point, Length tolerance)
Parameters
pointLengthPoint2The query point.
toleranceLengthMaximum perpendicular distance from the ray's line for the point to be considered "on" the ray.
Returns
Examples
var ray = new LengthRay2(LengthPoint2.Origin, Direction2.FromDegrees(0));
bool fwd = ray.HasPoint(LengthPoint2.FromMeters(3, 0), Length.FromMeters(0.001)); // true
bool back = ray.HasPoint(LengthPoint2.FromMeters(-1, 0), Length.FromMeters(0.001)); // false
WithMagnitude(Length)
Converts this ray into a finite LengthSegment2 by capping it at
magnitude from the origin.
public LengthSegment2 WithMagnitude(Length magnitude)
Parameters
magnitudeLengthThe length of the resulting segment.
Returns
- LengthSegment2
A segment from Position to
Position + Direction *.magnitude
Examples
var ray = new LengthRay2(LengthPoint2.Origin, Direction2.FromDegrees(0));
var segment = ray.WithMagnitude(Length.FromMeters(5));
// segment.Start == (0, 0)
// segment.Target == (5, 0)