求多边形面积,选定(0,0)点,用三角形法求,求三角形面积时用叉积,把式子展开并合并同类项可得模板中的公式。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
#define maxn 101
struct XPoint
{
double x, y;
}point[maxn];
int n;
void input()
{
for (int i = 0; i < n; i++)
{
scanf("%lf%lf", &point[i].x, &point[i].y);
}
}
double areaofp(int vcount, XPoint plg[])
{
int i;
double s;
if (vcount<3) return 0;
s=plg[0].y*(plg[vcount-1].x-plg[1].x);
for (i=1;i<vcount;i++)
s+=plg[i].y*(plg[(i-1)].x-plg[(i+1)%vcount].x);
return s/2;
}
int main()
{
//freopen("D:\\t.txt", "r", stdin);
while (scanf("%d", &n) != EOF && n != 0)
{
input();
printf("%.1f\n", areaofp(n, point));
}
return 0;
}