[boolean] Add basic boolean functionality (boolean() and Polygon.boolean())

This commit is contained in:
jan 2026-02-16 17:41:58 -08:00
commit 7ad59d6b89
8 changed files with 430 additions and 4 deletions

View file

@ -1,4 +1,4 @@
from typing import Any, cast, TYPE_CHECKING, Self
from typing import Any, cast, TYPE_CHECKING, Self, Literal
import copy
import functools
@ -462,3 +462,23 @@ class Polygon(Shape):
def __repr__(self) -> str:
centroid = self.vertices.mean(axis=0)
return f'<Polygon centroid {centroid} v{len(self.vertices)}>'
def boolean(
self,
other: Any,
operation: Literal['union', 'intersection', 'difference', 'xor'] = 'union',
scale: float = 1e6,
) -> list['Polygon']:
"""
Perform a boolean operation using this polygon as the subject.
Args:
other: Polygon, Iterable[Polygon], or raw vertices acting as the CLIP.
operation: 'union', 'intersection', 'difference', 'xor'.
scale: Scaling factor for integer conversion.
Returns:
A list of resulting Polygons.
"""
from ..utils.boolean import boolean
return boolean([self], other, operation=operation, scale=scale)