多边形面积的计算,一般都是用三角形的面积来累加。
为了避免重复,我们要规定累加的方向。
所以,向量的乘法在这里就有用了
因为S=1/2*a*b*Sin<θ>
所以可以用叉乘来表示。
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct point
{
int x,y;
}po[200];
int main()
{
int n;
int i;
int a,b;
double s;
while(scanf("%d",&n)&&n)
{
for(i=0;i<n;i++)
scanf("%d%d",&po[i].x,&po[i].y);
po[n]=po[0];
a=b=0;
s=0;
for(i=0;i<=n;i++)
{
s+=(a*po[i].y-b*po[i].x)*1.0/2;
a=po[i].x;
b=po[i].y;
}
printf("%.1lf\n",s);
}
return 0;
}