ExamplesBy LevelBy TopicLearning Paths
835 Intermediate

Point-in-Polygon Test

Functional Programming

Tutorial Video

Text description (accessibility)

This video demonstrates the "Point-in-Polygon Test" functional Rust example. Difficulty level: Intermediate. Key concepts covered: Functional Programming. Determining whether a point lies inside a polygon is fundamental to GIS applications, game collision detection, map interfaces (is this GPS coordinate inside this region?), and computational geometry. Key difference from OCaml: | Aspect | Rust | OCaml |

Tutorial

The Problem

Determining whether a point lies inside a polygon is fundamental to GIS applications, game collision detection, map interfaces (is this GPS coordinate inside this region?), and computational geometry. The ray casting algorithm casts a horizontal ray from the query point rightward and counts crossings with polygon edges — an odd count means inside, even means outside. The winding number algorithm counts how many times the polygon winds around the point, handling non-convex and self-intersecting polygons correctly. These tests appear in every GIS library, game physics engine, and graphics renderer.

🎯 Learning Outcomes

  • • Implement the ray casting algorithm: count horizontal ray crossings with polygon edges
  • • Handle edge cases: point exactly on edge, ray passing through vertex, horizontal edges
  • • Implement the winding number algorithm for non-convex and self-intersecting polygons
  • • Understand when each algorithm is appropriate: ray casting for simple polygons, winding number for complex
  • • Apply to batch queries: preprocess polygon for O(log n) per query vs O(n) per query
  • Code Example

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

    Key Differences

    AspectRustOCaml
    Mutationinside = !inside (bool flip)inside := not !inside
    Edge iterationfor i in 0..n with j = ifor loop or fold_left
    Previous vertexSeparate j variableSame or pair in fold state
    Float comparison!= for f64 (works here)<> for float
    Winding numberSeparate functionSame approach
    Batch queriesfn contains_batchList.map (point_in_polygon poly)

    OCaml Approach

    OCaml implements point-in-polygon with a List.fold_left over edge pairs or a for loop with Array. The condition is identical floating-point arithmetic. OCaml's ref for the mutable inside flag: let inside = ref false in ... inside := not !inside. The functional version threads the crossing count and previous vertex through fold. OCaml's List.filteri can pre-filter edges that could possibly cross the query point's y-coordinate for batch optimization.

    Full Source

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

    Deep Comparison

    OCaml vs Rust: Point In Polygon

    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

  • Implement the winding number algorithm and verify it handles self-intersecting polygons correctly where ray casting fails.
  • Add a point_on_edge test and handle the "on boundary" case by returning an enum Inside/Outside/OnBoundary.
  • Preprocess a convex polygon for O(log n) point-in-polygon using binary search on edge angles.
  • Batch test 10,000 random points against a complex polygon and measure the performance of ray casting vs. winding number.
  • Implement polygon union/intersection using multiple point-in-polygon tests (Sutherland-Hodgman algorithm).
  • Open Source Repos