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

#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[N],cv[N],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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值