---
title: "Classify line segment intersections in C#"
url: https://recursiveai.org/onemath/cookbook/classify-line-segment-intersection/
description: "Distinguish a crossing, an endpoint contact and an overlap between two closed 2D line segments. Runnable C# examples with OneMath."
version: "1.0.0"
---

# 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.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneMath) · [GitHub](https://github.com/recursive-ai-oss/onemath-csharp) · [Docs](https://recursiveai.org/onemath/docs/) · [Download example (.zip)](https://recursiveai.org/onemath/cookbook/classify-line-segment-intersection/classify-line-segment-intersection.zip)

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

```shell
dotnet add package RecursiveAI.OneMath
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/onemath/cookbook/classify-line-segment-intersection/classify-line-segment-intersection.zip), 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`.

```csharp
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

```csharp
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
```

## Related recipes

[Exact geometric predicates](https://recursiveai.org/onemath/cookbook/exact-predicates/) · [Invert a matrix](https://recursiveai.org/onemath/cookbook/invert-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](https://recursiveai.org/onemath/docs/predicates/) 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](https://recursiveai.org/onemath/docs/predicates/#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.
