Obtaining the “centroid” – convex polygon central point coordinates, from polygons points (vertices) coordinates:
1 2 3 4 5 6 7 |
def centroid(vertexes): _x_list = [vertex [0] for vertex in vertexes] _y_list = [vertex [1] for vertex in vertexes] _len = len(vertexes) _x = sum(_x_list) / _len _y = sum(_y_list) / _len return(_x, _y) |
The input function parameter is a tuple with coordinates of the polygon points. The function returns a tuple with the centroid coordinates:
1 2 |
polygon_data = ((0, 0), (1, 0), (1, 1), (0, 1)) print(centroid(polygon_data)) # (0.5, 0.5) |