public class LonLatCalibration
{
//已知两点(x1,y1) , (x2,y2)
//代入两点公式: (x-x1)/(x2-x1)=(y-y1)/(y2-y1)
//直线方程的公式有以下几种:
//斜截式:y=kx+b
//截距式:x/a+y/b=1
//两点式:(x-x1)/(x2-x1)=(y-y1)/(y2-y1)
//一般式:ax+by+c=0
//只要知道两点坐标,代入任何一种公式,都可以求出直线的方程。
//设直线方程为ax+by+c=0,点坐标为(m,n)
//则垂足为((b*b*m-a*b*n-a*c)/(a*a+b*b),(a*a*n-a*b*m-b*c)/(a*a+b*b))
/// <summary>
/// p点到(a,b)点两所在直线的垂点坐标
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="p"></param>
/// <returns></returns>
public static Vector GetFoot(Vector a, Vector b, Vector p)
{
double fa = b.y - a.y;
double fb = a.x - b.x;
double fc = a.y * b.x - a.x * b.y;
Vector foot = new Vector();//垂足
foot.x = (fb * fb * p.x - fa * fb * p.y - fa * fc) / (fa * fa + fb * fb);
foot.y = (fa * fa * p.y - fa * fb * p.x - fb * fc) / (fa * fa + fb * fb);
return foot;
}
/// <summary>
/// p点是否在(a,b)两点所在直线上
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="foot"></param>
/// <returns></returns>
public static bool DotIsOnLine(Vector a, Vector b, Vector foot)
{
return Math.Min(a.x, b.x) <= foot.x && foot.x <= Math.Max(a.x, b.x) && Math.Min(a.y, b.y) <= foot.y && foot.y <= Math.Max(a.y, b.y);
}
}
public class Vector
{
public double x;
public double y;
public override string ToString()
{
return string.Format("{0},{1}", x, y);
}
}
本文转自94cool博客园博客,原文链接:http://www.cnblogs.com/94cool/archive/2012/10/25/2738996.html,如需转载请自行联系原作者