【计几】二维凸包

andrew算法求凸包(时间复杂度: O ( n l o g n ) O(n logn) O(nlogn)

核心代码:

struct point{
    double x,y;
    bool operator < (point a){
        if(dcmp(x, a.x)) return dcmp(x, a.x) < 0;
        return dcmp(y, a.y) < 0;
    }
    bool operator == (point a) { return !dcmp(x, a.x) && !dcmp(y, a.y); }
};


point P[N];
int sta[N];
int andrew(int n)
{
    sort(P, P + n);
    n = unique(P, P + n) - P;
    int m = 0;
    for(int i=0; i<n; i++)      //先求下凸包
    {
        while(m > 1 && sign(area(P[sta[m - 2]], P[sta[m - 1]], P[i])) <= 0) m--;
        sta[m++] = i;
    }
    int v = m;
    for(int i=n - 2; i>=0; i--) //再求上凸包
    {
        while(m > v && sign(area(P[sta[m - 2]], P[sta[m - 1]], P[i])) <= 0) m--;
        sta[m++] = i;
    }
	if(n > 1) m--;              // 删去末尾多余的起始点,最后凸包是闭合的
    return m;
}
  • PS:凸包(convex hall)和凸多边形(convex polygon)是不一样的,凸包有可能退化为线段,后者是前者的子集。



【计几】二维凸包题集

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值