Returns the area of any polygon.

Source: Internet
Author: User

Address: http://blog.csdn.net/sun_shine_/article/details/18799739

Given the vertex coordinates (ordered) of a polygon, how do you obtain the area of the polygon?
We know that any polygon can be divided into N triangles. Therefore, if we take this as the breakthrough point, the first step is to divide the given polygon into several triangles and calculate the area separately, finally, we can accumulate the polygon and divide the Polygon into triangles in a variety of ways. Here, we divide the polygon according to the method shown in the following figure:

Figure 1

Point S is used as the starting point (point 1), and point a-> E is used as the point 2, 3... in sequence .......
What is the area of a triangle?
Based on the knowledge of linear algebra, we have the following Triangle Area Formula, called the directed area (signed area ):

Expand this criterion in the third column to obtain:

This is the directed area of a triangle composed of points 1, 2, and 3 (if the point is clockwise, the directed area is negative, and the directed area is positive ), then, let's continue our work and use the Triangle Area Formula to get the polygon area formula:
For Figure 1, the polygon area is:
S (1-> 6) = S (1, 2, 3) + S (1, 3, 4) + S (1, 5) + S (1, 5, 6)
Here we have some questions. First, figure 1 shows a convex polygon. Does this algorithm also apply to non-Convex Polygon? For example, the following figure shows the simplest Convex Polygon:

Figure 2

Using the division method just now, there will be a strange problem, that is, a triangle appears outside the graph, the other is out of the polygon range (divided into SAB and SBC). Is the result correct when we use the formula just now to calculate the area?
S (1-> 4) = S (1, 2, 3) + S (1, 3, 4)
First, I will announce the conclusion that this formula is correct. Why? Remember the concept of "directed area" I mentioned just now? If you forget it, please look back at the words that have been aggravated.
Please note that the SAB points are arranged clockwise and SBC points are arranged counterclockwise. The area is removed from the larger triangle beyond the SAB value, the final result is the area of the polygon SABC. Can this conclusion be extended to any polygon?

Figure 3

No proof is provided here. The formula below is the area formula of Any Polygon:

Example:

Input

The input data contains multiple test instances. Each test instance occupies one row. Each row starts with an integer N (3 <=n <= 100 ), it represents the number of edges of a polygon (of course also the number of vertices), and then the coordinates of N vertices (x1, Y1, X2, y2... XN, yn). To simplify the problem, all coordinates here are represented by integers.
All the integers in the input data are within the 32-bit integer range. n = 0 indicates the end of the data and is not processed.

Output

For each test instance, output the corresponding polygon area, and the result is accurate to one decimal place after the decimal point.
The output of each instance occupies one row.

Sample Input

3 0 0 1 0 14 1 0 0 1-1 0 0-10

Sample output

0.52.0

Code:

#include<cstdio>#include<cmath>int main(){     int n;     int p[110][2];     while(scanf("%d",&n)&& n){          for(int i = 0; i < n; i ++)          scanf("%d%d",&p[i][0],&p[i][1]);          p[n][0] = p[0][0];          p[n][1] = p[0][1];          int s = 0;          for(int i = 0; i < n; i ++){               s += (p[i][0]*p[i+1][1] - p[i+1][0]*p[i][1]);          }          printf("%.1lf\n",s/2.0);     }return 0;}
View code

Note that the above formula is cyclic for a week, and finally XN * Y1-X1 * YN should be added =

 

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.