hdu - 2036 改革春风吹满地(多边形面积计算(向量叉乘))
为了方便计算,选取坐标0,0为所有向量的起点,将多边形分成若干个三角形,计算所三角形组成的有向面积并累加求和再取绝对值
#include<cstdio>
#include<stack>
#include<queue>
#include<cmath>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#define INF 233333333333333
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int maxn = 105;
P point[maxn];
int main(void) {
int n;
while(cin >> n && n) {
for (int i = 0; i<n; i++)
cin >> point[i].first >> point[i].second;
point[n] = point[0];
double sum = 0;
for (int i = 0; i<=n-1; i++)
sum += 1.0*(point[i].first*point[i+1].second - point[i+1].first*point[i].second);
printf("%.1f\n", fabs(sum)/2.0);
}
return 0;
}