题目:http://poj.org/problem?id=2451
题意:话说没怎么看,不过大概是说给个0,0,到10000,10000的正方形,和一些半平面,求划分后剩下的面积的大小
分析:这题数据较大,不过比较简单,只要你的模板是O(nlogn)的复杂度的话,直接套用即可
注意,有关求面积的都要判断下剩下的半平面是否足够3个,要不会wa的,还有套模板的,记得看看数组大小T_T
代码:
#include<cmath> #include<cstdio> #include<iostream> #include<algorithm> using namespace std; const int mm=22222; const double eps=1e-8; typedef double diy; struct point { diy x,y; point(){} point(diy _x,diy _y):x(_x),y(_y){} }g[mm]; point Vector(point s,point t) { return point(t.x-s.x,t.y-s.y); } diy CrossProduct(point P,point Q) { return P.x*Q.y-P.y*Q.x; } diy MultiCross(point P,point Q,point R) { return CrossProduct(Vector(Q,P),Vector(Q,R)); } struct halfPlane { point s,t; diy angle; halfPlane(){} halfPlane(point _s,point _t){s=_s,t=_t,angle=atan2(t.y-s.y,t.x-s.x);} }hp[mm],q[mm]; point Intersection(halfPlane P,halfPlane Q) { diy a1=CrossProduct(Vector(P.s,Q.t),Vector(P.s,Q.s)); diy a2=CrossProduct(Vector(P.t,Q.s),Vector(P.t,Q.t)); return point((P.s.x*a2+P.t.x*a1)/(a1+a2),(P.s.y*a2+P.t.y*a1)/(a1+a2)); } bool IsParallel(halfPlane P,halfPlane Q) { return fabs(CrossProduct(Vector(P.s,P.t),Vector(Q.s,Q.t)))<eps; } bool cmp(halfPlane P,halfPlane Q) { if(fabs(P.angle-Q.angle)<eps) return MultiCross(P.s,P.t,Q.s)>0; return P.angle<Q.angle; } void HalfPlaneIntersect(int n,int &m) { sort(hp,hp+n,cmp); int i,l=0,r=1; for(m=i=1;i<n;++i) if(hp[i].angle-hp[i-1].angle>eps)hp[m++]=hp[i]; n=m,m=0; q[0]=hp[0],q[1]=hp[1]; for(i=2;i<n;++i) { if(IsParallel(q[r],q[r-1])||IsParallel(q[l],q[l+1]))break; while(l<r&&MultiCross(hp[i].s,hp[i].t,Intersection(q[r],q[r-1]))>0)--r; while(l<r&&MultiCross(hp[i].s,hp[i].t,Intersection(q[l],q[l+1]))>0)++l; q[++r]=hp[i]; } while(l<r&&MultiCross(q[l].s,q[l].t,Intersection(q[r],q[r-1]))>0)--r; while(l<r&&MultiCross(q[r].s,q[r].t,Intersection(q[l],q[l+1]))>0)++l; q[++r]=q[l]; for(i=l;i<r;++i) g[m++]=Intersection(q[i],q[i+1]); } int main() { int i,n,m; diy sx,sy,tx,ty,ans; while(~scanf("%d",&n)) { for(i=0;i<n;++i) { scanf("%lf%lf%lf%lf",&sx,&sy,&tx,&ty); hp[i]=halfPlane(point(sx,sy),point(tx,ty)); } hp[n++]=halfPlane(point(0,0),point(1e4,0)); hp[n++]=halfPlane(point(1e4,0),point(1e4,1e4)); hp[n++]=halfPlane(point(1e4,1e4),point(0,1e4)); hp[n++]=halfPlane(point(0,1e4),point(0,0)); HalfPlaneIntersect(n,m); g[m]=g[0]; ans=0; if(m>2) { for(i=0;i<m;++i) ans+=CrossProduct(g[i],g[i+1]); if(ans<0)ans=-ans; } printf("%.1lf\n",ans/2); } return 0; }