UVA 10173 Smallest Bounding Rectangle

这题写得略沧桑,发现计算几何的细节实在是太多了,然后对于代码格式的强迫症写起来真心蛋疼。

旋转卡壳,先前自己YY的也是差不多这回事,但是算面积的地方是算垂足然后算记录这样丢精度了亲……

#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>

#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <string>
#include <bitset>
#include <memory>
#include <complex>
#include <numeric>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <ctype.h>
#include <locale.h>

using namespace std;

#pragma pack(4)

const double  eps = 1e-8;
const double   pi = acos(-1.0);
const int     inf = 0x7f7f7f7f;

#define loop(a,n)                            \
    for(int i=0;n>i;i++)                     \
        cout<<a[i]<<(i!=n-1?' ':'\n')
#define loop2(a,n,m)                         \
    for(int i=0;n>i;i++)                     \
        for(int j=0;m>j;j++)                 \
            cout<<a[i][j]<<(j!=m-1?' ':'\n')

#define   at(a,i) ((a)&(1<<(i)))
#define   nt(a,i) ((a)^(1<<(i)))
#define set1(a,i) ((a)|(1<<(i)))
#define set0(a,i) ((a)&(~(1<<(i))))

#define cmp(a,b) (fabs((a)-(b))<eps?0:(((a)-(b))>eps?+1:-1))

#define lmax(a,b) ((a)>(b)?(a):(b))
#define lmin(a,b) ((a)<(b)?(a):(b))
#define fmax(a,b) (cmp(a,b)>0?(a):(b))
#define fmin(a,b) (cmp(a,b)<0?(a):(b))

const int MAXV = 1001;

typedef struct Point
{
    double o[2];
    double & operator [] (int i)
    {
        return o[i];
    }
    friend ostream & operator << (ostream & cout,Point argu)
    {
        return cout<<"("<<argu[0]<<","<<argu[1]<<")";
    }
    Point(double x=0,double y=0)
    {
        o[0]=x;
        o[1]=y;
    }
    Point operator + (Point argu)
    {
        return Point(o[0]+argu[0],o[1]+argu[1]);
    }
    Point operator - (Point argu)
    {
        return Point(o[0]-argu[0],o[1]-argu[1]);
    }
    Point operator + (double argu)
    {
        return (*this)*(1+argu/length());
    }
    Point operator - (double argu)
    {
        return (*this)*(1-argu/length());
    }
    Point operator * (double argu)
    {
        return Point(o[0]*argu,o[1]*argu);
    }
    Point operator / (double argu)
    {
        return Point(o[0]/argu,o[1]/argu);
    }
    bool operator < (Point argu) const
    {
        if(cmp(o[0],argu[0])!=0) return cmp(o[0],argu[0])<0;
        else return cmp(o[1],argu[1])<0;
    }
    bool operator == (Point argu) const
    {
        return cmp(o[0],argu[0])==0&&cmp(o[1],argu[1])==0;
    }
    double length(void)
    {
        return sqrt(o[0]*o[0]+o[1]*o[1]);
    }
    double angle(void) const
    {
        return fmod(atan2(o[1],o[0])+2*pi,2*pi);
    }
}Vector;

double dot(Vector a,Vector b)
{
    return a[0]*b[0]+a[1]*b[1];
}
double dot(Point o,Point a,Point b)
{
    return dot(a-o,b-o);
}
int ward(Point p,Point s,Point e)
{
    return cmp(dot(s,e,p),0);
}
double cross(Vector a,Vector b)
{
    return a[0]*b[1]-a[1]*b[0];
}
double cross(Point o,Point a,Point b)
{
    return cross(a-o,b-o);
}
int side(Point p,Point s,Point e)
{
    return cmp(cross(s,e,p),0);
}

double area(Point poly[],int n)
{
    double ans=0;
    for(int i=1;n>i+1;i++)
    {
        ans+=cross(poly[0],poly[i],poly[i+1]);
    }
    return abs(ans)/2;
}

int boundingBox(double x,double a,double b)
{
    return cmp(a,x)*cmp(x,b);
}
int boundingBox(Point p,Point s,Point e)
{
    if(boundingBox(p[0],s[0],e[0])>0&&boundingBox(p[1],s[1],e[1])>0) return 1;
    else if(boundingBox(p[0],s[0],e[0])>=0&&boundingBox(p[1],s[1],e[1])>=0) return 0;
    else return -1;
}

int locate(Point p,Point s,Point e)
{
    if(p==s||p==e) return 0;
    return side(p,s,e)==0&&boundingBox(p,s,e)>=0?1:-1;
}
int locate(Point p,Point poly[],int n)
{
    int cnt[3]={0};
    poly[n]=poly[0];
    for(int i=0;n>i;i++)
    {
        cnt[side(p,poly[i],poly[i+1])+1]=true;
        if(cnt[1]) return 0;
        if(cnt[2]&&cnt[0]) return -1;
    }
    return 1;
}

