ExamplesBy LevelBy TopicLearning Paths
836 Fundamental

Line Segment Intersection

Functional Programming

Tutorial Video

Text description (accessibility)

This video demonstrates the "Line Segment Intersection" functional Rust example. Difficulty level: Fundamental. Key concepts covered: Functional Programming. Testing whether two line segments intersect is a building block of computational geometry: polygon union/intersection, road network analysis, PCB trace routing, robot obstacle detection, and sweep line algorithms all reduce to repeated segment intersection tests. Key difference from OCaml: | Aspect | Rust | OCaml |

Tutorial

The Problem

Testing whether two line segments intersect is a building block of computational geometry: polygon union/intersection, road network analysis, PCB trace routing, robot obstacle detection, and sweep line algorithms all reduce to repeated segment intersection tests. The cross product orientation test determines which side of a directed line a point lies on; two segments intersect if and only if each segment's endpoints straddle the other segment's line. Degenerate cases (collinear segments, touching at endpoints) require careful handling. This is one of the most error-prone geometric primitives due to floating-point precision issues.

🎯 Learning Outcomes

  • • Implement the orientation test using cross products: left turn, right turn, or collinear
  • • Test segment intersection: two segments AB and CD intersect iff orientations disagree properly
  • • Handle the collinear overlap case: segments lie on the same line and may share a range
  • • Understand the "straddle" test: A and B on opposite sides of line CD, AND C and D on opposite sides of line AB
  • • Compute the actual intersection point using parametric line equations when intersection is confirmed
  • Code Example

    #![allow(clippy::all)]
    //! Placeholder

    Key Differences

    AspectRustOCaml
    Point representationstruct Point or (f64, f64)Record or tuple
    Orientation returni32 (0,1,2 or -1,0,1)int
    Epsilon handlingval.abs() < f64::EPSILONSame
    Collinear checkSeparate on_segment functionSame
    Integer coordsi64 for exact arithmeticint for exact
    Production librarygeo crateGg library

    OCaml Approach

    OCaml uses the same cross-product orientation test with float arithmetic. The compare function on floats respects IEEE 754. OCaml's polymorphic comparison works on (float * float) point tuples directly. The on_segment collinear check uses min/max range tests. OCaml's pipe operator |> chains the four orientation computations readably. The Gg library provides production-quality 2D geometry primitives.

    Full Source

    #![allow(clippy::all)]
    //! Placeholder

    Deep Comparison

    OCaml vs Rust: Line Segment Intersection

    Overview

    See the example.rs and example.ml files for detailed implementations.

    Key Differences

    AspectOCamlRust
    Type systemHindley-MilnerOwnership + traits
    MemoryGCZero-cost abstractions
    MutabilityExplicit refmut keyword
    Error handlingOption/ResultResult<T, E>

    See README.md for detailed comparison.

    Exercises

  • Compute the exact intersection point (as Option<Point>) when two segments do intersect.
  • Implement the sweep line algorithm using segment intersection tests to find all intersections among n segments in O((n + k) log n).
  • Handle collinear overlapping segments by returning the overlap interval as Option<(Point, Point)>.
  • Implement with integer coordinates and compare robustness vs. floating-point on near-collinear cases.
  • Use your segment intersection test to implement a simple polygon clip against a rectangular window (Cohen-Sutherland).
  • Open Source Repos