#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<queue>
#include<vector>
#include<map>
#include<set>
#include<cstdlib>
#include<ctime>
using namespace std;
#define LL long long
#define uLL unsigned long long
#define lowbit(x) ((x)&(-x))
#define For(x,i,j) for(int x=(i);x<=(j);x++)
#define FOR(x,i,j) for(int x=(i);x>=(j);x--)
#define ls(o) (o<<1)
#define rs(o) (o<<1|1)
#define debug(x) cout<<"debug : "<<x<<endl;
inline int read(){
int x=0,w=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
return x*w;
}
#define N 505
int cnt;
int q[N];
const double eps=1e-8;
struct Vector{
double x,y;
Vector(double a=0,double b=0){x=a,y=b;}
Vector operator+(Vector a){return Vector(x+a.x,y+a.y);}
Vector operator-(Vector a){return Vector(x-a.x,y-a.y);}
Vector operator*(double a){return Vector(x*a,y*a);}
Vector operator/(double a){return Vector(x/a,y/a);}
double len(){return sqrt(x*x+y*y);}
double dis(Vector a){return Vector(a-(*this)).len();}
double dot(Vector a){return x*a.x+y*a.y;}
double cross(Vector a){return x*a.y-y*a.x;}
bool operator<(Vector a)const{return (x<a.x)||(x==a.x&&y<a.y);}
}pg[N],ans[N];
struct Line{
Vector st,ed;
Line(Vector a={0,0},Vector b={0,0}){st=a,ed=b;}
}line[N];
int dcmp(double x){
if(fabs(x)<eps) return 0;
if(x<0) return -1;
return 1;
}
double get_angle(Line x){
return atan2(x.ed.y-x.st.y,x.ed.x-x.st.x);
}
double area(Vector a,Vector b,Vector c){
return Vector(b-a).cross(c-a);
}
bool cmp(const Line &x,const Line &y){
double a=get_angle(x),b=get_angle(y);
if(!dcmp(a-b)) return area(x.st,x.ed,y.ed)<0;
return a<b;
}
Vector get_line_intersection(Vector p,Vector v,Vector q,Vector w){
Vector u=p-q;
double t=w.cross(u)/v.cross(w);
return {p.x+t*v.x,p.y+t*v.y};
}
Vector get_line_intersection(Line a,Line b){
return get_line_intersection(a.st,a.ed-a.st,b.st,b.ed-b.st);
}
bool on_right(Line a,Line b,Line c){
Vector o=get_line_intersection(b,c);
return dcmp(area(a.st,a.ed,o))<=0;
}
double half_plane_intersection(){
sort(line+1,line+cnt+1,cmp);
int hh=1,tt=0;
for(int i=1;i<=cnt;i++){
if(i&&!dcmp(get_angle(line[i])-get_angle(line[i-1]))) continue;
while(hh<tt&&on_right(line[i],line[q[tt-1]],line[q[tt]])) tt--;
while(hh<tt&&on_right(line[i],line[q[hh]],line[q[hh+1]])) hh++;
q[++tt]=i;
}
while(hh<tt&&on_right(line[hh],line[q[tt-1]],line[q[tt]])) tt--;
while(hh<tt&&on_right(line[tt],line[q[hh]],line[q[hh+1]])) hh++;
q[++tt]=q[hh];
int k=0;
for(int i=hh;i<tt;i++)
ans[++k]=get_line_intersection(line[q[i]],line[q[i+1]]);
double ret=0;
for(int i=2;i<=k-1;i++)
ret+=area(ans[1],ans[i],ans[i+1]);
return ret/2;
}
int main(){
int n,m;
cin>>n;
while(n--){
cin>>m;
for(int i=1;i<=m;i++) cin>>pg[i].x>>pg[i].y;
for(int i=1;i<=m;i++)
line[++cnt]=Line(pg[i],pg[i%m+1]);
}
printf("%.3lf\n",half_plane_intersection());
return 0;
}
对于下面这组数据
2
3
0 0
2 0
0 3
4
1 1
3 1
3 3
1 3
我的输出:
0.00
正确输出:
0.083
不知是哪一步出了问题QAQ