2618: [Cqoi2006] Convex polygon Time Limit:5 Sec Memory limit:128 MB
submit:878 solved:450
[Submit] [Status] [Discuss] The vertex coordinates of n convex polygons are given by Description , and the area of their intersection is obtained. For example, when n=2, two convex polygons are shown below:
The area of the intersecting part is 5.233. Input
The first line has an integer n, which indicates the number of polygons, and the following describes each polygon in turn. The first line of the I polygon contains an integer mi that represents the number of sides of the polygon, the following MI line is two integers per line, and the coordinates of each vertex are counterclockwise.
Output
The output file contains only a real number that represents the area of the intersection and retains three decimal places.
Sample Input 2
6
-2 0
-1-2
1-2
2 0
1 2
-1 2
4
0-3
1-1
2 2
-1 0
Sample Output 5.233 HINT
100% Data satisfaction: 2<=n<=10,3<=mi<=50, integers in each dimension coordinates [ -1000,1000]
Area of half plane intersection
Judge function written in >-eps on WA a point, metaphysics problem ...
#include <iostream> #include <cstdio> #include <cmath> #include <cstdlib> #include <cstring > #include <algorithm> #define F (i,j,n) for (int i=j;i<=n;i++) #define D (i,j,n) for (int i=j;i>=n;i--) #
Define ll long #define MAXN 1000 #define EPS 1e-8 using namespace std;
int N,m,cnt,tot;
Double ans; struct p{double x,y;}
P[MAXN],A[MAXN]; struct l{p a,b;double angle;}
L[MAXN],Q[MAXN];
inline int read () {int X=0,f=1;char ch=getchar (); while (ch< ' 0 ' | |
Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();}
while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();}
return x*f; } inline P operator-(p a,p b) {return (P) {a.x-b.x,a.y-b.y};} inline double operator * (P a,p b) {return a.x*b.y-a.y*b.x;}
line bool operator < (L l1,l L2) {if (Fabs (l1.angle-l2.angle) <=eps) return (l1.a-l2.a) * (l1.b-l2.a) >0;
else return l1.angle<l2.angle;
} inline P inter (L l1,l L2) {double k1= (l2.b-l1.a) * (l1.b-l1.a), k2= (l1.b-l1.a) * (l2.a-l1.a), t=k1/(K1+K2); Return (P) {l2.b.x+ (l2.a.x-l2.b.x) *t,l2.b.y+ (L2.A.Y-L2.B.Y) *t};
} inline bool Judge (L a,l b,l t) {P p=inter (a,b);
Return (P-T.A) * (T.B-T.A) >eps;
} inline void HPi () {sort (l+1,l+cnt+1);
int head=1,tail=0;tot=1;
F (i,2,cnt) {if (Fabs (l[i].angle-l[i-1].angle) >eps) tot++;
L[tot]=l[i];
} Cnt=tot;
Q[++TAIL]=L[1];Q[++TAIL]=L[2];
F (i,3,cnt) {while (Head<tail&&judge (q[tail],q[tail-1],l[i)) tail--;
while (Head<tail&&judge (Q[head],q[head+1],l[i])) head++;
Q[++tail]=l[i];
while (Head<tail&&judge (Q[tail],q[tail-1],q[head])) tail--;
tot=0;
F (I,HEAD,TAIL-1) a[++tot]=inter (q[i],q[i+1]);
A[++tot]=inter (Q[head],q[tail]);
int main () {n=read ();
while (n--) {m=read ();
F (i,1,m) P[i].x=read (), P[i].y=read ();
F (i,1,m-1) l[++cnt]= (l) {p[i],p[i+1]};
L[++cnt]= (L) {p[m],p[1]};
} F (i,1,cnt) l[i].angle=atan2 (l[i].b.y-l[i].a.y,l[i].b.x-l[i].a.x);
HPI ();
A[TOT+1]=A[1];
F (I,1,tot) ans+=a[i]*a[i+1];
printf ("%.3lf\n", ANS/2);
return 0;
}