|
8 | 8 | Quadrilateral = Tuple[Point, Point, Point, Point] |
9 | 9 |
|
10 | 10 |
|
| 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 | + |
11 | 56 | def get_bbox_as_polygon(polygon: Polygon) -> Quadrilateral: |
12 | 57 | """ |
13 | 58 | Given a sequence of points, calculate a polygon that encompasses all points. |
|
0 commit comments