nyoj-1011-So Easy[II] (polygon area solution)

topic link

1  /* 
2      Name:nyoj-1011-So Easy[II] 
 3      Copyright:
 4      Author:
 5      Date: 2018/4/26 17:12:09
 6      Description:
 7The      polygon, starting from the first vertex, is divided into Several small triangles
 8      calculate the triangle area by cross product 
 9  */ 
10 #include <cstring>
 11 #include <iostream>
 12 #include <cstdio>
 13 #include <cmath>
 14  using  namespace std;
 15  struct node{
 16      double x ,y;
 17 } arr[ 105 ];
18 double cross(node a,node b1,node b2){//求(b1-a) 和(b2-a) 的叉乘 
19     double x1,y1,x2,y2;
20     x1=b1.x-a.x;
21     y1=b1.y-a.y;
22     x2=b2.x-a.x;
23     y2=b2.y-a.y;   
24     return x1*y2-x2*y1;
25 }
26 int main()
27 {
28     int n;
29     while (cin>>n) {
30         memset(arr, 0, sizeof(arr));
31          for ( int i= 0 ; i<n; i++ ) {
 32              cin>>arr[i].x>> arr[i].y;
 33          }
 34          double area = 0 ;
 35          for ( int i= 2 ; i<n; i++ ) {
 36              area += cross(arr[ 0 ], arr[i- 1 ], arr[i]) / 2.0 ;
 37          }
 38          printf( " %.2lf\n " , fabs(area) ); // Only add absolute value at the end, negative 
39 may appear during calculation     }
40     return 0;
41 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324974630&siteId=291194637