raygeo.geo.shape.polygon
Polygon manipulation functions.
Provides area (signed and unsigned), perimeter, bounding box, centroid, convex hull, point-in-polygon containment test, edge extraction, convexity testing, boolean operations (union, intersection, difference), offset (inflate or deflate), cleaning (removing near-duplicate vertices), normalisation (outer CCW / inner CW winding order), and transformations (translate, rotate, scale, flip). All functions accept list-of-tuples input; many also provide numpy array variants.
Functions
clean_polygon()
clean_polygon(polygon: Sequence[Point], tolerance: Optional[float] = None) -> Optional[Polygon]
Clean a polygon by removing near-duplicate points.
Returns: Cleaned polygon or None.
| Parameter | Type | Description |
|---|---|---|
polygon | Sequence[Point] | Input polygon as (x, y) points. |
tolerance | Optional[float] = None | Distance tolerance for deduplication. |
| Returns | Optional[Polygon] |
flip_polygon()
flip_polygon(polygon: Sequence[Point], flip_h: bool, flip_v: bool) -> Polygon
Flip a polygon horizontally and/or vertically.
Returns: Flipped polygon.
| Parameter | Type | Description |
|---|---|---|
polygon | Sequence[Point] | Polygon as (x, y) points. |
flip_h | bool | Whether to flip horizontally. |
flip_v | bool | Whether to flip vertically. |
| Returns | Polygon |
flip_polygon_numpy()
flip_polygon_numpy(polygon: NDArray[f64], flip_h: bool, flip_v: bool) -> Any
Flip a polygon from numpy array.
Returns: Flipped polygon as numpy array.
| Parameter | Type | Description |
|---|---|---|
polygon | NDArray[f64] | Polygon as a 2D numpy array. |
flip_h | bool | Whether to flip horizontally. |
flip_v | bool | Whether to flip vertically. |
| Returns | Any |
flip_polygons()
flip_polygons(polygons: Any, flip_h: bool, flip_v: bool) -> list[Polygon]
Flip multiple polygons.
Returns: Flipped polygons.
| Parameter | Type | Description |
|---|---|---|
polygons | Any | List of polygons to flip. |
flip_h | bool | Whether to flip horizontally. |
flip_v | bool | Whether to flip vertically. |
| Returns | list[Polygon] |
flip_polygons_numpy()
flip_polygons_numpy(polygons: list, flip_h: bool, flip_v: bool) -> Any
Flip polygons from numpy arrays.
Returns: List of flipped numpy arrays.
| Parameter | Type | Description |
|---|---|---|
polygons | list | List of 2D numpy arrays. |
flip_h | bool | Whether to flip horizontally. |
flip_v | bool | Whether to flip vertically. |
| Returns | Any |
get_polygon_area()
get_polygon_area(polygon: Sequence[Point]) -> float
Get the unsigned area of a polygon.
Returns: Unsigned area.
| Parameter | Type | Description |
|---|---|---|
polygon | Sequence[Point] | Polygon as (x, y) points. |
| Returns | float |
get_polygon_bounds()
get_polygon_bounds(polygon: Sequence[Point]) -> Rect
Get the bounding rectangle of a polygon.
Returns: Bounding rectangle as (x_min, y_min, x_max, y_max).
| Parameter | Type | Description |
|---|---|---|
polygon | Sequence[Point] | Polygon as (x, y) points. |
| Returns | Rect |
get_polygon_centroid()
get_polygon_centroid(polygon: Sequence[Point]) -> Point
Get the centroid of a polygon.
Returns: Centroid point (x, y).
| Parameter | Type | Description |
|---|---|---|
polygon | Sequence[Point] | Polygon as (x, y) points. |
| Returns | Point |
get_polygon_convex_hull()
get_polygon_convex_hull(polygon: Sequence[Point]) -> Polygon
Get the convex hull of a polygon.
Returns: Convex hull as list of points.
| Parameter | Type | Description |
|---|---|---|
polygon | Sequence[Point] | Polygon as (x, y) points. |
| Returns | Polygon |
get_polygon_edges()
get_polygon_edges(polygon: Sequence[Point]) -> list[tuple[Point, Point]]
Get the edges of a polygon.
Returns: List of ((x1, y1), (x2, y2)) edges.
| Parameter | Type | Description |
|---|---|---|
polygon | Sequence[Point] | Polygon as (x, y) points. |
| Returns | list[tuple[Point, Point]] |
get_polygon_group_bounds()
get_polygon_group_bounds(polygons: Any) -> Rect
Get the bounding rectangle of a group of polygons.
Returns: Bounding rectangle as (x_min, y_min, x_max, y_max).
| Parameter | Type | Description |
|---|---|---|
polygons | Any | List of polygons. |
| Returns | Rect |
get_polygon_perimeter()
get_polygon_perimeter(polygon: Sequence[Point]) -> float
Get the perimeter of a polygon.
Returns: Perimeter length.
| Parameter | Type | Description |
|---|---|---|
polygon | Sequence[Point] | Polygon as (x, y) points. |
| Returns | float |
get_polygon_signed_area()
get_polygon_signed_area(polygon: Sequence[Point]) -> float
Get the signed area of a polygon.
Returns: Signed area (positive for CCW, negative for CW).
| Parameter | Type | Description |
|---|---|---|
polygon | Sequence[Point] | Polygon as (x, y) points. |
| Returns | float |
get_polygons_difference()
get_polygons_difference(poly1: Sequence[Point], poly2: Sequence[Point]) -> list[Polygon]
Get the difference of two polygons.
Returns: Difference polygon(s).
| Parameter | Type | Description |
|---|---|---|
poly1 | Sequence[Point] | First polygon as (x, y) points. |
poly2 | Sequence[Point] | Second polygon to subtract. |
| Returns | list[Polygon] |
get_polygons_intersection()
get_polygons_intersection(poly1: Sequence[Point], poly2: Sequence[Point]) -> list[Polygon]
Get the intersection of two polygons.
Returns: Intersection polygon(s).
| Parameter | Type | Description |
|---|---|---|
poly1 | Sequence[Point] | First polygon as (x, y) points. |
poly2 | Sequence[Point] | Second polygon as (x, y) points. |
| Returns | list[Polygon] |
get_polygons_union()
get_polygons_union(polygons: Any) -> list[Polygon]
Get the union of multiple polygons.
Returns: Union polygon(s).
| Parameter | Type | Description |
|---|---|---|
polygons | Any | List of polygons to union. |
| Returns | list[Polygon] |
is_almost_equal()
is_almost_equal(a: float, b: float, tolerance: Optional[float] = None) -> bool
Check if two floats are almost equal.
Returns: True if |a - b| < tolerance.
| Parameter | Type | Description |
|---|---|---|
a | float | First float. |
b | float | Second float. |
tolerance | Optional[float] = None | Comparison tolerance. |
| Returns | bool |
is_point_inside_polygon()
is_point_inside_polygon(point: Point, polygon: Sequence[Point]) -> bool
Check if a point is inside a polygon.
Returns: True if point is inside the polygon.
| Parameter | Type | Description |
|---|---|---|
point | Point | Point (x, y) to test. |
polygon | Sequence[Point] | Polygon as (x, y) points. |
| Returns | bool |
is_polygon_clockwise()
is_polygon_clockwise(points: Sequence[Point]) -> bool
Check if a polygon has clockwise winding order.
Returns: True if the winding is clockwise.
| Parameter | Type | Description |
|---|---|---|
points | Sequence[Point] | Sequence of (x, y) points defining a polygon. |
| Returns | bool |
is_polygon_convex()
is_polygon_convex(polygon: Sequence[Point]) -> bool
Check if a polygon is convex.
Returns: True if the polygon is convex.
| Parameter | Type | Description |
|---|---|---|
polygon | Sequence[Point] | Polygon as (x, y) points. |
| Returns | bool |
normalize_polygons()
normalize_polygons(polygons: Any) -> tuple[list[Polygon], float, float]
Normalize polygons (outer CCW, inner CW).
Returns: Tuple of (normalized_polygons, min_x, min_y).
| Parameter | Type | Description |
|---|---|---|
polygons | Any | List of polygons to normalize. |
| Returns | tuple[list[Polygon], float, float] |
normalize_polygons_numpy()
normalize_polygons_numpy(polygons: Sequence[NDArray[f64]]) -> tuple[list[NDArray[f64]], float, float]
Normalize polygons from numpy arrays.
Returns: Tuple of (normalized_arrays, min_x, min_y).
| Parameter | Type | Description |
|---|---|---|
polygons | Sequence[NDArray[f64]] | Sequence of 2D numpy arrays. |
| Returns | tuple[list[NDArray[f64]], float, float] |
offset_polygon()
offset_polygon(polygon: Sequence[Point], offset: float) -> list[Polygon]
Offset (inflate/deflate) a polygon.
Returns: Offset polygon(s).
| Parameter | Type | Description |
|---|---|---|
polygon | Sequence[Point] | Polygon as (x, y) points. |
offset | float | Offset distance (positive to inflate, negative to deflate). |
| Returns | list[Polygon] |
point_in_polygon_numpy()
point_in_polygon_numpy(point: Point, polygon: NDArray[f64]) -> bool
Check if point is in polygon from numpy array.
Returns: True if point is inside the polygon.
| Parameter | Type | Description |
|---|---|---|
point | Point | Point (x, y) to test. |
polygon | NDArray[f64] | Polygon as a 2D numpy array. |
| Returns | bool |
point_line_distance()
point_line_distance(point: Point, line_start: Point, line_end: Point) -> float
Compute the distance from a point to a line.
Returns: Perpendicular distance.
| Parameter | Type | Description |
|---|---|---|
point | Point | Point (x, y). |
line_start | Point | Line start point (x, y). |
line_end | Point | Line end point (x, y). |
| Returns | float |
polygon_area_numpy()
polygon_area_numpy(polygon: NDArray[f64]) -> float
Get the area of a polygon from numpy array.
Returns: Signed area.
| Parameter | Type | Description |
|---|---|---|
polygon | NDArray[f64] | Polygon as a 2D numpy array. |
| Returns | float |
polygon_bounds_numpy()
polygon_bounds_numpy(polygon: NDArray[f64]) -> Rect
Get bounds of a polygon from numpy array.
Returns: Bounding rectangle as (x_min, y_min, x_max, y_max).
| Parameter | Type | Description |
|---|---|---|
polygon | NDArray[f64] | Polygon as a 2D numpy array. |
| Returns | Rect |
polygon_group_bounds_numpy()
polygon_group_bounds_numpy(polygons: Sequence[NDArray[f64]]) -> Rect
Get bounds of polygon group from numpy arrays.
Returns: Bounding rectangle as (x_min, y_min, x_max, y_max).
| Parameter | Type | Description |
|---|---|---|
polygons | Sequence[NDArray[f64]] | Sequence of 2D numpy arrays. |
| Returns | Rect |
polygon_perimeter_numpy()
polygon_perimeter_numpy(polygon: NDArray[f64]) -> float
Get the perimeter of a polygon from numpy array.
Returns: Perimeter length.
| Parameter | Type | Description |
|---|---|---|
polygon | NDArray[f64] | Polygon as a 2D numpy array. |
| Returns | float |
polygons_intersect()
polygons_intersect(p1: Sequence[Point], p2: Sequence[Point], min_area: float = 0) -> bool
Check if two polygons intersect.
Returns: True if polygons intersect.
| Parameter | Type | Description |
|---|---|---|
p1 | Sequence[Point] | First polygon as (x, y) points. |
p2 | Sequence[Point] | Second polygon as (x, y) points. |
min_area | float = 0 | Minimum intersection area threshold. |
| Returns | bool |
polygons_intersect_numpy()
polygons_intersect_numpy(poly1: NDArray[f64], poly2: NDArray[f64], min_area: float = 0) -> bool
Check if polygons intersect from numpy arrays.
Returns: True if polygons intersect.
| Parameter | Type | Description |
|---|---|---|
poly1 | NDArray[f64] | First polygon as a 2D numpy array. |
poly2 | NDArray[f64] | Second polygon as a 2D numpy array. |
min_area | float = 0 | Minimum intersection area threshold. |
| Returns | bool |
rotate_polygon()
rotate_polygon(polygon: Sequence[Point], angle: float) -> Polygon
Rotate a polygon by an angle.
Returns: Rotated polygon.
| Parameter | Type | Description |
|---|---|---|
polygon | Sequence[Point] | Polygon as (x, y) points. |
angle | float | Rotation angle in radians. |
| Returns | Polygon |
rotate_polygon_numpy()
rotate_polygon_numpy(polygon: NDArray[f64], angle: float) -> Any
Rotate a polygon from numpy array.
Returns: Rotated polygon as numpy array.
| Parameter | Type | Description |
|---|---|---|
polygon | NDArray[f64] | Polygon as a 2D numpy array. |
angle | float | Rotation angle in radians. |
| Returns | Any |
rotate_polygons()
rotate_polygons(polygons: Any, angle: float) -> list[Polygon]
Rotate multiple polygons by an angle.
Returns: Rotated polygons.
| Parameter | Type | Description |
|---|---|---|
polygons | Any | List of polygons to rotate. |
angle | float | Rotation angle in radians. |
| Returns | list[Polygon] |
rotate_polygons_numpy()
rotate_polygons_numpy(polygons: Sequence[NDArray[f64]], angle: float) -> list[Any]
Rotate polygons from numpy arrays.
Returns: List of rotated numpy arrays.
| Parameter | Type | Description |
|---|---|---|
polygons | Sequence[NDArray[f64]] | Sequence of 2D numpy arrays. |
angle | float | Rotation angle in radians. |
| Returns | list[Any] |
scale_polygon()
scale_polygon(polygon: Sequence[Point], scale: float, scale_y: Optional[float] = None) -> Polygon
Scale a polygon.
Returns: Scaled polygon.
| Parameter | Type | Description |
|---|---|---|
polygon | Sequence[Point] | Polygon as (x, y) points. |
scale | float | X (and Y if scale_y is None) scale factor. |
scale_y | Optional[float] = None | Y scale factor (optional). |
| Returns | Polygon |
to_clipper_numpy()
to_clipper_numpy(polygon: Any, scale: int = 10000000) -> list[tuple[int, int]]
Convert a polygon to Clipper coordinates.
Returns: Polygon with integer coordinates for Clipper.
| Parameter | Type | Description |
|---|---|---|
polygon | Any | Polygon as numpy array or list of tuples. |
scale | int = 10000000 | Scale factor for integer conversion. |
| Returns | list[tuple[int, int]] |
translate_bounds()
translate_bounds(bounds: Rect, dx: float, dy: float) -> Rect
Translate a bounding rectangle.
Returns: Translated bounding rectangle.
| Parameter | Type | Description |
|---|---|---|
bounds | Rect | Bounding rectangle (x_min, y_min, x_max, y_max). |
dx | float | X translation. |
dy | float | Y translation. |
| Returns | Rect |
translate_polygon()
translate_polygon(polygon: Sequence[Point], dx: float, dy: float) -> Polygon
Translate a polygon.
Returns: Translated polygon.
| Parameter | Type | Description |
|---|---|---|
polygon | Sequence[Point] | Polygon as (x, y) points. |
dx | float | X translation. |
dy | float | Y translation. |
| Returns | Polygon |
translate_polygon_numpy()
translate_polygon_numpy(polygon: NDArray[f64], dx: float, dy: float) -> Any
Translate a polygon from numpy array.
Returns: Translated polygon as numpy array.
| Parameter | Type | Description |
|---|---|---|
polygon | NDArray[f64] | Polygon as a 2D numpy array. |
dx | float | X translation. |
dy | float | Y translation. |
| Returns | Any |
translate_polygons()
translate_polygons(polygons: Any, dx: float, dy: float) -> list[Polygon]
Translate a list of polygons.
Returns: Translated polygons.
| Parameter | Type | Description |
|---|---|---|
polygons | Any | List of polygons to translate. |
dx | float | X translation. |
dy | float | Y translation. |
| Returns | list[Polygon] |
translate_polygons_numpy()
translate_polygons_numpy(polygons: Sequence[NDArray[f64]], dx: float, dy: float) -> list[Any]
Translate polygons from numpy arrays.
Returns: List of translated numpy arrays.
| Parameter | Type | Description |
|---|---|---|
polygons | Sequence[NDArray[f64]] | Sequence of 2D numpy arrays. |
dx | float | X translation. |
dy | float | Y translation. |
| Returns | list[Any] |