案例代码:
bool JudgeDoubleIsZero(double f)
{
const double EPSINON = 0.000001F;
if ((f >= -EPSINON) && (f <= EPSINON))
return true;
return false;
}
bool Collinear_New(const cv::Point2f& a, const cv::Point2f& b, const cv::Point2f& c, float angThresold)
{
//两个向量 ba 和 bc
float b2a[2] = { a.x - b.x, a.y - b.y }, b2c[2] = { c.x - b.x, c.y - b.y };
float dotProduct = 0.0; //向量点乘
float a1 = 0.0;//向量模长
float b1 = 0.0;//向量模长
float cosr = 0.0;//向量夹角
dotProduct = b2a[0] * b2c[0] + b2a[1] * b2c[1];
a1 = sqrt(b2a[0] * b2a[0] + b2a[1] * b2a[1]);
b1 = sqrt(b2c[0] * b2c[0] + b2c[1] * b2c[1]);
if (JudgeDoubleIsZero(a1) || JudgeDoubleIsZero(b1))//边长为0,说明至少2点重合,直接判断为共线
{
return true;
}
cosr = dotProduct / a1 / b1;//余弦值
cosr = acos(cosr) * 180 / 3.1415926;//转换为角度 。acos范围是0~ pi
if (cosr < angThresold || cosr > 180 - angThresold)//共线
{
return true;
}
return false;
}