#include<bits/stdc++.h> using namespace std; typedef long double lf; struct Point{ lf x,y; Point(void) {} Point(lf x, lf y): x(x), y(y) {} }; istream& operator >> (istream& s, Point& a){ s >> a.x >> a.y; return s; } ostream& operator << (ostream& s, const Point& a){ s << "(" << setw(6) << a.x << ", " << setw(6) << a.y << ")"; return s; } struct Line{ lf a,b,c; Line(void) {} Line(const Point& x, const Point& y){ a = y.x - x.x; b = x.y - y.y; c = x.x*y.y - x.y*y.x; } }; lf det(const Point& a, const Point& b, const Point& c){ return a.x*b.y + b.x*c.y + c.x*a.y - (a.y*b.x + b.y*c.x + c.y*a.x); } lf calcField(const vector<Point>& t){ lf res = 0; for(int i=0;i<t.size();i++) res += det(t[0], t[i], t[(i+1)%t.size()])/2.0; return res; } int getSide(const Point& a, const Point& b, const Point& c){ lf x = det(a,b,c); if(x < 0.0) return -1; return (x > 0.0 ? 1 : 0); } lf distance(const Point& a, const Point& b){ return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y)); } bool operator == (const Point& a, const Point& b){ return (a.x == b.x) && (a.y == b.y); } Point c; bool cmp(const Point& a, const Point& b){ if(a == c) return true; if(b == c) return false; int x = getSide(c,a,b); return (x == 1) || (x == 0 && distance(c,a) < distance(c,b)); } vector<Point> convexHull(vector<Point>& t){ c = t[0]; for(auto& i : t) if(i.y < c.y || (i.y == c.y && i.x < c.x)) c = i; sort(t.begin(), t.end(), cmp); vector<Point> s; s.push_back(t[0]), s.push_back(t[1]); int n = s.size(); for(int i=2;i<t.size();i++){ while(n >= 2 && getSide(s[n-2], s[n-1], t[i]) != 1) s.pop_back(), n--; s.push_back(t[i]); n++; } return s; } Point intersect(const Point& a, const Point& b, const Point& x, const Point& y){ Line p(a,b); Line q(x,y); lf k = p.b*q.c - p.c*q.b; lf l = p.c*q.a - p.a*q.c; lf m = p.a*q.b - p.b*q.a; // cout << "<<< " << l/m << " " << k/m << "\n"; return Point(l/m, k/m); } vector<Point> intersect(vector<Point> a, vector<Point> b){ vector<Point> res; /* for(auto& i : b) cout << "$$ " << i << "\n"; for(auto& i : a) cout << "## " << i << "\n"; cout << "______________\n"; */ for(int i=0;i<a.size();i++){ res.clear(); Point x = a[i]; Point y = a[(i+1)%a.size()]; // cout << x << " " << y << "\n"; for(int j=0;j<b.size();j++){ Point p = b[j]; Point q = b[(j+1)%b.size()]; int k = getSide(x,y,p); int l = getSide(x,y,q); // cout << "====> " << p << " " << q << "\n"; if(k >= 0 && l >= 0){ res.push_back(q); // cout << "A\n"; }else if(k > 0 && l <= 0){ res.push_back(intersect(x,y,p,q)); // cout << "B\n"; }else if(k < 0 && l >= 0){ res.push_back(intersect(x,y,p,q)); res.push_back(q); // cout << "C\n"; } } b = res; // for(auto& i : b) // cout << "---> " << i << "\n"; // cout << "-----------------\n"; } return res; } int main(void){ ios_base::sync_with_stdio(false); cout << fixed << setprecision(9); int n; cin >> n; vector<pair<Point, Point> > t(n); for(auto& i : t) cin >> i.first >> i.second; vector<Point> res; bool ok=false; vector<Point> p; vector<Point> h; for(int i=0;i<(1<<n);i++){ p.clear(); for(int j=0;j<n;j++) if(i&(1<<j)) p.push_back(t[j].first); else p.push_back(t[j].second); h = convexHull(p); if(!ok) res = p, ok = true; else res = intersect(res, p); /* for(auto& i : res) cout << "############## " << i << "\n"; cout << "-----------------\n"; */ } cout << (res.size() == 0 ? 0.0 : calcField(res)) << "\n"; return 0; }
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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | #include<bits/stdc++.h> using namespace std; typedef long double lf; struct Point{ lf x,y; Point(void) {} Point(lf x, lf y): x(x), y(y) {} }; istream& operator >> (istream& s, Point& a){ s >> a.x >> a.y; return s; } ostream& operator << (ostream& s, const Point& a){ s << "(" << setw(6) << a.x << ", " << setw(6) << a.y << ")"; return s; } struct Line{ lf a,b,c; Line(void) {} Line(const Point& x, const Point& y){ a = y.x - x.x; b = x.y - y.y; c = x.x*y.y - x.y*y.x; } }; lf det(const Point& a, const Point& b, const Point& c){ return a.x*b.y + b.x*c.y + c.x*a.y - (a.y*b.x + b.y*c.x + c.y*a.x); } lf calcField(const vector<Point>& t){ lf res = 0; for(int i=0;i<t.size();i++) res += det(t[0], t[i], t[(i+1)%t.size()])/2.0; return res; } int getSide(const Point& a, const Point& b, const Point& c){ lf x = det(a,b,c); if(x < 0.0) return -1; return (x > 0.0 ? 1 : 0); } lf distance(const Point& a, const Point& b){ return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y)); } bool operator == (const Point& a, const Point& b){ return (a.x == b.x) && (a.y == b.y); } Point c; bool cmp(const Point& a, const Point& b){ if(a == c) return true; if(b == c) return false; int x = getSide(c,a,b); return (x == 1) || (x == 0 && distance(c,a) < distance(c,b)); } vector<Point> convexHull(vector<Point>& t){ c = t[0]; for(auto& i : t) if(i.y < c.y || (i.y == c.y && i.x < c.x)) c = i; sort(t.begin(), t.end(), cmp); vector<Point> s; s.push_back(t[0]), s.push_back(t[1]); int n = s.size(); for(int i=2;i<t.size();i++){ while(n >= 2 && getSide(s[n-2], s[n-1], t[i]) != 1) s.pop_back(), n--; s.push_back(t[i]); n++; } return s; } Point intersect(const Point& a, const Point& b, const Point& x, const Point& y){ Line p(a,b); Line q(x,y); lf k = p.b*q.c - p.c*q.b; lf l = p.c*q.a - p.a*q.c; lf m = p.a*q.b - p.b*q.a; // cout << "<<< " << l/m << " " << k/m << "\n"; return Point(l/m, k/m); } vector<Point> intersect(vector<Point> a, vector<Point> b){ vector<Point> res; /* for(auto& i : b) cout << "$$ " << i << "\n"; for(auto& i : a) cout << "## " << i << "\n"; cout << "______________\n"; */ for(int i=0;i<a.size();i++){ res.clear(); Point x = a[i]; Point y = a[(i+1)%a.size()]; // cout << x << " " << y << "\n"; for(int j=0;j<b.size();j++){ Point p = b[j]; Point q = b[(j+1)%b.size()]; int k = getSide(x,y,p); int l = getSide(x,y,q); // cout << "====> " << p << " " << q << "\n"; if(k >= 0 && l >= 0){ res.push_back(q); // cout << "A\n"; }else if(k > 0 && l <= 0){ res.push_back(intersect(x,y,p,q)); // cout << "B\n"; }else if(k < 0 && l >= 0){ res.push_back(intersect(x,y,p,q)); res.push_back(q); // cout << "C\n"; } } b = res; // for(auto& i : b) // cout << "---> " << i << "\n"; // cout << "-----------------\n"; } return res; } int main(void){ ios_base::sync_with_stdio(false); cout << fixed << setprecision(9); int n; cin >> n; vector<pair<Point, Point> > t(n); for(auto& i : t) cin >> i.first >> i.second; vector<Point> res; bool ok=false; vector<Point> p; vector<Point> h; for(int i=0;i<(1<<n);i++){ p.clear(); for(int j=0;j<n;j++) if(i&(1<<j)) p.push_back(t[j].first); else p.push_back(t[j].second); h = convexHull(p); if(!ok) res = p, ok = true; else res = intersect(res, p); /* for(auto& i : res) cout << "############## " << i << "\n"; cout << "-----------------\n"; */ } cout << (res.size() == 0 ? 0.0 : calcField(res)) << "\n"; return 0; } |