Skip to main content

Module ring

Module ring 

Source
Expand description

Ring (closed polygon) utility functions.

A ring is a Vec<[i64; 2]> of vertices in order (first != last, closing edge implied). All operations use exact integer arithmetic — no epsilon, no floating point.

Input contract for decompose():

  • Rings must be CCW (counter-clockwise)
  • Rings must be simple (no self-intersections)
  • Collinear vertices should be removed for cleaner decomposition

Functions§

ensure_ccw
Ensure the ring is CCW. Reverses in place if CW.
is_ccw
True if the ring is counter-clockwise (positive signed area).
is_simple
True if the ring has no self-intersections (is simple). O(n²) check — all non-adjacent edge pairs. A ring with n < 3 vertices is not simple.
normalize_ring
Normalize a ring: remove collinear vertices, ensure CCW winding. Does NOT check for simplicity (caller must ensure input is simple). Returns None if the result has fewer than 3 vertices (degenerate).
remove_collinear
Remove collinear vertices from a ring. A vertex is collinear if cross(prev, curr, next) == 0 (exact integer test). Returns a new ring with collinear vertices removed. The result may have fewer vertices, but preserves the polygon shape exactly.
rotate_ring
Rotate ring so that index start becomes index 0. Preserves winding.
signed_area_2x
Twice the signed area of the ring (positive = CCW, negative = CW). Uses the shoelace formula with i128 intermediate arithmetic. NOT the same as twice_area_fp2 in area.rs — this preserves sign for orientation detection.