Green's formula to calculate the area of a polygon

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
\iint_d(\frac{\partial Q}{\partial x} - \frac{\partial P}{\partial y})d\sigma = \oint_LPdx + Qdy
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 = \int_Dd \ sigma = \ oint_Lxdy
Let the coordinates of the i-th point be , and P_ithe coordinates of (x_i, y_i)the i + 1-th point P_{i + 1}be , then the parameter formula of (x_{i + 1}, y_{i + 1})the line segment is , so , so the area . The above formula can calculate the area of ​​any simple polygon, including triangles, quadrilaterals, and hexagons.\overline{P_iP_{i + 1}}\begin{cases}x = x_i + (x_{i + 1} - x_i)t \\y = y_i + (y_{i + 1} - y_i)t\end{cases}
\int_{\overline{P_iP_{i + 1}}}xdy = \int_0^1(x_i + (x_{i + 1} - x_i)t)(y_{i + 1} - y_i)dt=\frac{1}{2}(x_i + x_{i + 1})(y_{i + 1} - y_i)
S=\frac{1}{2}|\sum_{i=1}^{n}(x_i + x_{i + 1})(y_{i + 1} - y_i)|

python implementation:
# 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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324831678&siteId=291194637