图像中求点到直线的距离

来源:互联网 发布:单片机开发板有什么用 编辑:程序博客网 时间:2024/05/19 02:06

在图像中求距离用到较多的是对两点式直线求距离,这篇文章给出的代码也是基于这种情况,在给定直线上两个点的情况下求直线外一点到直线的距离。
首先回顾下直线的多种表达公式:
http://baike.baidu.com/view/2136602.htm
以及点到直线的距离公式:
http://baike.baidu.com/subview/3268213/3268213.htm
那么从两点式化为一般式:
// 两点式公式为(y - y1)/(x - x1) = (y2 - y1)/ (x2 - x1)
// 化简为一般式为(y2 - y1)x + (x1 - x2)y + (x2y1 - x1y2) = 0
最后,照着公式走就可以了。

//HEADER内声明:typedef struct _point2i{    double row;    double column;}point2i;//SOURCE:void point2lineDis(const point2i& point, const point2i& linePoint1, const point2i& linePoint2, double& distance){    double A, B, C;    A = linePoint2.column - linePoint1.column;    B = linePoint1.row - linePoint2.row;    C = linePoint2.row * linePoint1.column - linePoint1.row * linePoint2.column;    distance = abs(A * point.row + B * point.column + C) / sqrt(A * A + B * B);}
0 0
原创粉丝点击