点的凸包

struct point{
    double x,y;
    point(){}
    point(double x,double y):x(x),y(y){}
    point operator + (point &p){
        return point(x+p.x,y+p.y);
    }
    point operator - (point &p){
        return point(x-p.x,y-p.y);
    }
    point operator * (double k){
        return point(x*k,y*k);
    }
    point operator / (double k){
        return point(x/k,y/k);
    }
};

typedef vector<point>polygon;

int cmp(point a,point b)
{
    if(a.x!=b.x){
        return a.x<b.x;
    }else{
        return a.y<b.y;
    }
}

double cross(point a,point b)
{
    return a.x*b.y-a.y*b.x;
}

#define eps (1e-10)
#define CLOCKWISE 1
#define COUNTER_CLOCKWISE -1
int ccw(point a,point b,point c)
{
    point base=b-a;
    point linshi=c-a;
    if(cross(base,linshi)<eps){
        return CLOCKWISE;
    }else{
        return COUNTER_CLOCKWISE;
    }
}

polygon andrewscanf(polygon s)
{
    int n=s.size();
    if(n<3){
        return s;
    }
    sort(s.begin(),s.end(),cmp);
    polygon u,l;
    u.push_back(s[0]);
    u.push_back(s[1]);

    l.push_back(s[n-1]);
    l.push_back(s[n-2]);
    for(int i=2;i<n;i++){
        for(int j=u.size();j>=2&&(ccw(u[j-2],u[j-1],s[i])!=CLOCKWISE);j--){
            u.pop_back();
        }
        u.push_back(s[i]);
    }
    for(int i=n-3;i>=0;i--){
        for(int j=l.size();j>=2&&(ccw(l[j-2],l[j-1],s[i])!=CLOCKWISE);j--){
           // cout<<s[i].x<<" "<<s[i].y<<" "<<l[j-2].x<<" "<<l[j-2].y<<" "<<l[j-1].x<<" "<<l[j-1].y<<endl;
            l.pop_back();
        }
        l.push_back(s[i]);
    }
    reverse(l.begin(),l.end());
    for(int i=u.size()-2;i>=1;i--){
        l.push_back(u[i]);
    }
    return l;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值