Introduction to Algorithms Chapter 31, Section 1, Question 8. As long as it is a simple polygon whose edges do not intersect, that is, not only convex polygons, but also various odd-shaped concave polygons, the area can be calculated using Green's formula.
Green's formula: If the functions P(x,y), Q(x,y) are continuous on the closed region D surrounded by one or more smooth curves, and have continuous first-order partial derivatives, then there is
L as the region The boundary curve of D, and take the positive direction.
A simple polygon with disjoint edges is just a closed area enclosed by several line segments, so Green's formula can be used.
Let P=0, Q=x, then the area S =
Let the coordinates of the i-th point be , and the coordinates of the i + 1-th point be , then the parameter formula of the line segment is , so , so the area . The above formula can calculate the area of any simple polygon, including triangles, quadrilaterals, and hexagons.
# P is list of vertices of the polygon
def polygon_area(P):
n = len(P)
P.append(P[0])
S = 0
for i in range(0, n):
S = S + (P[i][0] + P[i + 1][0]) * (P[i + 1][1] - P[i][1])
return 0.5 * abs(S)
Author: Wang Er
Link : https://www.zhihu.com/question/53259589/answer/134574326
Source: Zhihu The
copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.