经过图像处理,得到两直线方程,求两直线交点;
直线的一般方程为F(x) = ax + by + c = 0。既然我们已经知道直线的两个点,假设为(x0,y0), (x1, y1),那么可以得到a = y0 – y1, b = x1 – x0, c = x0y1 – x1y0。
因此我们可以将两条直线分别表示为
F0(x) = a0*x + b0*y + c0 = 0, F1(x) = a1*x + b1*y + c1 = 0
那么两条直线的交点应该满足
a0*x + b0*y +c0 = a1*x + b1*y + c1
由此可推出
x = (b0*c1 – b1*c0)/D
y = (a1*c0 – a0*c1)/D
D = a0*b1 – a1*b0, (D为0时,表示两直线平行)
void GetCrossPoint(float A1, float B1, float A3, float B3,int count,vector<Point2d>& cross_Pt)
{
int L1x0 = 1, L1x1 = 500, L3x0 = 1, L3x1 = 500; //像素坐标X上随便取两点
float L1y0 = A1*L1x0 + B1; //根据已知直线方程求对应的Y值
float L1y1 = A1*L1x1 + B1;
float L3y0 = A3*L3x0 + B3;
float L3y1 = A3*L3x1 + B3;
float a1 = L1y0 - L1y1;
float b1 = L1x1 - L1x0;
float c1 = L1x0*L1y1 - L1x1*L1y0;
float a3 = L3y0 - L3y1;
float b3 = L3x1 - L3x0;
float c3 = L3x0*L3y1 - L3x1*L3y0;
float D = a1*b3 - a3*b1;
double x = (b1*c3 - b3*c1) / D;
double y = (a3*c1 - a1*c3) / D;
Point2d tmp_pt;
tmp_pt.x = x;
tmp_pt.y = y;
cross_Pt.push_back(tmp_pt);
}