1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
#include<bits/stdc++.h> using namespace std; const double pi = acos(-1); const double eps = 1e-8; int sign(double x) { if (fabs(x) < eps) return 0; if (x < 0) return -1; return 1; } struct Point{ double x, y; Point(double x = 0, double y = 0) : x(x), y(y) { } Point operator +(Point B){return Point(x+B.x,y+B.y);} Point operator -(Point B){return Point(x-B.x,y-B.y);} Point operator *(double k){return Point(x*k,y*k);} Point operator /(double k){return Point(x/k,y/k);} bool operator == (Point B){return sign(x-B.x)==0&&sign(y-B.x)==0;} bool operator <(Point B) {return sign(x-B.x)<0||sign(x-B.x)==0&&sign(y-B.y)<0;}
}; typedef Point Vector; typedef Point PT; double cross(Point a, Point b){
return a.x * b.y - b.x * a.y; } double area(Point a, Point b, Point c){
return cross(a - b, a - c); } int to_cmp(double x, double y){ if (fabs(x - y) < eps) return 0; if (x < y) return -1; return 1; } double get_angle(Point a, Point b){
return atan2(a.y - b.y, a.x - b.x); } Point get_line_intersection(Point p, Vector v, Point q, Vector w) {
Vector u = p - q; double t = cross(w, u) / cross(v, w); return p + v * t; } const int N=1e5+10; int n,m; int cnt,q[N]; Point pg[N],res[N]; struct Line { Point st, ed; bool operator<(Line TB){
double A = get_angle(st,ed), B = get_angle(TB.st,TB.ed); if(!to_cmp(A, B)) return area(st, ed, TB.ed) < 0; return A < B; }
}line[N];
Point 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) { Point o = get_line_intersection(b, c); return sign(area(a.st, a.ed, o)) <= 0; } double get_angle(Line a) { return get_angle(a.st,a.ed); } double half_plane_intersection() { sort(line, line + cnt); int hh = 0, tt = -1; for(int i = 0; i < cnt; i++) { if(i && !to_cmp(get_angle(line[i]), get_angle(line[i - 1]))) continue; while(hh + 1 <= tt && on_right(line[i], line[q[tt - 1]], line[q[tt]])) tt--; while(hh + 1 <= tt && on_right(line[i], line[q[hh]], line[q[hh + 1]])) hh++; q[++tt] = i; } while(hh + 1 <= tt && on_right(line[q[hh]], line[q[tt - 1]], line[q[tt]])) tt--; while(hh + 1 <= tt && on_right(line[q[tt]], line[q[hh]], line[q[hh + 1]])) hh++;
q[++tt] = q[hh]; int k = 0; for(int i = hh; i < tt; i++) res[k++] = get_line_intersection(line[q[i]], line[q[i + 1]]);
double ans = 0; for(int i = 1; i + 1 < k; i++) ans += area(res[0], res[i], res[i + 1]); return ans / 2; } void solve(){ cin>>n; while(n--){ cin>>m; for(int i = 0; i < m; i++) cin>>pg[i].x>>pg[i].y; for(int i = 0; i < m; i++) line[cnt++] = {pg[i], pg[(i + 1) % m]}; } double res= half_plane_intersection(); cout<<fixed<<setprecision(3)<<res; } signed main(){ solve(); }
|