点(x3,y3)到经过点(x1,y1)和点(x2,y2)的直线的最短距离

时间:2022-11-05 23:00:56

/// <summary>
/// 点(x3,y3)到经过点(x1,y1)和点(x2,y2)的直线的最短距离
/// </summary>
/// <param name="pt1"></param>
/// <param name="pt2"></param>
/// <param name="pt3"></param>
/// <returns></returns>
public static double GetMinDistance(MapPoint pt1, MapPoint pt2, MapPoint pt3)
{
double dis = 0;
if (pt1.X == pt2.X)
{
dis = Math.Abs(pt3.X - pt1.X);
return dis;
}
double lineK = (pt2.Y - pt1.Y) / (pt2.X - pt1.X);
double lineC = (pt2.X * pt1.Y - pt1.X * pt2.Y) / (pt2.X - pt1.X);
dis = Math.Abs(lineK * pt3.X - pt3.Y + lineC) / (Math.Sqrt(lineK * lineK + 1));
return dis;
}