Recursive AI

Classify line segment intersections in C#

OneMath is a free, open-source deterministic math library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Classify two closed 2D line segments exactly, with no epsilon: distinguish crossings, endpoint contacts and overlaps.

Terminal
dotnet add package RecursiveAI.OneMath

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneMath.Predicates;
 
// Arguments: ax, ay, bx, by, cx, cy, dx, dy.
Console.WriteLine(SegmentPredicates.Classify(
    0, 0, 4, 0, 2, -1, 2, 1));
Text
Proper

Unzip the example, then dotnet run.

The full program

The full program keeps the first segment at (0, 0)–(4, 0) and changes the second segment to show all four relations. Nonfinite or out-of-range coordinates throw ArgumentOutOfRangeException.

C#
using RecursiveAI.OneMath.Predicates;
 
Console.WriteLine(SegmentPredicates.Classify(
    0, 0, 4, 0, 2, -1, 2, 1));
Console.WriteLine(SegmentPredicates.Classify(
    0, 0, 4, 0, 4, 0, 5, 1));
Console.WriteLine(SegmentPredicates.Classify(
    0, 0, 4, 0, 2, 0, 6, 0));
Console.WriteLine(SegmentPredicates.Classify(
    0, 0, 4, 0, 0, 1, 4, 1));
Text
Proper
Touching
CollinearOverlap
Disjoint

Variations

Treat a zero-length segment as a point

C#
using RecursiveAI.OneMath.Predicates;
 
// A zero-length segment is a point on the other segment.
Console.WriteLine(SegmentPredicates.Classify(
    2, 0, 2, 0, 0, 0, 4, 0));
Text
Touching

Exact geometric predicates · Invert a matrix

The short answer

Call SegmentPredicates.Classify with eight doubles: the x and y coordinates of the two endpoints of each segment. It returns a SegmentRelation, not a computed intersection coordinate. Proper means the crossing is inside both segments; Touching includes a shared endpoint or a T-junction; CollinearOverlap means they share a segment of positive length; Disjoint means they share nothing.

This distinction lets a caller decide what counts as a collision without losing information. For a closed-boundary test, every result except Disjoint is an intersection. For crossings strictly inside both segments, accept only Proper. Keep Touching separate when assembling connected edges so that an intended shared vertex is not mistaken for a crossing.

How do you check if two line segments intersect in C#?

Pass endpoints, not direction vectors. In the snippet at the top, the horizontal segment runs from (0, 0) to (4, 0). The other runs vertically from (2, −1) to (2, 1), so their crossing is interior to both. No slope division or vertical-line special case is needed in your calling code.

The classifier uses the signs of four orientation predicates to establish which side of each supporting line the opposite endpoints occupy. The predicate documentation defines those signs using the exact real values of the supplied doubles. It first checks the input domain and then classifies the segments. Reversing either segment’s endpoints, or exchanging the two segments, does not change the relation.

The return value does not contain a position. If the application needs a crossing coordinate for drawing or splitting geometry, that is a separate calculation. Do not infer a unique crossing coordinate from CollinearOverlap: that case contains infinitely many shared points.

What is the line segment intersection algorithm for collinear segments?

For collinear inputs, OneMath orders the endpoints lexicographically by x and then y. It compares the larger lower endpoint with the smaller upper endpoint. A positive-length shared interval gives CollinearOverlap, equal endpoints give Touching, and a gap gives Disjoint. This also handles vertical segments, where y determines the order after the equal x coordinates.

The third call in the download compares (0, 0)–(4, 0) with (2, 0)–(6, 0). Their shared interval is (2, 0)–(4, 0), so the result is CollinearOverlap. A zero-length input is treated as a point instead: a point on the other segment gives Touching, not overlap.

How do you make a robust line segment intersection test?

Use the classifier’s exact predicate result rather than introducing a distance epsilon into the relation test. The supported coordinate domain is zero or an absolute value from 2⁻¹⁴⁰ through 2²⁰⁰, inclusive. Every coordinate must also be finite. Classify rejects all coordinates outside that domain up front, as documented in Input requirements and range.

Exact classification describes the doubles you supplied; it cannot restore precision lost before the call. If a measurement tolerance should make nearby endpoints count as identical, define that input policy separately. Snapping coordinates changes the geometry being classified, while predicate exactness determines the relation of that geometry without a tolerance threshold.

All onemath recipes