对于两直线a1*x + b1*y = c1, a2*x + b2*y = c2
由克莱姆法则得,D = a1 * b2 - b1*a2, D1 =c1*b2 - b1*c2, D2 = a1*c2 - c1*a2
解得交点: x = D1 / D, y = D2 / D
直线方程用两点法可表示为 (y2 - y1)*x - (x2 - x1)*y = x1*(y2 - y1) - y1*(x2 - x1)
a = (y2 - y1), b = - (x2 - x1), c = x1*(y2 - y1) - y1*(x2 - x1)
bool Intersection(CvPoint astart, CvPoint aend, CvPoint bstart, CvPoint bend, CvPoint &pt)
{
float dxa = astart.x - aend.x;
float dxb = bstart.x - bend.x;
float dya = astart.y - aend.y;
float dyb = bstart.y - bend.y;
float p = astart.x * aend.y - astart.y * aend.x;
float q = bstart.x * bend.y - bstart.y * bend.x;
float denom = dxa * dyb - dya * dxb;
if(abs(denom) < 1e-12)
return false;
float x =(p * dxb - dxa * q) / denom;
float y = (p * dyb - dya *q) / denom;
pt.x = cvRound(x);
pt.y = cvRound(y);
return true;
}