Skip to main content

difference_polygon

Function difference_polygon 

Source
pub fn difference_polygon(
    poly1: &Polygon,
    poly2: &Polygon,
) -> Result<Vec<Polygon>>
Expand description

Computes the difference of two polygons (poly1 - poly2)

Returns the portion of poly1 that does not overlap with poly2. Handles:

  • Disjoint polygons (returns poly1)
  • poly2 contains poly1 (returns empty)
  • poly1 contains poly2 (returns poly1 with poly2 as hole)
  • Partial overlap (simplified result)
  • Existing interior rings (holes) in both polygons

§Interior Ring Handling

  • Holes in poly1: Preserved unless they are completely covered by poly2
  • Holes in poly2: If poly2 is inside poly1, poly2’s holes become new polygon regions (since subtracting a hole means keeping that area)
  • New holes: Created when poly2 is entirely contained within poly1

§Arguments

  • poly1 - The polygon to subtract from
  • poly2 - The polygon to subtract

§Returns

Vector of polygons representing the difference

§Errors

Returns error if polygons are invalid