题意就是按逆时针或者顺时针的顺序给n边形的n个点
计算出n边形的面积
比如给这个1,2,3,4,5,6
依次算出三角形的面积就再叠加就可以了,不过注意用叉积的话因为可能是顺时针也可能是逆时针,最后结果要判正负
我画的是凸多边形,其实凹多边形也一样,当成凸多边形处理就可以,用叉积算的时候多出来的部分会抵消的
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
class coordinate
{
public:
double x;
double y;
double cross(coordinate a)//叉积
{
return x*a.y-a.x*y;
}
};
coordinate operator-(coordinate a,coordinate b)
{
coordinate t;
t.x=a.x-b.x;
t.y=a.y-b.y;
return t;
}
int main()
{
int n;
while(scanf("%d",&n)!=-1)
{
double ans=0;
coordinate a,b,c;
scanf("%lf %lf",&a.x,&a.y);
scanf("%lf %lf",&b.x,&b.y);
scanf("%lf %lf",&c.x,&c.y);
ans+=(b-a).cross(c-a);
coordinate t1,t2;
t1=b-a;
t2=c-a;
for(int i=3;i<n;i++)
{
coordinate temp;
scanf("%lf %lf",&temp.x,&temp.y);
t1=t2;
t2=temp-a;
ans+=t1.cross(t2);
}
if(ans<0)
{
ans*=-1;
}
printf("%.2lf\n",ans/2);
}
return 0;
}