Time Limit: 6000MS | Memory Limit: 65536K | |
Total Submissions: 8580 | Accepted: 3227 |
Description
Input
Output
Sample Input
3 10000 10000 0 5000 10000 5000 5000 10000 0 5000 5000 0
Sample Output
54166666.7
Hint
Source
【思路】
半平面交。
注意设置边界,输入向量方向和eps选择,1e-10足够。
【代码】
1 #include<cmath> 2 #include<cstdio> 3 #include<vector> 4 #include<cstring> 5 #include<algorithm> 6 #define FOR(a,b,c) for(int a=(b);a<=(c);a++) 7 using namespace std; 8 9 const int eps = 1e-10; 10 11 struct Pt { 12 double x,y; 13 Pt (double x=0,double y=0) :x(x),y(y) {} 14 }; 15 typedef Pt vec; 16 17 vec operator - (Pt a,Pt b) { return vec(a.x-b.x,a.y-b.y); } 18 vec operator + (vec a,vec b) { return vec(a.x+b.x,a.y+b.y); } 19 vec operator * (vec a,double x) { return vec(a.x*x,a.y*x); } 20 21 double cross(Pt a,Pt b) { return a.x*b.y-a.y*b.x; } 22 23 struct Line { 24 Pt p; vec v; double ang; 25 Line() {} 26 Line(Pt p,vec v) :p(p),v(v) { ang=atan2(v.y,v.x); } 27 bool operator < (const Line& rhs) const { 28 return ang < rhs.ang; 29 } 30 }; 31 //p在l的左边 32 bool onleft(Line L,Pt p) { return cross(L.v,p-L.p)>0; } 33 Pt getLineInter(Line a,Line b) { 34 vec u=a.p-b.p; 35 double t=cross(b.v,u)/cross(a.v,b.v); 36 return a.p+a.v*t; 37 } 38 39 vector<Pt> HPI(vector<Line> L) { 40 int n=L.size(); 41 sort(L.begin(),L.end()); 42 int f,r; 43 vector<Pt> p(n) , ans; 44 vector<Line> q(n); 45 q[f=r=0]=L[0]; 46 for(int i=1;i<n;i++) { 47 while(f<r && !onleft(L[i],p[r-1])) r--; 48 while(f<r && !onleft(L[i],p[f])) f++; 49 q[++r]=L[i]; 50 if(fabs(cross(q[r].v,q[r-1].v))<eps) { 51 r--; 52 if(onleft(q[r],L[i].p)) q[r]=L[i]; 53 } 54 if(f<r) p[r-1]=getLineInter(q[r-1],q[r]); 55 } 56 while(f<r && !onleft(q[f],p[r-1])) r--; 57 if(r-f<=1) return ans; 58 p[r]=getLineInter(q[r],q[f]); 59 for(int i=f;i<=r;i++) ans.push_back(p[i]); 60 return ans; 61 } 62 vector<Line> L; 63 vector<Pt> p; 64 int n; 65 66 int main() { 67 //freopen("in.in","r",stdin); 68 //freopen("out.out","w",stdout); 69 scanf("%d",&n); 70 double x1,y1,x2,y2; 71 for(int i=0;i<n;i++) { 72 scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); 73 Pt a(x1,y1) , b(x2,y2); 74 L.push_back(Line(a,b-a)); 75 } 76 Pt a(0,0),b(10000,0),c(10000,10000),d(0,10000); 77 L.push_back(Line(a,b-a)); 78 L.push_back(Line(b,c-b)); 79 L.push_back(Line(c,d-c)); 80 L.push_back(Line(d,a-d)); 81 p = HPI(L); 82 double ans=0; int m=p.size(); 83 for(int i=1;i<m-1;i++) 84 ans += cross(p[i]-p[0],p[i+1]-p[0]); 85 printf("%.1lf",ans/2); 86 return 0; 87 }