旋转卡壳法找最近的点

//计算叉乘--线段旋转方向和对应的四边形的面积--返回(p1-p0)*(p2-p0)叉积
inline int xmult(const cv::Point &p1 , const cv::Point &p2 , const cv::Point &p0)
{
    return (p1.x-p0.x)*(p2.y-p0.y) - (p1.y-p0.y)*(p2.x-p0.x);
}
//计算距离平方
inline double dis(const cv::Point &a , const cv::Point &b)
{
    return (a.x - b.x)*(a.x - b.x)+(a.y - b.y)*(a.y - b.y);
}
 //旋转卡壳法,输出两个点的位置和最远距离
int rotating_calipers(std::vector<cv::Point>&stack, int&m ,int& n) 
{
    double ans = 0;
    int top = stack.size();
    for(int i = 0,j = 1; i < top ; i++, j = (i+1)%(top)){
        int q = j;
        while(xmult( stack[j] , stack[(q+1)%(top)] , stack[i] ) > xmult( stack[j] , stack[q] , stack[i]))
            q = (q+1)%(top);
        double l1 = dis(stack[i] , stack[q]);
        double l2 = dis(stack[j] , stack[q]);
        double ans_ = std::max(l1 , l2);
        if(ans_ > ans){
            ans = ans_;
            m = q;
            n = l1 > l2 ? i : j;
        }
    }
    return ans;
}

在使用旋转卡壳法之前先计算点的凸包,红色为物体的边缘,黄色为找到的最远的点 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值