Calculate the area of any polygon.

Source: Internet
Author: User

For convex polygon, it is easy to calculate. For example, you can divide a polygon point into several triangles, calculate the area of these triangles, and add them together. Given the coordinate of triangle vertices, the triangle product can be calculated using the cross multiplication of vectors.

 

 

For a concave polygon, if it is still divided into triangles according to the above method, for example, the area of the polygon is s_abc + s_acd + s_ade, which obviously exceeds the area of the polygon.

 

When we calculate the ABC area of a triangle based on the two-dimensional vector cross multiplication, we use

In this way, the area obtained is positive, but the vector cross-multiplication has a direction, that is, positive and negative. if you remove the absolute value symbol in the third formula above, that is, then the area is positive and negative. In the second figure above, S = s_abc + s_acd + s_ade. If s_abc and s_ade are positive, s_acd is negative, which adds up to the area of the polygon. For convex polygon, the area of all triangles is the same as positive or negative.

 

If we do not use a polygon point as the vertex to divide the triangle but use any point, for example, this method is also true: S = s_oab + s_obc + s_ocd + s_ode + s_oea. During computing, when we take the O point as the origin, the computation can be simplified. Address of this Article

When the O point is the origin, based on the cross product calculation formula of the vector, the area of each triangle is calculated as follows:

 

S_oab = 0.5 * (a_x * B _y-a_y * B _x) [(a_x, a_y) is the coordinate of Point]

S_obc = 0.5 * (B _x * c_y-B _y * C_x)

S_ocd = 0.5 * (C_x * d_y-c_y * D_x)

S_ode = 0.5 * (D_x * e_y-d_y * e_x)

S_oea = 0.5 * (e_x * a_y-e_y * a_x)

 

 

 

 

 

The Code is as follows:

Struct point2d {Double X; Double Y; point2d (double XX, double YY): X (XX), y (yy) {}}; // calculates the area of any polygon, the vertex is arranged clockwise or counterclockwise in 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 );}

 

The algorithm can also be optimized to merge similar items in the preceding format.

S = s_oab + s_obc + s_ocd + s_ode + s_oea =

0.5 * (a_y * (E_x-B_x) + B _y * (A_x-C_x) + c_y * (B _x-D_x) + d_y * (C_x-E_x) + e_y * (D_x-A_x ))

 

The Code is as follows:

Struct point2d {Double X; Double Y; point2d (double XX, double YY): X (XX), y (yy) {}}; // calculates the area of any polygon, the vertex is arranged clockwise or counterclockwise in double computepolygonarea (const vector <point2d> & points) {int point_num = points. size (); If (point_num <3) return 0.0; double S = points [0]. y * (points [point_num-1]. x-points [1]. x); For (INT I = 1; I <point_num; ++ I) s ++ = points [I]. y * (points [I-1]. x-points [(I + 1) % point_num]. x); Return FABS (S/2.0 );}

 

[Copyright statement] Reprinted with the following source:Http://www.cnblogs.com/TenosDoIt/p/4047211.html

Calculate the area of any polygon.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.