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
| #include <bits/stdc++.h> #define F(n) Fi(i,n) #define Fi(i,n) Fl(i,0,n) #define Fl(i,l,n) for(int i=l;i<n;i++) using namespace std; const int N = 51; const double EXP = 1e-9; typedef pair<double,double> PDD; PDD D[N], E[N], ST[N]; int st; struct V{ double x,y; V(){} V(double _x,double _y):x(_x),y(_y){} V(PDD a,PDD b):x(b.first - a.first), y(b.second - a.second){} V turn(){ return V(-x,- y); } }; double cross(V a, V b){ return a.x*b.y - a.y*b.x; } inline bool biger(double x){ return x>EXP; } inline bool equal(double x){ return -EXP<x&&x<EXP; } void my_unique(PDD *P, int &_n){ int pre = 0; F(_n-1){ if(P[pre]!=P[i+1]){ P[++pre] = P[i+1]; } } _n = pre+1; } void hull(PDD *P, int &_n){ sort(P,P+_n); my_unique(P, _n); if(_n<=2)return; st=0; F(_n){ while(st>=2 && !biger(cross(V(ST[st-2], ST[st-1]), V(ST[st-1], P[i]))) ){ st--; } ST[st++] = P[i]; } st--; int tag = st+2; F(_n){ while(st>=tag && !biger(cross(V(ST[st-2], ST[st-1]), V(ST[st-1], P[_n-i-1]))) ){ st--; } ST[st++] = P[_n-i-1]; } st--; F(st)P[i] = ST[i]; _n = st; } inline double countC(PDD p, V v){ return p.first*v.y + p.second*(-v.x); } double distance(PDD a, V v1, PDD b, V v2){ double dis = -countC(a,v1) - countC(b,v2); if(!biger(dis)){ return 0.0; }else{ return dis / sqrt(v1.x*v1.x + v1.y*v1.y); } } inline bool check_middle(V a, V b, V c){ return biger(cross(a,b)) && biger(cross(b,c)); } inline double distanceP(PDD a, PDD b){ return sqrt((a.first-b.first)*(a.first-b.first) + (a.second-b.second)*(a.second-b.second)); } void solve(PDD *P1, int _n, PDD *P2, int _m){ V v1 = V(0,-1), v2 = V(0,1); int p1 = 0,p2; F(_m)if(P2[(i+1)%_m]<P2[i]){ p2 = i; break; } double ans = 0; F(_n+_m){ double tmp = distance(P1[p1], v1, P2[p2], v2); if(biger(tmp)){ ans = max(tmp, ans); } V PtP = V(P1[p1].second-P2[p2].second, P2[p2].first-P1[p1].first), v1n = V(P1[p1],P1[(p1+1)%_n]), v2n = V(P2[p2],P2[(p2+1)%_m]); if(_m==1)v2n = v1n.turn(); if(check_middle(v1,PtP,v1n) && check_middle(v2,PtP.turn(),v2n)){ tmp = distanceP(P1[p1], P2[p2]); ans = max(tmp, ans); } if(equal(cross(v1n, v2n)) ){ v1 = v1n; v2 = v1.turn(); p1 = (p1+1)%_n; p2 = (p2+1)%_m; }else if(biger(cross(v1n, v2n)) ){ v2 = v2n; v1 = v2.turn(); p2 = (p2+1)%_m; }else{ v1 = v1n; v2 = v1.turn(); p1 = (p1+1)%_n; } } if(biger(ans))cout<<ans<<endl; else cout<<"IMPOSSIBLE"<<endl; } bool dot_in_middle(PDD a, PDD b, PDD c){ return a<b && b<c; } main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout<<fixed<<setprecision(3); int n,m; while(cin>>n>>m, n||m){ F(n)cin>>D[i].first>>D[i].second; F(m)cin>>E[i].first>>E[i].second; hull(D, n); hull(E, m); if(n==1 && m==1){ double ans = distanceP(D[0],E[0]); if(biger(ans))cout<<ans<<endl; else cout<<"IMPOSSIBLE"<<endl; continue; } if(m==1){ solve(D,n,E,m); }else{ solve(E,m,D,n); } } }
|