在图像中求距离用到较多的是对两点式直线求距离,这篇文章给出的代码也是基于这种情况,在给定直线上两个点的情况下求直线外一点到直线的距离。
首先回顾下直线的多种表达公式:
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& line