计算两直线交点

对于两直线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; 

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值