Files
supervision/tests/geometry/test_utils.py
T
0a95bae8a8 chore: bump minimum Python to 3.10 (#2260)
- Drop Python 3.9 from CI test matrix
- requires-python = ">=3.10" in pyproject.toml
- ruff target-version py39 → py310
- mypy python_version 3.9 → 3.10
- Remove Python 3.9 classifier

---------

Co-authored-by: Claude Code <noreply@anthropic.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Codex <codex@openai.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
2026-06-29 14:45:30 +02:00

61 lines
1.9 KiB
Python

import numpy as np
import pytest
from supervision.geometry.core import Point
from supervision.geometry.utils import get_polygon_center
def generate_test_polygon(n: int) -> np.ndarray:
"""
Generate a semicircle with a given number of points.
Parameters:
n (int): amount of points in polygon
Returns:
Polygon: test polygon in the form of a semicircle.
Examples:
```python
from supervision.geometry.utils import get_polygon_center
import numpy as np
test_polygon = generate_test_data(1000)
get_polygon_center(test_polygon)
Point(x=500, y=1212)
```
"""
r: int = n // 2
x_axis = np.linspace(0, 2 * r, n)
y_axis = (r**2 - (x_axis - r) ** 2) ** 0.5 + 2 * r
polygon = np.array([x_axis, y_axis]).T
return polygon
@pytest.mark.parametrize(
("polygon", "expected_result"),
[
(generate_test_polygon(10), Point(x=5.0, y=12.0)),
(generate_test_polygon(50), Point(x=25.0, y=61.0)),
(generate_test_polygon(100), Point(x=50.0, y=121.0)),
(generate_test_polygon(1000), Point(x=500.0, y=1212.0)),
(generate_test_polygon(3000), Point(x=1500.0, y=3637.0)),
(generate_test_polygon(10000), Point(x=5000.0, y=12122.0)),
(generate_test_polygon(20000), Point(x=10000.0, y=24244.0)),
(generate_test_polygon(50000), Point(x=25000.0, y=60610.0)),
],
)
def test_get_polygon_center(polygon: np.ndarray, expected_result: Point) -> None:
"""
Verify that get_polygon_center correctly calculates the centroid of a polygon.
Scenario: Calculating the center point (centroid) of various polygons.
Expected: The returned `Point` correctly represents the average position of all
polygon vertices, which is used for placing labels or markers at the center
of detected objects.
"""
result = get_polygon_center(polygon)
assert result == expected_result