void convex_hull(poly &v,poly &p) { p.clear() ; sort(v.begin(),v.end()) ; v.resize(unique(v.begin(),v.end())-v.begin()) ; int n=v.size() ; int sz=0 ; for(int i=0;i<n;i++) { while(sz>=2 && dcmp(cross(st[sz-2],st[sz-1],v[i]))<0) sz-- ; st[sz++]=v[i] ; } for(int i=0;i<sz;i++) p.push_back(st[i]) ; sz=0 ; for(int i=0;i<n;i++) { while(sz>=2 && dcmp(cross(st[sz-2],st[sz-1],v[i])>0)) sz-- ; st[sz++]=v[i] ; } for(int i=sz-1;i>=0;i--) { if(i==sz-1 && st[i]==p.back()) continue ; if(i==0 && st[i]==p[0]) continue ; p.push_back(st[i]) ; } }
poly graham(poly p){ int i,j,n = p.size(); poly g; pivo = *min_element(p.begin(), p.end(), cmp_pivo); sort( p.begin(), p.end(), cmp_radial); // n previous declared for( i=j=0; i<n;i++){ while( j>=2 && ccw( g[j-2] , g[j-1], p[i]) >=0){ g.pop_back(); j--; } g.push_back(p[i]); j++; } return g; }
poly graham (poly p) { for (int i = 1; i < p.size(); ++i) { if (p[i] < p[0]) swap(p[0],p[i]); } pivot = p[0]; sort(p.begin(),p.end(),angleCmp); poly chull (p.begin(),p.begin()+3); for (int i = 3; i < p.size(); ++i) { while(chull.size()>=2 && cross(chull[chull.size()-2],chull[chull.size()-1],p[i]) <= 0) { chull.erase(chull.end()-1); } chull.push_back(p[i]); } return chull; }
poly hull( poly p ) { int k = 0, n = p.size(); sort( p.begin(), p.end()); poly h( 2 * n ); for(int i = 0; i < n; i++){ while( k >= 2 && ccw( h[k-2], h[k-1], p[i] ) <= 0 ) k--; h[ k++ ] = p[ i ]; } int tmp = k + 1; for(int i = n - 2; i >= 0; i--){ while( k >= tmp && ccw( h[k-2], h[k-1], p[i] ) <= 0 ) k--; h[ k++ ] = p[ i ]; } h.resize( k ); return h; }
void convex_hull(poly & t, poly & r) { unsigned i; Compare order; // Search leftmost vertex order.p0 = t[0]; for (i = 1; i < t.size(); ++i) if (t[i].real() < order.p0.real()) order.p0 = t[i]; sort(t.begin(), t.end(), order); for (i = 0; i < t.size(); ++i) { r.push_back(t[i]); // Pop vertices that become internal while (r.size() > 3u && angle(r.end()[-3], r.end()[-2], r.end()[-1])) { r.end()[-2] = r.back(); r.pop_back(); } } return; }