int intersection(Point s1,Point e1,Point s2,Point e2)
{
    if(side(s1,s2,e2)*side(e1,s2,e2)<0&&side(s2,s1,e1)*side(e2,s1,e1)<0) return 1;
    else
    {
        if(locate(s1,s2,e2)>=0) return 0;
        if(locate(e1,s2,e2)>=0) return 0;
        if(locate(s2,s1,e1)>=0) return 0;
        if(locate(e2,s1,e1)>=0) return 0;
        return -1;
    }
}

struct Line
{
    Point s,e;
    double o[3],angle;
    double & operator [] (int i)
    {
        return o[i];
    }
    friend ostream & operator << (ostream & cout,Line argu)
    {
        return cout<<"["<<argu.s<<"->"<<argu.e<<"]";
        return cout<<"["<<argu[0]<<","<<argu[1]<<" "<<argu[2]<<"]";
    }
    Line(Point _s=0,Point _e=0)
    {
        s=_s;
        e=_e;
        o[0]=s[1]-e[1];
        o[1]=e[0]-s[0];
        o[2]=cross(s,e);
        angle=(e-s).angle();
    }
    bool operator < (Line argu) const
    {
        if(cmp(angle,argu.angle)!=0) return cmp(angle,argu.angle)<0;
        else return side(argu.s,s,e)<0;
    }
    bool operator == (Line argu) const
    {
        return cmp(angle,argu.angle)==0;
    }
    Vector dir(void) const
    {
        return Vector(o[1],-o[0]);
    }
    Vector pen(void) const
    {
        return Vector(o[0],+o[1]);
    }
};

int ward(Point p,Line l)
{
    return ward(p,l.s,l.e);
}
int side(Point p,Line l)
{
    return side(p,l.s,l.e);
}

#ifndef DIS
#define DIS

double dis(Point a,Point b)
{
    return (b-a).length();
}
double dis(Point a,Point b,Line l)
{
    return fabs(dot(l.s,l.e,b)-dot(l.s,l.e,a))/dis(l.s,l.e);
}
double dis(Point p,Line l)
{
    return fabs(cross(l.s,l.e,p))/dis(l.s,l.e);
}
double dis(Point p,Point s,Point e)
{
    if(ward(p,s,e)*ward(p,e,s)>0) return dis(p,Line(s,e));
    else return fmin(dis(p,s),dis(p,e));
}
double dis(Line a,Line b)
{
    if(cmp(a.angle,b.angle)!=0) return 0;
    else return dis(a.s,b);
}
double dis(Line l,Point s,Point e)
{
    if(side(s,l)*side(e,l)<0) return 0;
    else return fmin(dis(s,l),dis(e,l));
}
double dis(Point s1,Point e1,Point s2,Point e2)
{
    if(intersection(s1,e1,s2,e2)>=0) return 0;
    else return fmin(fmin(dis(s1,s2,e2),dis(e1,s2,e2)),fmin(dis(s2,s1,e1),dis(e2,s1,e1)));
}

#endif // DIS

Point convex[MAXV];

int convexHull(Point py[],int n)
{
    int cnt=0,ans=0;

    sort(py,py+n);
    for(int i=0;n>i;i++)
    {
        while(cnt>=ans+2&&side(py[i],convex[cnt-2],convex[cnt-1])<=0) cnt--;
        convex[cnt++]=py[i];
    }
    ans=cnt-1;
    for(int i=n-2;i>=0;i--)
    {
        while(cnt>=ans+2&&side(py[i],convex[cnt-2],convex[cnt-1])<=0) cnt--;
        convex[cnt++]=py[i];
    }
    return cnt-1;
}

double rotateCliper(Point py[],int n)
{
    if(n<3) return 0;

    int f=1,l=1,r=1;
    double ans=1e100;

    py[n]=py[0];
    for(int i=0;n>i;i++)
    {
        while(cmp(cross(py[i],py[i+1],py[f+1]),cross(py[i],py[i+1],py[f]))>=0) f=(f+1)%n;
        while(cmp(dot(py[i],py[i+1],py[r+1]),dot(py[i],py[i+1],py[r]))>=0) r=(r+1)%n;
        if(!i) l=r;
        while(cmp(dot(py[i],py[i+1],py[l+1]),dot(py[i],py[i+1],py[l]))<=0) l=(l+1)%n;
        ans=fmin(ans,dis(py[f],Line(py[i],py[i+1]))*dis(py[l],py[r],Line(py[i],py[i+1])));
    }
    return ans;
}

int n;
double x,y;
Point a[MAXV];

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("Smallest Bounding Rectangle.txt","r",stdin);
    #else
    #endif

    while(scanf("%d",&n),n)
    {
        for(int i=0;n>i;i++)
        {
            scanf("%lf %lf",&x,&y);
            a[i]=Point(x,y);
        }
        printf("%.4f\n",rotateCliper(convex,convexHull(a,n)));
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值