觉得巨恶心不知道怎么办,结果一看题解发现因为凸多边形相交还是一个多边形,并不会变成多个奇怪的很多个小图形,所以可以直接半平面交求面积,测板子+1,过样例就A真爽。
#include<bits/stdc++.h>
#define maxl 1010
#define eps 1e-8
using namespace std;
inline int sgn(double x)
{
if(x>-eps && x<eps) return 0;
if(x>0) return 1;
else return -1;
}
struct point
{
double x,y;
point(double a=0,double b=0)
{
x=a;y=b;
}
point operator + (const point &b)const
{
return point(x+b.x,y+b.y);
}
point operator - (const point &b)const
{
return point(x-b.x,y-b.y);
}
friend point operator * (double t,point p)
{
return point(t*p.x,t*p.y);
}
};
inline double dot(const point &a,const point &b)
{
return a.x*b.x+a.y*b.y;
}
inline double det(const point &a,const point &b)
{
return a.x*b.y-a.y*b.x;
}
struct halfplane
{
point s,e;
double k;
halfplane(point a=point(),point b=point())
{
s=a;e=b;
k=atan2(e.y-s.y,e.x-s.x);
}
point operator & (const halfplane &b)const
{
double t=det(b.s-s,b.e-b.s);
t=t/det(e-s,b.e-b.s);
return s+t*(e-s);
}
};
int n,m[maxl],cnt;
point a[maxl][maxl],res[maxl];
halfplane line[maxl*maxl];
double ans;
inline void prework()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&m[i]);
for(int j=1;j<=m[i];j++)
scanf("%lf%lf",&a[i][j].x,&a[i][j].y);
a[i][0]=a[i][m[i]];
for(int j=0;j<m[i];j++)
line[cnt++]=halfplane(a[i][j],a[i][j+1]);
}
}
inline bool satisfy(point a,const halfplane L)
{
return sgn(det(a-L.s,L.e-L.s))<=0;
}
inline bool HPIcmp(const halfplane &a,const halfplane &b)
{
int res=sgn(a.k-b.k);
return res==0 ? det(a.s-b.s,b.e-b.s)<0 : a.k<b.k;
}
halfplane Q[maxl];
inline void HPI(halfplane line[],int n,point res[],int &resn)
{
int tot=n;
sort(line,line+n,HPIcmp);
tot=1;
for(int i=1;i<n;i++)
if(sgn(line[i].k-line[i-1].k)!=0)
line[tot++]=line[i];
int head=0,tail=1;
Q[0]=line[0];Q[1]=line[1];resn=0;
for(int i=2;i<tot;i++)
{
if(sgn(det(Q[tail].e-Q[tail].s,Q[tail-1].e-Q[tail-1].s))==0
|| sgn(det(Q[head].e-Q[head].s,Q[head+1].e-Q[head+1].s))==0)
return;
while(head<tail && !satisfy(Q[tail]&Q[tail-1],line[i]))
tail--;
while(head<tail && !satisfy(Q[head]&Q[head+1],line[i]))
head++;
Q[++tail]=line[i];
}
while(head<tail && !satisfy(Q[tail]&Q[tail-1],Q[head]))
tail--;
while(head<tail && !satisfy(Q[head]&Q[head+1],Q[tail]))
head++;
if(tail<=head+1) return;
for(int i=head;i<tail;i++)
res[resn++]=Q[i]&Q[i+1];
if(head<tail-1);
res[resn++]=Q[head]&Q[tail];
}
inline double get_area(point a[],int n)
{
double area=0;
a[n]=a[0];
for(int i=0;i<n;i++)
area+=det(a[i],a[i+1]);
return fabs(area/2);
}
inline void mainwork()
{
int resn=0;
HPI(line,cnt,res,resn);
ans=get_area(res,resn);
}
inline void print()
{
printf("%.3f",ans);
}
int main()
{
prework();
mainwork();
print();
return 0;
}