Solving the area (inside the plane) of any polygon and solving the area plane
The calculation of the polygon in the plane, that is, the calculation of the polygon in the plane coordinate system. It is known that the coordinates of each point are sequential, clockwise or clockwise. Calculate the area based on the given coordinates.
Here is a simple method to calculate the area of a polygon by using integral points. The area can be obtained based on the sum of points on the X axis of each edge. Note that if you calculate points and obtain area in clockwise direction, the opposite number of area is counter-clockwise. If you do not understand it, you can verify it by yourself. The principle is very simple and is not described in detail.
1 # include <iostream> 2 using namespace std; 3 // calculate the integral 4 double cal of an edge (int x1, int y1, int x2, int y2) {5 return (x2-x1) * (y1 + y2)/2.0; 6} 7 int main () {8 int x0, y0, x1, y1, x2, y2; 9 int n; 10 while (cin> n & n! = 0) {// calculate the points of each edge cyclically 11 cin> x1> y1> x2> y2; 12x0 = x1; y0 = y1; 13 double sum = 0; 14 sum + = cal (x1, y1, x2, y2); 15 for (int I = 3; I <= n; I ++) {16x1 = x2; y1 = y2; 17 cin> x2> y2; 18 sum + = cal (x1, y1, x2, y2 ); 19} 20 sum + = cal (x2, y2, x0, y0); // link points of the last vertex and the first vertex. 21 printf ("%. 1f \ n",-sum); 22} 23 return 0; 24}