Skip to content

Commit c29f421

Browse files
authored
✨ add functions for working with centroids (#93)
1 parent 25a61b1 commit c29f421

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Quickly and easily connect to Mindee's API services using Python.
44
## Quick Start
55
Here's the TL;DR of getting started.
66

7-
First, get an [API Key](https://developers.mindee.com/docs/make-your-first-request#create-an-api-key)
7+
First, get an [API Key](https://developers.mindee.com/docs/create-api-key)
88

99
Then, install this library:
1010
```shell

mindee/geometry.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,51 @@
88
Quadrilateral = Tuple[Point, Point, Point, Point]
99

1010

11+
def get_centroid(polygon: Polygon) -> Point:
12+
"""
13+
Get the central point (centroid) given a list of points.
14+
15+
:param polygon: List of Points
16+
:return: Point
17+
"""
18+
numb_vertices = len(polygon)
19+
x_sum = sum([x for x, _ in polygon])
20+
y_sum = sum([y for _, y in polygon])
21+
return x_sum / numb_vertices, y_sum / numb_vertices
22+
23+
24+
def get_min_max_y(vertices: Polygon) -> Point:
25+
"""
26+
Get the maximum and minimum Y value given a list of points.
27+
28+
:param vertices: List of points
29+
"""
30+
points = [y for _, y in vertices]
31+
return min(points), max(points)
32+
33+
34+
def is_point_in_polygon_y(point: Point, polygon: Polygon) -> bool:
35+
"""
36+
Determine if the Point is in the Polygon's Y-axis.
37+
38+
:param point: Point to compare
39+
:param polygon: Polygon to look into
40+
"""
41+
min_y, max_y = get_min_max_y(polygon)
42+
return is_point_in_y(point, min_y, max_y)
43+
44+
45+
def is_point_in_y(point: Point, min_y: float, max_y: float) -> bool:
46+
"""
47+
Determine if the Point is in the Polygon's Y-axis.
48+
49+
:param point: Point to compare
50+
:param min_y: Minimum Y-axis value
51+
:param max_y: Maximum Y-axis value
52+
"""
53+
return min_y <= point[1] <= max_y
54+
55+
1156
def get_bbox_as_polygon(polygon: Polygon) -> Quadrilateral:
1257
"""
1358
Given a sequence of points, calculate a polygon that encompasses all points.

tests/test_geometry.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,22 @@ def test_bbox_polygon(polygon_a, polygon_b, polygon_c):
4646
(0.381, 0.43),
4747
(0.205, 0.43),
4848
)
49+
50+
51+
def test_is_point_in_polygon_y(polygon_a, polygon_b, polygon_c):
52+
# Should be in polygon A & B, since polygons overlap
53+
point_a = (0.125, 0.535)
54+
# Should only be in polygon C
55+
point_b = (0.300, 0.420)
56+
57+
assert geometry.is_point_in_polygon_y(point_a, polygon_a)
58+
assert geometry.is_point_in_polygon_y(point_a, polygon_b)
59+
assert geometry.is_point_in_polygon_y(point_a, polygon_c) is False
60+
61+
assert geometry.is_point_in_polygon_y(point_b, polygon_a) is False
62+
assert geometry.is_point_in_polygon_y(point_b, polygon_b) is False
63+
assert geometry.is_point_in_polygon_y(point_b, polygon_c)
64+
65+
66+
def test_get_centroid(polygon_a):
67+
assert geometry.get_centroid(polygon_a) == (0.149, 0.538)

0 commit comments

Comments
 (0)