#include<bits/stdc++.h> using namespace std; #ifndef d #define d(...) #endif #define st first #define nd second #define pb push_back #define siz(c) (int)(c).size() #define all(c) (c).begin(), (c).end() typedef long long LL; typedef long double LD; constexpr int INF=1e9+7; constexpr LL INFL=1e18; template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) { return os << "(" << P.st << "," << P.nd << ")"; } constexpr LD eps=1e-11; struct point { LD x,y; point(LD x=0,LD y=0):x(x),y(y){} bool isZero() const { return max(abs(y),abs(x))<eps; } bool inUpper() const { return (y==0?x:y)>0; } LD operator ^ (const point& b) const { return x*b.y-y*b.x; } LD operator * (const point& b) const { return x*b.x + y*b.y; } point operator * (const LD c) const { return point(x*c,y*c); } point operator + (const point& b) const { return point(x+b.x,y+b.y); } point operator - () const { return point(-x,-y); } point operator -(const point& b) const { return (*this)+(-b); } bool operator < (const point& b) const { if(isZero()) return !b.isZero(); if(inUpper()!=b.inUpper()) return inUpper(); return ((*this)^b)>0; } bool operator == (const point& b) const { return ((*this)+(-b)).isZero(); } LD val() const { return (*this)*(*this); } friend istream& operator >> (istream& in, point& P) { in >> P.x >> P.y; return in; } friend ostream& operator << (ostream& out, const point P) { out << "(" << P.x << ", "<< P.y << ")"; return out; } }; constexpr int maxn=102; int n; point mag[maxn*2]; struct line { point p[2]; line(const point x, const point y) { p[0]=x,p[1]=y; } point df() const { return p[1]+(-p[0]); } point& operator[](int a) { return p[a]; } bool on(const point x) const { return (x^(p[1]+(-p[0])))<eps; } point proj(const point& x) { auto diff=df(); return p[0]+diff*((diff*(x+(-p[0])))/(diff*diff)); } friend bool parallel(const line& a,const line& b) { return abs(a.df()^b.df())<eps; } friend point intersect(line& a, line& b) { assert(not parallel(a,b)); point vec_a=a.df(), vec_b1=b[1]+(-a[0]), vec_b0=b[0]+(-a[0]); LD tr_area = vec_b1^vec_b0; LD quad_area = (vec_b1^vec_a) + (vec_a^vec_b0); return a[0]+vec_a*(tr_area/quad_area); } }; vector<line>ogr; LD cross(const point a,const point b) { return a^b; } LD dblcmp(const LD x) { if(abs(x)<eps) return 0; return x; } point isLL(point a, point b, point c, point d) { point p1 = b - a, p2 = d - c; long double a1 = p1.y, b1 = -p1.x, c1; long double a2 = p2.y, b2 = -p2.x, c2; if (dblcmp(a1 * b2 - a2 * b1) == 0) return point(0,0); else { c1 = a1 * a.x + b1 * a.y; c2 = a2 * c.x + b2 * c.y; return point((c1 * b2 - c2 * b1) / (a1 * b2 - a2 * b1), (c1 * a2 - c2 * a1) / (b1 * a2 - b2 * a1)); } } struct hp_t { point p1, p2; long double a; hp_t() {} hp_t(point tp1, point tp2) : p1(tp1), p2(tp2) { tp2 = tp2 - tp1; a = atan2(tp2.y, tp2.x); } bool operator==(const hp_t &r) const { return dblcmp(a - r.a) == 0; } bool operator<(const hp_t &r) const { if (dblcmp(a - r.a) == 0) return dblcmp(cross(r.p2 - r.p1, p2 - r.p1)) >= 0; else return a < r.a; } } hp[maxn*maxn*4]; int cnt; constexpr int maxd=400000; void addhp(point p1, point p2) { hp[++cnt] = hp_t(p1, p2); } void init() { cnt = 0; addhp(point(-maxd, -maxd), point(maxd, -maxd)); addhp(point(maxd, -maxd), point(maxd, maxd)); addhp(point(maxd, maxd), point(-maxd, maxd)); addhp(point(-maxd, maxd), point(-maxd, -maxd)); } bool checkhp(hp_t h1, hp_t h2, hp_t h3) { point p = isLL(h1.p1, h1.p2, h2.p1, h2.p2); return dblcmp(cross(p - h3.p1, h3.p2 - h3.p1)) > 0; } vector<point> hp_inter() { sort(hp + 1, hp + 1 + cnt); cnt = unique(hp + 1, hp + 1 + cnt) - hp - 1; deque<hp_t> DQ; DQ.push_back(hp[1]); DQ.push_back(hp[2]); for (int i = 3; i <= cnt; ++i) { while (DQ.size() > 1 && checkhp(*----DQ.end(), *--DQ.end(), hp[i])) DQ.pop_back(); while (DQ.size() > 1 && checkhp(*++DQ.begin(), *DQ.begin(), hp[i])) DQ.pop_front(); DQ.push_back(hp[i]); } while (DQ.size() > 1 && checkhp(*----DQ.end(), *--DQ.end(), DQ.front())) DQ.pop_back(); while (DQ.size() > 1 && checkhp(*++DQ.begin(), *DQ.begin(), DQ.back())) DQ.pop_front(); DQ.push_front(DQ.back()); vector<point> res; while (DQ.size() > 1) { hp_t tmp = DQ.front(); DQ.pop_front(); res.push_back(isLL(tmp.p1, tmp.p2, DQ.front().p1, DQ.front().p2)); } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); //cout<<(dblcmp(0.000000001)==0)<<endl; cin>>n; for(int i=0;i<n;i++) { cin>>mag[2*i]>>mag[2*i+1]; } for(int i=0;i<2*n;i++) for(int j=i+1;j<2*n;j++) { auto p=mag[i], q=mag[j]; line l(point(-p.y,p.x),point(-q.y,q.x)); //cerr<<"rzuty na linie: "<<l[0]<<" --> "<<l[1]<<endl; LD mn=INF,mx=-INF; for(int m=0;m<n;m++) { auto a=l.proj(mag[2*m]), b=l.proj(mag[2*m+1]); //cerr<<"rzuty maga: "<<m<<": "<<a<<" "<<b<<endl; LD ax,bx; if(abs((l.df()).y) < eps) { ax=(a.x-l[0].x)/(l[1].x-l[0].x); bx=(a.x-l[0].x)/(l[1].x-l[0].x); } else { ax=(a.y-l[0].y)/(l[1].y-l[0].y); bx=(b.y-l[0].y)/(l[1].y-l[0].y); } //cerr<<"ax,bx: "<<ax<<" "<<bx<<endl; mx=max(mx,min(ax,bx)); mn=min(mn,max(ax,bx)); } //cerr<<"mn,mx: "<<mn<<" "<<mx<<endl; ogr.emplace_back(l[0]+l.df()*mn,l[0]+(l.df()*mn)+q-p); //cerr<<"WPROWADZONO WEKTOREK: "<<ogr.back()[0]<<" --> "<<ogr.back()[1]<<endl; ogr.emplace_back(l[0]+l.df()*mx,l[0]+(l.df()*mx)+p-q); //cerr<<"WPROWADZONO WEKTOREK: "<<ogr.back()[0]<<" --> "<<ogr.back()[1]<<endl; } cout<<fixed<<setprecision(20); init(); for(auto& l:ogr) { addhp(l[0],l[1]); } auto hull=hp_inter(); if(siz(hull)<3) { cout<<0.0<<"\n"; return 0; } LD res=0; for(int i=1;i<=siz(hull);i++) res+=hull[i-1]^hull[i%siz(hull)]; cout<<fixed<<setprecision(20)<<abs(res)/2<<"\n"; }
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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | #include<bits/stdc++.h> using namespace std; #ifndef d #define d(...) #endif #define st first #define nd second #define pb push_back #define siz(c) (int)(c).size() #define all(c) (c).begin(), (c).end() typedef long long LL; typedef long double LD; constexpr int INF=1e9+7; constexpr LL INFL=1e18; template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) { return os << "(" << P.st << "," << P.nd << ")"; } constexpr LD eps=1e-11; struct point { LD x,y; point(LD x=0,LD y=0):x(x),y(y){} bool isZero() const { return max(abs(y),abs(x))<eps; } bool inUpper() const { return (y==0?x:y)>0; } LD operator ^ (const point& b) const { return x*b.y-y*b.x; } LD operator * (const point& b) const { return x*b.x + y*b.y; } point operator * (const LD c) const { return point(x*c,y*c); } point operator + (const point& b) const { return point(x+b.x,y+b.y); } point operator - () const { return point(-x,-y); } point operator -(const point& b) const { return (*this)+(-b); } bool operator < (const point& b) const { if(isZero()) return !b.isZero(); if(inUpper()!=b.inUpper()) return inUpper(); return ((*this)^b)>0; } bool operator == (const point& b) const { return ((*this)+(-b)).isZero(); } LD val() const { return (*this)*(*this); } friend istream& operator >> (istream& in, point& P) { in >> P.x >> P.y; return in; } friend ostream& operator << (ostream& out, const point P) { out << "(" << P.x << ", "<< P.y << ")"; return out; } }; constexpr int maxn=102; int n; point mag[maxn*2]; struct line { point p[2]; line(const point x, const point y) { p[0]=x,p[1]=y; } point df() const { return p[1]+(-p[0]); } point& operator[](int a) { return p[a]; } bool on(const point x) const { return (x^(p[1]+(-p[0])))<eps; } point proj(const point& x) { auto diff=df(); return p[0]+diff*((diff*(x+(-p[0])))/(diff*diff)); } friend bool parallel(const line& a,const line& b) { return abs(a.df()^b.df())<eps; } friend point intersect(line& a, line& b) { assert(not parallel(a,b)); point vec_a=a.df(), vec_b1=b[1]+(-a[0]), vec_b0=b[0]+(-a[0]); LD tr_area = vec_b1^vec_b0; LD quad_area = (vec_b1^vec_a) + (vec_a^vec_b0); return a[0]+vec_a*(tr_area/quad_area); } }; vector<line>ogr; LD cross(const point a,const point b) { return a^b; } LD dblcmp(const LD x) { if(abs(x)<eps) return 0; return x; } point isLL(point a, point b, point c, point d) { point p1 = b - a, p2 = d - c; long double a1 = p1.y, b1 = -p1.x, c1; long double a2 = p2.y, b2 = -p2.x, c2; if (dblcmp(a1 * b2 - a2 * b1) == 0) return point(0,0); else { c1 = a1 * a.x + b1 * a.y; c2 = a2 * c.x + b2 * c.y; return point((c1 * b2 - c2 * b1) / (a1 * b2 - a2 * b1), (c1 * a2 - c2 * a1) / (b1 * a2 - b2 * a1)); } } struct hp_t { point p1, p2; long double a; hp_t() {} hp_t(point tp1, point tp2) : p1(tp1), p2(tp2) { tp2 = tp2 - tp1; a = atan2(tp2.y, tp2.x); } bool operator==(const hp_t &r) const { return dblcmp(a - r.a) == 0; } bool operator<(const hp_t &r) const { if (dblcmp(a - r.a) == 0) return dblcmp(cross(r.p2 - r.p1, p2 - r.p1)) >= 0; else return a < r.a; } } hp[maxn*maxn*4]; int cnt; constexpr int maxd=400000; void addhp(point p1, point p2) { hp[++cnt] = hp_t(p1, p2); } void init() { cnt = 0; addhp(point(-maxd, -maxd), point(maxd, -maxd)); addhp(point(maxd, -maxd), point(maxd, maxd)); addhp(point(maxd, maxd), point(-maxd, maxd)); addhp(point(-maxd, maxd), point(-maxd, -maxd)); } bool checkhp(hp_t h1, hp_t h2, hp_t h3) { point p = isLL(h1.p1, h1.p2, h2.p1, h2.p2); return dblcmp(cross(p - h3.p1, h3.p2 - h3.p1)) > 0; } vector<point> hp_inter() { sort(hp + 1, hp + 1 + cnt); cnt = unique(hp + 1, hp + 1 + cnt) - hp - 1; deque<hp_t> DQ; DQ.push_back(hp[1]); DQ.push_back(hp[2]); for (int i = 3; i <= cnt; ++i) { while (DQ.size() > 1 && checkhp(*----DQ.end(), *--DQ.end(), hp[i])) DQ.pop_back(); while (DQ.size() > 1 && checkhp(*++DQ.begin(), *DQ.begin(), hp[i])) DQ.pop_front(); DQ.push_back(hp[i]); } while (DQ.size() > 1 && checkhp(*----DQ.end(), *--DQ.end(), DQ.front())) DQ.pop_back(); while (DQ.size() > 1 && checkhp(*++DQ.begin(), *DQ.begin(), DQ.back())) DQ.pop_front(); DQ.push_front(DQ.back()); vector<point> res; while (DQ.size() > 1) { hp_t tmp = DQ.front(); DQ.pop_front(); res.push_back(isLL(tmp.p1, tmp.p2, DQ.front().p1, DQ.front().p2)); } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); //cout<<(dblcmp(0.000000001)==0)<<endl; cin>>n; for(int i=0;i<n;i++) { cin>>mag[2*i]>>mag[2*i+1]; } for(int i=0;i<2*n;i++) for(int j=i+1;j<2*n;j++) { auto p=mag[i], q=mag[j]; line l(point(-p.y,p.x),point(-q.y,q.x)); //cerr<<"rzuty na linie: "<<l[0]<<" --> "<<l[1]<<endl; LD mn=INF,mx=-INF; for(int m=0;m<n;m++) { auto a=l.proj(mag[2*m]), b=l.proj(mag[2*m+1]); //cerr<<"rzuty maga: "<<m<<": "<<a<<" "<<b<<endl; LD ax,bx; if(abs((l.df()).y) < eps) { ax=(a.x-l[0].x)/(l[1].x-l[0].x); bx=(a.x-l[0].x)/(l[1].x-l[0].x); } else { ax=(a.y-l[0].y)/(l[1].y-l[0].y); bx=(b.y-l[0].y)/(l[1].y-l[0].y); } //cerr<<"ax,bx: "<<ax<<" "<<bx<<endl; mx=max(mx,min(ax,bx)); mn=min(mn,max(ax,bx)); } //cerr<<"mn,mx: "<<mn<<" "<<mx<<endl; ogr.emplace_back(l[0]+l.df()*mn,l[0]+(l.df()*mn)+q-p); //cerr<<"WPROWADZONO WEKTOREK: "<<ogr.back()[0]<<" --> "<<ogr.back()[1]<<endl; ogr.emplace_back(l[0]+l.df()*mx,l[0]+(l.df()*mx)+p-q); //cerr<<"WPROWADZONO WEKTOREK: "<<ogr.back()[0]<<" --> "<<ogr.back()[1]<<endl; } cout<<fixed<<setprecision(20); init(); for(auto& l:ogr) { addhp(l[0],l[1]); } auto hull=hp_inter(); if(siz(hull)<3) { cout<<0.0<<"\n"; return 0; } LD res=0; for(int i=1;i<=siz(hull);i++) res+=hull[i-1]^hull[i%siz(hull)]; cout<<fixed<<setprecision(20)<<abs(res)/2<<"\n"; } |