本文提供一个简单的方法计算多边形面积,参考维基百科
实现代码:
def polygon_area(polygon):
"""
compute polygon area
polygon: list with shape [n, 2], n is the number of polygon points
"""
area = 0
q = polygon[-1]
for p in polygon:
area += p[0] * q[1] - p[1] * q[0]
q = p
return abs(area) / 2.0
polygon = np.array([[0, 0], [-1, 1], [0, 2], [1, 1]]).astype("float32")
area = polygon_area(polygon)
print(area)
# 2.0