- 计算两条直线的方向向量,以及直线上的两点
double deltaX_X1 = endPoint.x - orgin.x;
double deltaY_X1 = endPoint.y -orgin.y;
double deltaX_X2 = endPoint.x - orgin.x;
double deltaY_X2 = endPoint.y - orgin.y;
double lineLengthX1 = sqrt(deltaX_X1*deltaX_X1+deltaY_X1*deltaY_X1);
double lineLengthX2 = sqrt(deltaX_X2*deltaX_X2+deltaY_X2*deltaY_X2);
X1DirectionVector.push_back(deltaX_X1/lineLengthX1);
X1DirectionVector.push_back(deltaY_X1/lineLengthX1);
X2DirectionVector.push_back(deltaX_X2/lineLengthX2);
X2DirectionVector.push_back(deltaY_X2/lineLengthX2);
- 计算交点
cv::Point intersectionPointCalculate(std::vector<double> X1DirectionVec,cv::Point point1, std::vector<double> X2DirectionVec,cv::Point point2)
{
cv::Point crossPoint(9999,9999);
if((X1DirectionVec.at(0)!=0) && X2DirectionVec.at(0)!=0)
{
double ka = X1DirectionVec.at(1)/X1DirectionVec.at(0);
double kb = X2DirectionVec.at(1)/X2DirectionVec.at(0);
double x = ((-1)*kb*double(point2.x)+double(point2.y)+ka*double(point1.x)-double(point1.y)) / (ka - kb);
crossPoint.x = int(x);
crossPoint.y = ka*(x-double(point1.x))+double(point1.y);
}
if((X1DirectionVec.at(0)==0) && X2DirectionVec.at(0)==0)
{
}
if((X1DirectionVec.at(0)==0) && X2DirectionVec.at(0)!=0)
{
double kb = X2DirectionVec.at(1)/X2DirectionVec.at(0);
crossPoint.x = point1.x;
crossPoint.y = kb*(crossPoint.x-point2.x)+point2.y;
}
if((X1DirectionVec.at(0)!=0) && X2DirectionVec.at(0)==0)
{
double ka = X1DirectionVec.at(1)/X1DirectionVec.at(0);
crossPoint.x = point2.x;
crossPoint.y = ka*(crossPoint.x-point1.x)+point1.y;
}
return crossPoint;
}
|