#include<bits/stdc++.h> #define VAR(i,n) __typeof(n) i = (n) #define loop(i,j,s) for(int i=j;i<s;i++) #define loopback(i,j,s) for(int i=j;i>=s;i--) #define foreach(i,c) for(VAR(i,(c).begin());i!=(c).end();i++) #define pln( x ) cout << x << "\n" #define ps( x ) cout << x << " " #define entr cout << "\n" #define pcnt(i) __builtin_popcount(i) #define ll long long #define pb push_back #define mp make_pair #define ff first #define ss second #define SIZE(c) (c).size() #define ALL(c) (c).begin(), (c).end() using namespace std; typedef vector<int> VI; const int INFTY=20000000; const int MAX=110; const int MOD=10000000; void coutTab(int* tab,int n){ loop(i,0,n){ cout<<tab[i]<<" "; } cout<<"\n"; } //------------------------------------------ struct Point{ double x, y; double ang; }; typedef pair<Point, Point> ppp; typedef vector<Point> vp; vector<ppp> pps(MAX); int n; double prodVec(Point p0,Point p1, Point p2){ return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x); } vp convexHull(vp ps){ if(SIZE(ps)<2) return ps; int bi = 0; loop(i,1,SIZE(ps)){ if((ps[i].y==ps[bi].y&&ps[i].x<ps[bi].y)||ps[i].y<ps[bi].y){ bi = i; } } swap(ps[bi],ps[0]); Point bp = ps[0]; loop(i,0,SIZE(ps)){ ps[i].x-=bp.x; ps[i].y-=bp.y; } loop(i,1,SIZE(ps)){ ps[i].ang = (double)ps[i].x/(ps[i].x*ps[i].x+ps[i].y*ps[i].y); } sort(ps.begin()+1,ps.end(), [](Point a, Point b){ return a.ang>b.ang; }); stack<Point> S; S.push(ps[0]); S.push(ps[1]); loop(i,2,SIZE(ps)){ Point v = S.top(); S.pop(); Point r = S.top(); S.pop(); while(!S.empty()&&prodVec(r,v,ps[i])>=0){ v=r; r=S.top(); S.pop(); } S.push(r); S.push(v); S.push(ps[i]); } vp res; while(!S.empty()){ res.pb(S.top()); S.pop(); } loop(i,0,SIZE(res)){ res[i].x+=bp.x; res[i].y+=bp.y; } reverse(ALL(res)); return res; } bool inPol(vp pol, Point p){ pol = convexHull(pol); loop(i,0,SIZE(pol)){ if(prodVec(pol[i],pol[(i+1)%SIZE(pol)],p)>0) return false; } return true; } bool intLin(Point p1, Point p2, Point p3, Point p4){ if(prodVec(p1,p2,p3)*prodVec(p1,p2,p4)<0 && prodVec(p3,p4,p1)*prodVec(p3,p4,p2)<0) return true; return false; } vp intPol(vp pol1, vp pol2){ vp res; loop(i,0,SIZE(pol1)){ if(inPol(pol2,pol1[i])) res.pb(pol1[i]); } loop(i,0,SIZE(pol2)){ if(inPol(pol1,pol2[i])) res.pb(pol2[i]); } loop(i,0,SIZE(pol1)){ loop(j,0,SIZE(pol2)){ Point pi1 = pol1[i]; Point pi2 = pol1[(i+1)%SIZE(pol1)]; Point pj1 = pol2[j]; Point pj2 = pol2[(j+1)%SIZE(pol2)]; if(intLin(pi1,pi2,pj1,pj2)){ //ps(pi1.x);ps(pi2.x);pln("przeciecie"); double a1 = (double)(pi2.y-pi1.y)/(pi2.x-pi1.x); double b1 = pi2.y-a1*pi2.x; double a2 = (double)(pj2.y-pj1.y)/(pj2.x-pj1.x); double b2 = pj2.y-a1*pj2.x; Point inte; inte.x = (b2-b1)/(a1-a2); inte.y = a1*inte.x+b1; res.pb(inte); } } } for(auto r : res){ // ps(r.x);pln(r.y); } return res; } double field(vp pol){ double res=0; pol = convexHull(pol); loop(i,1,SIZE(pol)){ res += abs(prodVec(pol[0],pol[i],pol[(i+1)%SIZE(pol)]))/2.0; } return res; } int main(){ ios_base::sync_with_stdio(0); cin>>n; loop(i,0,n){ cin>>pps[i].ff.x>>pps[i].ff.y>>pps[i].ss.x>>pps[i].ss.y; } vp ps,res; loop(mask, 0, 1<<n){ ps.clear(); loop(j,0,n){ if(1<<j&mask){ ps.pb(pps[j].ss); }else{ ps.pb(pps[j].ff); } } vp hulled = convexHull(ps); if(mask == 0) res = hulled; else res = intPol(res,hulled); } pln(field(res)); }
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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | #include<bits/stdc++.h> #define VAR(i,n) __typeof(n) i = (n) #define loop(i,j,s) for(int i=j;i<s;i++) #define loopback(i,j,s) for(int i=j;i>=s;i--) #define foreach(i,c) for(VAR(i,(c).begin());i!=(c).end();i++) #define pln( x ) cout << x << "\n" #define ps( x ) cout << x << " " #define entr cout << "\n" #define pcnt(i) __builtin_popcount(i) #define ll long long #define pb push_back #define mp make_pair #define ff first #define ss second #define SIZE(c) (c).size() #define ALL(c) (c).begin(), (c).end() using namespace std; typedef vector<int> VI; const int INFTY=20000000; const int MAX=110; const int MOD=10000000; void coutTab(int* tab,int n){ loop(i,0,n){ cout<<tab[i]<<" "; } cout<<"\n"; } //------------------------------------------ struct Point{ double x, y; double ang; }; typedef pair<Point, Point> ppp; typedef vector<Point> vp; vector<ppp> pps(MAX); int n; double prodVec(Point p0,Point p1, Point p2){ return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x); } vp convexHull(vp ps){ if(SIZE(ps)<2) return ps; int bi = 0; loop(i,1,SIZE(ps)){ if((ps[i].y==ps[bi].y&&ps[i].x<ps[bi].y)||ps[i].y<ps[bi].y){ bi = i; } } swap(ps[bi],ps[0]); Point bp = ps[0]; loop(i,0,SIZE(ps)){ ps[i].x-=bp.x; ps[i].y-=bp.y; } loop(i,1,SIZE(ps)){ ps[i].ang = (double)ps[i].x/(ps[i].x*ps[i].x+ps[i].y*ps[i].y); } sort(ps.begin()+1,ps.end(), [](Point a, Point b){ return a.ang>b.ang; }); stack<Point> S; S.push(ps[0]); S.push(ps[1]); loop(i,2,SIZE(ps)){ Point v = S.top(); S.pop(); Point r = S.top(); S.pop(); while(!S.empty()&&prodVec(r,v,ps[i])>=0){ v=r; r=S.top(); S.pop(); } S.push(r); S.push(v); S.push(ps[i]); } vp res; while(!S.empty()){ res.pb(S.top()); S.pop(); } loop(i,0,SIZE(res)){ res[i].x+=bp.x; res[i].y+=bp.y; } reverse(ALL(res)); return res; } bool inPol(vp pol, Point p){ pol = convexHull(pol); loop(i,0,SIZE(pol)){ if(prodVec(pol[i],pol[(i+1)%SIZE(pol)],p)>0) return false; } return true; } bool intLin(Point p1, Point p2, Point p3, Point p4){ if(prodVec(p1,p2,p3)*prodVec(p1,p2,p4)<0 && prodVec(p3,p4,p1)*prodVec(p3,p4,p2)<0) return true; return false; } vp intPol(vp pol1, vp pol2){ vp res; loop(i,0,SIZE(pol1)){ if(inPol(pol2,pol1[i])) res.pb(pol1[i]); } loop(i,0,SIZE(pol2)){ if(inPol(pol1,pol2[i])) res.pb(pol2[i]); } loop(i,0,SIZE(pol1)){ loop(j,0,SIZE(pol2)){ Point pi1 = pol1[i]; Point pi2 = pol1[(i+1)%SIZE(pol1)]; Point pj1 = pol2[j]; Point pj2 = pol2[(j+1)%SIZE(pol2)]; if(intLin(pi1,pi2,pj1,pj2)){ //ps(pi1.x);ps(pi2.x);pln("przeciecie"); double a1 = (double)(pi2.y-pi1.y)/(pi2.x-pi1.x); double b1 = pi2.y-a1*pi2.x; double a2 = (double)(pj2.y-pj1.y)/(pj2.x-pj1.x); double b2 = pj2.y-a1*pj2.x; Point inte; inte.x = (b2-b1)/(a1-a2); inte.y = a1*inte.x+b1; res.pb(inte); } } } for(auto r : res){ // ps(r.x);pln(r.y); } return res; } double field(vp pol){ double res=0; pol = convexHull(pol); loop(i,1,SIZE(pol)){ res += abs(prodVec(pol[0],pol[i],pol[(i+1)%SIZE(pol)]))/2.0; } return res; } int main(){ ios_base::sync_with_stdio(0); cin>>n; loop(i,0,n){ cin>>pps[i].ff.x>>pps[i].ff.y>>pps[i].ss.x>>pps[i].ss.y; } vp ps,res; loop(mask, 0, 1<<n){ ps.clear(); loop(j,0,n){ if(1<<j&mask){ ps.pb(pps[j].ss); }else{ ps.pb(pps[j].ff); } } vp hulled = convexHull(ps); if(mask == 0) res = hulled; else res = intPol(res,hulled); } pln(field(res)); } |