Calculate the area of a polygon.

Source: Internet
Author: User
Tags x2 y2

Question: enter a dot column and link them to a closed polygon to calculate the area of the polygon.

Analysis: Method 1: The calculated area can be in the form of fixed points. Fixed Points have positive and negative values, sum sequentially, and duplicate parts offset each other. The absolute value of the remaining total area is the area of the polygon.

From the result of Linear Integration, we can easily see that the integral of a straight line segment is actually the area of the trapezoid Int (P1, P2) in the area enclosed by the line segment and the X axis) = Int (k * x + B, P1.x, P2.x) = 0.5 * (P2.x-P1.x) * (P2.y + P1.y), slope k = (P1.y-P2.y) /(P1.x-P2.x), intercept B = P1.y-k * P1.x;

The complexity of the algorithm is O (N), and N is the number of vertices.

[Cpp]
Struct Point {
Float x, y;
};
Float LinearIntegration (const Point & p1, const Point & p2 ){
Return 0.5 * (p2.x-p1.x) * (p2.y + p1.y );
}
Float ComputePolygonArea (const Point points [], int length ){
If (points = NULL | length <= 0) return 0.0;
Float area = 0.0;
For (int I = 0; I <length-1; ++ I ){
Area + = LinearIntegration (points [I], points [I + 1]);
}
Area + = LinearIntegration (points [length-1], points [0]);
Return area >=0.0? Area:-area;
}
Method 2: considering that the coordinates of the three vertices of a triangle on the plane can be used to determine the area of the triangle directly using the det. For example, P1 (x1, y1), P2 (x2, y2), P3 (x3, y3 ),

S (P1, P2, P3) = det [x1 y1 1; x2 y2 1; x3 y3 1] * 0.5 = [(x2-x1) * (y3-y1) -(x3-x1) * (y2-y1)] * 0.5;

You can find any point in the plane of the polygon, form a directed triangle with each side of the polygon, and obtain the area of the directed triangle sequentially, and then add, because the directed area is similar to the above-mentioned fixed point, the positive and negative area can offset the duplicate part, and the absolute value of the remaining area is the area of the polygon.

[Cpp]
Struct Point {
Float x, y;
Point () {x = 0.0; y = 0.0 ;}
Point (float _ x, float _ y) {x = _ x; y = _ y ;}
};
Float ComputeTriangleArea (const Point & p1, const Point & p2, const Point & p3 ){
Return 0.5 * (p2.x-p1.x) * (p3.y-p1.y)-(p3.x-p1.x) * (p2.y-p1.y ));
}
Float ComputePolygonAreaTri (const Point points [], int length ){
If (points = NULL | length <= 0) return 0.0;
Point p0 (0.0, 0.0 );
Float area = 0.0;
For (int I = 0; I <length-1; ++ I ){
Area + = ComputeTriangleArea (p0, points [I], points [I + 1]);
}
Area + = ComputeTriangleArea (p0, points [length-1], points [0]);
Return area >=0.0? Area:-area;
}

 

Related Article

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.