向量的叉乘求解任意多边形的面积,给出的点坐标要顺时针(结果为负)或逆时针(结果为正)
//任意多边形面积=向量的叉乘
//
#include <iostream>
#include <cmath>
using namespace std;
typedef struct node
{
double x, y;
}Tpoint;
double polygonArea( Tpoint p[], int n )
{
double area = 0;
int i, j;
for( i=0; i<n; i++ )
{
j = (i+1)%n;
area += p[i].x*p[j].y - p[j].x*p[i].y;
}
area /= 2.0;
return fabs(area);
}
int main()
{
Tpoint p[105];
int n;
while(scanf("%d", &n) && n)
{
int i;
for( i=0; i < n; i++ )
scanf("%lf%lf",&p[i].x, &p[i].y);
printf("%.1lf\n", polygonArea(p, n));
}
return 0;
}