Calculate the area of any polygon

Question: In the two-dimensional plane coordinate system, for any polygon, the coordinates of each vertex are known a_{1}(x_{1},y_{1}),a_{2}(x_{2},y_{2}),...,a_{n}(x_{n},y_{n}), and the area of ​​the polygon is calculated.

When I saw the words "coordinates" and "area", the first thing I thought of was the content of the last blog I just wrote: the outer product of vectors. Then we try to start from the outer product of the vector to see if we can answer the problem of calculating the area.

Definition of vector outer product:

If two vectors\overrightarrow{a}=(x_{a},y_{a},z_{a}),\overrightarrow{b}=(x_{b},y_{b},z_{b})

Then \overrightarrow{a}\times \overrightarrow{b}=(y_{a}z_{b}-z_{a}y_{b},x_{a}z_{b}-z_{a}x_{b},x_{a}y_{b}-y_{a}x_{b})

In order to facilitate the calculation and derivation, we further evolve the definition formula of the outer product, as follows:

\overrightarrow{a}\times \overrightarrow{b}

=(y_{a}z_{b}-z_{a}y_{b},x_{a}z_{b}-z_{a}x_{b},x_{a}y_{b}-y_{a}x_{b})

=(y_{a}z_{b}-z_{a}y_{b})\overrightarrow{i}+(x_{a}z_{b}-z_{a}x_{b})\overrightarrow{j}+(x_{a}y_{b}-y_{a}x_{b})\overrightarrow{k}

=\begin{vmatrix} \overrightarrow{i} & \overrightarrow{j} & \overrightarrow{k}\\ x_{a} & y_{a} & z_{a}\\ x_{b} & y_{b} & z_{b} \end{vmatrix}

Dimensionality reduction to a two-dimensional plane, we put z_{a}=0,z_{b}=0, get

\overrightarrow{a}\times \overrightarrow{b}= \begin{vmatrix} \overrightarrow{i} & \overrightarrow{j} & \overrightarrow{k}\\ x_{a} & y_{a} & z_{a}\\ x_{b} & y_{b} & z_{b} \end{vmatrix}= \begin{vmatrix} \overrightarrow{i} & \overrightarrow{j} & \overrightarrow{k}\\ x_{a} & y_{a} & 0\\ x_{b} & y_{b} & 0 \end{vmatrix}, which \overrightarrow{i},\overrightarrow{j},\overrightarrow{k}represent the unit vectors of the x-axis, y-axis, and z-axis respectively.

Because in the third column, except for the first row, all other rows are 0, so we expand according to the third column of the determinant, and get

\overrightarrow{a}\times \overrightarrow{b}= \begin{vmatrix} \overrightarrow{i} & \overrightarrow{j} & \overrightarrow{k}\\ x_{a} & y_{a} & 0\\ x_{b} & y_{b} & 0 \end{vmatrix}= \overrightarrow{k}A_{13}= \begin{vmatrix} x_{a} & y_{a}\\ x_{b} & y_{b} \end{vmatrix}\overrightarrow{k}, which A_{13}represents \overrightarrow{k}the algebraic cofactor.

 So when we calculate the outer product of two vectors in a two-dimensional plane, we can evaluate it by calculating the determinant of the ordered array of coordinates of the two vectors. In the previous blog, we have verified that the absolute value of the outer product of two vectors is equal to twice the area of ​​the triangle enclosed by the origin (0,0)and the three points.(x_{a},y_{a}),(x_{b},y_{b})

With this foundation in place, let's go back to the question.

 The problem describes a polygon. Let's start with the simplest polygon: a triangle, as shown in the figure below:

The area of ​​the triangle in the figure above S_{\Delta ABC}is equal to

S_{\Delta ABC}=S_{\Delta OAB}-S_{\Delta OBC}-S_{\Delta OCA}

 We use the vector outer product to bring in the area of ​​each triangle in the above formula, we can get

S_{\Delta ABC}=S_{\Delta OAB}-S_{\Delta OBC}-S_{\Delta OCA}=1/2(\overrightarrow{OA}\times \overrightarrow{OB}+\overrightarrow{OB}\times \overrightarrow{OC}+\overrightarrow{OC}\times \overrightarrow{OA})

 Note: When vectors are cross-multiplied, the result will be positive or negative, and the positive and negative represent the direction, which satisfies the right-hand rule.

After computing the triangles, we move on to the quadrilaterals.

The quadrilateral becomes a little more complicated, because the quadrilateral has so-called convex and concave sides. Let's try to see if the two scenarios can have the same calculation idea, that is, no sense of bumps. We first calculate the two graphics separately.

Convex shape:

S_{ABCD}

=-S_{\Delta OAB}-S_{\Delta OBC}+S_{\Delta OCD}+S_{\Delta ODA}

1/2(\overrightarrow{OA}\times \overrightarrow{OB}+\overrightarrow{OB}\times \overrightarrow{OC}+\overrightarrow{OC}\times \overrightarrow{OD}+\overrightarrow{OD}\times \overrightarrow{OA})

Concave shape:

 S_{ABCD}

-S_{\Delta OAB}-S_{\Delta OBC}+S_{\Delta OCD}+S_{\Delta ODA}

1/2(\overrightarrow{OA}\times \overrightarrow{OB}+\overrightarrow{OB}\times \overrightarrow{OC}+\overrightarrow{OC}\times \overrightarrow{OD}+\overrightarrow{OD}\times \overrightarrow{OA})

