nyoj-1011-So Easy[II] (多边形面积求解)

题目链接

 1 /*
 2     Name:nyoj-1011-So Easy[II] 
 3     Copyright:
 4     Author:
 5     Date: 2018/4/26 17:12:09
 6     Description:
 7     将多边形,从第一个顶点出发,分为若干个小的三角形
 8     通过叉乘计算三角形面积 
 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
", fabs(area));//只能在最后加上绝对值,计算过程中可能出现负的 
39     }
40     return 0;
41 }
原文地址:https://www.cnblogs.com/slothrbk/p/8954006.html