//计算叉乘--线段旋转方向和对应的四边形的面积--返回(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;
}
在使用旋转卡壳法之前先计算点的凸包,红色为物体的边缘,黄色为找到的最远的点