It is found that the formulas of the upper and lower results are unified, that is, the convex and concave sides of the quadrilateral. When using the vector outer product for area calculation, the calculation formula is consistent, and there is no sense of bumps.

Now we have verified a problem-solving method from triangles to quadrilaterals, which is:

The vectors composed of each vertex and the origin of the polygon, in a counterclockwise order (or clockwise, can also be used), and the two vectors before and after are cross-multiplied to find the outer product, and the tail and head vectors are also cross-multiplied, (in other words, that is The two endpoints connected by each edge in the graph need to be cross-multiplied by head-to-tail ), and then the absolute value of half of the sum obtained by adding all the outer products is the area of ​​the polygon. Right now

S_{polygon}=1/2(\overrightarrow{oa_{1}}\times\overrightarrow{oa_{2}} +\overrightarrow{oa_{2}}\times\overrightarrow{oa_{3}}+...+\overrightarrow{oa_{n}}\times\overrightarrow{oa_{1}} )

Can this calculation method be applied to all polygons? Let us prove it by mathematical induction.

Assuming that when there are n sides, S_{n}= 1/2(\overrightarrow{oa_{1}}\times\overrightarrow{oa_{2}} +\overrightarrow{oa_{2}}\times\overrightarrow{oa_{3}}+...+\overrightarrow{oa_{n}}\times\overrightarrow{oa_{1}} )is true, then add a point on this basis at this time a_{n+1}to become n+1 sides

As shown in the figure above, we need to add a_{n}a_{n+1},a_{n+1}a_{1}two edges (the yellow part in the figure) and remove a_{n}a_{1}this edge (the red dotted line in the figure), then

S_{n+1}

S_{n}+1/2(\overrightarrow{oa_{n}}\times \overrightarrow{oa_{n+1}}+\overrightarrow{oa_{n+1}}\times \overrightarrow{oa_{1}}-\overrightarrow{oa_{n}}\times \overrightarrow{oa_{1}})

=1/2(\overrightarrow{oa_{1}}\times\overrightarrow{oa_{2}} +\overrightarrow{oa_{2}}\times\overrightarrow{oa_{3}}+...+\overrightarrow{oa_{n}}\times\overrightarrow{oa_{1}} ) +

  1/2(\overrightarrow{oa_{n}}\times \overrightarrow{oa_{n+1}}+\overrightarrow{oa_{n+1}}\times \overrightarrow{oa_{1}}-\overrightarrow{oa_{n}}\times \overrightarrow{oa_{1}})(Note: This corresponds to adding two sides and removing one side)

=1/2(\overrightarrow{oa_{1}}\times\overrightarrow{oa_{2}} +\overrightarrow{oa_{2}}\times\overrightarrow{oa_{3}}+...+\overrightarrow{oa_{n}}\times\overrightarrow{oa_{n+1}}+\overrightarrow{oa_{n+1}}\times\overrightarrow{oa_{1}} )

The final result satisfies the calculation rules we summarized above. So far, we have theoretically obtained the solution of the polygon area.

 Then, next, we need to transplant this calculation method to the computer and let the computer perform the calculation for us, so we need to continue to evolve the calculation formula into a form that is convenient for the computer to calculate.

S_{polygon}

1/2(\overrightarrow{oa_{1}}\times\overrightarrow{oa_{2}} +\overrightarrow{oa_{2}}\times\overrightarrow{oa_{3}}+...+\overrightarrow{oa_{n}}\times\overrightarrow{oa_{1}} )

=1/2(\begin{vmatrix} x_{1} & y_{1}\\ x_{2} & y_{2} \end{vmatrix} + \begin{vmatrix} x_{2} & y_{2}\\ x_{3} & y_{3} \end{vmatrix} +...+\begin{vmatrix} x_{n} & y_{n}\\ x_{1} & y_{1} \end{vmatrix})

=1/2(x_{1}y_{2}-y_{1}x_{2}+x_{2}y_{3}-y_{2}x_{3}+...+x_{n}y_{1}-y_{n}x_{1})

=1/2\sum_{i=1}^{n}(x_{i}y_{i+1}-y_{i}x_{i+1})

Among them, i=nat that time , i+1=n+1=1iex_{n+1}=x_{1},y_{n+1}=y_{1}

Finally, we got the above formula, which is the famous Shoelace Theorem shoelace theorem .

 What are its characteristics? Let's borrow pictures from the Internet, as follows:

 

 Because this formula operates like a shoelace, it is called the shoelace theorem.

 With the shoelace theorem, we can easily port it into code.

struct Point2d  
{  
    double x;  
    double y;  
    Point2d(double xx, double yy): x(xx), y(yy){}  
};  
   
//计算任意多边形的面积,顶点按照顺时针或者逆时针方向排列  
double ComputePolygonArea(const vector<Point2d> &points)  
{  
    int point_num = points.size();  
    if(point_num < 3)return 0.0;  
    double s = 0;  
    for(int i = 0; i < point_num; ++i)  
        s += points[i].x * points[(i+1)%point_num].y - points[i].y * points[(i+1)%point_num].x;  
    return fabs(s/2.0);  
}

Guess you like

Origin blog.csdn.net/m0_74178120/article/details/128737748