您的位置:首页 > 其它

最小矩形覆盖(凸包旋转卡壳)

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: