代码
def polygon_area(poly):
"""
compute area of a polygon
:param poly:
:return:
"""
edge = [(poly[1][0] - poly[0][0]) * (poly[1][1] + poly[0][1]),
(poly[2][0] - poly[1][0]) * (poly[2][1] + poly[1][1]),
(poly[3][0] - poly[2][0]) * (poly[3][1] + poly[2][1]),
(poly[0][0] - poly[3][0]) * (poly[0][1] + poly[3][1])]
return np.sum(edge) / 2.
计算任意多边形面积的Shoelace公式如下
如代码所示,假设poly为任意四边形,四个顶点分别为,则根据Shoelace公式展开面积为。代码中的公式则为,展开相消会发现和Shoelace公式一样。
参考
https://zhuanlan.zhihu.com/p/110025234