http://www.cnblogs.com/object360/p/5788966.html
C#判断点是否在直线上
判断点在直线上,实际上就是判断点到直线上的垂直距离。点到直线垂直距离为0,则点在线上。当然也可以误差设置误差几个像素。
参考资料并进行修改:http://blog.sina.com.cn/s/blog_5d5c80840101bnhw.html
//判断点是否在直线上
public static bool GetPointIsInLine(PointF pf, PointF p1, PointF p2, double range)
{
//range 判断的的误差,不需要误差则赋值0
//点在线段首尾两端之外则return false
double cross = (p2.X - p1.X) * (pf.X - p1.X) + (p2.Y - p1.Y) * (pf.Y - p1.Y);
if (cross <= 0) return false;
double d2 = (p2.X - p1.X) * (p2.X - p1.X) + (p2.Y - p1.Y) * (p2.Y - p1.Y);
if (cross >= d2) return false;
double r = cross / d2;
double px = p1.X + (p2.X - p1.X) * r;
double py = p1.Y + (p2.Y - p1.Y) * r;
//判断距离是否小于误差
return Math.Sqrt((pf.X - px) * (pf.X - px) + (py - pf.Y) * (py - pf.Y)) <= range;
}
代码源码:http://download.csdn.net/detail/object360/9607980