Skip to content

Commit

Permalink
Merge pull request #1602 from ablazejuk/patch-1
Browse files Browse the repository at this point in the history
Validate that polygon has at least 3 vertices in geometry utils.py
  • Loading branch information
LinasKo authored Oct 17, 2024
2 parents 75a4ff1 + 1b95d1d commit f60d89f
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions supervision/geometry/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ def get_polygon_center(polygon: np.ndarray) -> Point:
Point: The center of the polygon, represented as a
Point object with x and y attributes.
Raises:
ValueError: If the polygon has no vertices.
Examples:
```python
import numpy as np
Expand All @@ -30,6 +33,9 @@ def get_polygon_center(polygon: np.ndarray) -> Point:
# This is one of the 3 candidate algorithms considered for centroid calculation.
# For a more detailed discussion, see PR #1084 and commit eb33176

if len(polygon) == 0:
raise ValueError("Polygon must have at least one vertex.")

shift_polygon = np.roll(polygon, -1, axis=0)
signed_areas = np.cross(polygon, shift_polygon) / 2
if signed_areas.sum() == 0:
Expand Down

0 comments on commit f60d89f

Please sign in to comment.