求多边形的面积(模板)
程序员文章站
2022-03-30 13:09:05
...
#include <bits/stdc++.h>
using namespace std;
const int maxn=0x3f3f3f;
struct node{
double x;
double y;
};
node G[maxn];
int n;
double Cross(node a, node b) { // 叉积计算
return a.x*b.y - a.y*b.x;
}
int main()
{
while (scanf("%d", &n) != EOF && n) { //输入边数
for (int i = 0; i < n; i++)
scanf("%lf %lf", &G[i].x, &G[i].y);
double sum = 0;
G[n].x = G[0].x;
G[n].y = G[0].y;
for (int i = 0; i < n; i++) { //输入n个点的坐标
sum += Cross(G[i], G[i + 1]);
}
sum = sum / 2.0;
printf("%.1f\n", sum);
}
return 0;
}
若证明,以下篇博客写的挺好
http://blog.csdn.net/tigercoder/article/details/70161646
下一篇: 过分了啊老太太