最小矩形覆盖(凸包旋转卡壳)
2016-10-19 15:43
477 查看
#include<bits/stdc++.h> using namespace std; const double esp = 1e-6; int sgn(double x){return x<-esp?-1:x>esp;} const int N = 5e4+10; int n; struct Point{ double x,y; void in(){scanf("%lf%lf",&x,&y);} void pri(){printf("x=%lf,y=%lf\n",x,y);} Point(double x,double y):x(x),y(y){} Point(){} Point operator -(Point o){return Point(x-o.x,y-o.y);} Point operator +(Point o){return Point(x+o.x,y+o.y);} Point operator *(double len){return Point(x*len,y*len);} double operator *(Point o){return x*o.y-y*o.x;} double operator /(Point o){return x*o.x+y*o.y;} double dis(){return sqrt(x*x+y*y);} bool operator < (Point o){ if(sgn(y-o.y)==0) return sgn(x-o.x)<0; return sgn(y-o.y)<0; } }p ,cv ,rec[4]; Point rt(Point o,double a){ return Point(o.x*cos(a)-o.y*sin(a),o.y*cos(a)+o.x*sin(a)); } bool cmp(Point a,Point b){ int re = (a-p[1])*(b-p[1]); if(re==0) return (a-p[1]).dis()<(b-p[1]).dis(); return re>0; } int graham(){ for(int i=2;i<=n;i++) if(p[i]<p[1]) swap(p[i],p[1]); sort(p+2,p+n+1,cmp); int top=2; cv[1]=p[1],cv[2]=p[2]; for(int i=3;i<=n;i++){ for(;top>1 && sgn((cv[top]-cv[top-1])*(p[i]-cv[top]))<=0;top--) ; cv[++top]=p[i]; } cv[top+1]=p[1]; return top; } double det(Point a,Point b,Point c){ return fabs((b-a)*(c-a)); } double dot(Point a,Point b,Point c){ return (b-a)/(c-a); } double ans; int tot; const double pi = acos(-1.0); void rc(){ ans=1e60+10; int t=1,l=1,r=1; for(int i=1;i<=tot;i++){ for(;sgn(det(cv[i],cv[i+1],cv[t])-det(cv[i],cv[i+1],cv[t+1]))<=0;t=t%tot+1) ; double D = (cv[i]-cv[i+1]).dis(); for(;sgn(dot(cv[i],cv[i+1],cv[r])-dot(cv[i],cv[i+1],cv[r+1]))<=0;r=r%tot+1) ; if(i==1) l=r; for(;sgn(dot(cv[i+1],cv[i],cv[l])-dot(cv[i+1],cv[i],cv[l+1]))<=0;l=l%tot+1) ; double R = dot(cv[i],cv[i+1],cv[r])/D; double L =dot(cv[i],cv[i+1],cv[l])/D; double W = fabs(R-L); double H = det(cv[i],cv[i+1],cv[t])/D; double tmp = W*H; if(sgn(tmp-ans)<=0){ ans=tmp; rec[0]=cv[i]+(cv[i+1]-cv[i])*(R/D); rec[1]=rec[0]+rt(cv[i+1]-cv[i],pi/2)*(H/D); rec[2]=rec[1]+(cv[i]-cv[i+1])*(W/D); rec[3]=rec[2]+(rec[0]-rec[1]); } } } int main(){ while(scanf("%d",&n)==1){ for(int i=1;i<=n;i++) p[i].in(); tot = graham(); rc(); printf("area=%lf\n",ans); int st = 0; for(int i=1;i<4;i++) if(rec[i]<rec[st]) st=i; for(int i=0;i<4;i++) printf("%.5lf %.5lf\n",rec[(st+i)%4].x,rec[(st+i)%4].y); } return 0; }
相关文章推荐
- UVA 10173 最小矩形覆盖(凸包+旋转卡壳)
- [BZOJ 1185][HNOI 2007]最小矩形覆盖(凸包+旋转卡壳)
- UVA 10173 最小矩形覆盖(凸包+旋转卡壳)
- UVA 10173 最小矩形覆盖(凸包+旋转卡壳)
- UVA 10173 最小矩形覆盖(凸包+旋转卡壳)
- UVA 10173 最小矩形覆盖(凸包+旋转卡壳)
- HDU 5251 矩形面积(二维凸包旋转卡壳最小矩形覆盖问题) --2015百度之星题目
- 【最小矩形面积覆盖:凸包+旋转卡壳】UVA 10173 Smallest Bounding Rectangle
- UVA 10173 最小矩形覆盖(凸包+旋转卡壳)
- HDU 5251 矩形面积(二维凸包旋转卡壳最小矩形覆盖问题) --2015百度之星题目
- UVA 10173 最小矩形覆盖(凸包+旋转卡壳)
- HDU5251矩形面积(凸包+旋转卡壳求最小矩形覆盖)
- UVA 10173 最小矩形覆盖(凸包+旋转卡壳)
- 【BZOJ1185】最小矩形覆盖 计算几何 凸包 旋转卡壳
- UVA 10173 最小矩形覆盖(凸包+旋转卡壳)
- bzoj 1185 [HNOI2007]最小矩形覆盖 凸包+旋转卡壳
- UVA 10173 最小矩形覆盖(凸包+旋转卡壳)
- [BZOJ1185][HNOI2007]最小矩形覆盖(凸包+旋转卡壳)
- UVA 10173 最小矩形覆盖(凸包+旋转卡壳)
- UVA 10173 最小矩形覆盖(凸包+旋转卡壳)