## c++求两条直线的交点

``````cv::Point2d get2lineIPoint(cv::Point2d lineOnePt1, cv::Point2d lineOnePt2, cv::Point2d lineTwoPt1, cv::Point2d lineTwoPt2)
{

double x;
double y;

cv::Point2d result(-1,-1);
double k = (lineOnePt1.y - lineOnePt2.y) / (lineOnePt1.x- lineOnePt2.x);
double b = lineOnePt1.y - k * lineOnePt1.x;

double k1 = (lineTwoPt1.y - lineTwoPt2.y) / (lineTwoPt1.x - lineTwoPt2.x);
double b1 = lineTwoPt1.y - k1 * lineTwoPt1.x;

x = (b1 - b) / (k - k1);
y = k * x + b;
result.x = x;
result.y = y;

return result;
}
``````

``````cv::Point2d get2lineIPoint(cv::Vec4f lineParam1, cv::Vec4f lineParam2)
{

//Vec4f :参数的前半部分给出的是直线的方向，而后半部分给出的是直线上的一点
cv::Point2d result(-1, -1);

double cos_theta = lineParam1[0];
double sin_theta = lineParam1[1];
double x = lineParam1[2];
double y = lineParam1[3];
double k = sin_theta / cos_theta;
double b = y - k * x;

cos_theta = lineParam2[0];
sin_theta = lineParam2[1];
x = lineParam2[2];
y = lineParam2[3];
double k1 = sin_theta / cos_theta;
double b1 = y - k1 * x;

result.x = (b1 - b) / (k - k1);
result.y = k * x + b;

return result;
}

``````