ArcGIS 点到直线的距离

/****点到直线的距离***
 * 过点(x1,y1)和点(x2,y2)的直线方程为:KX -Y + (x2y1 - x1y2)/(x2-x1) = 0
 * 设直线斜率为K = (y2-y1)/(x2-x1),C=(x2y1 - x1y2)/(x2-x1)
 * 点P(x0,y0)到直线AX + BY +C =0DE 距离为:d=|Ax0 + By0 + C|/sqrt(A*A + B*B)
 * 点(x3,y3)到经过点(x1,y1)和点(x2,y2)的直线的最短距离为:
 * distance = |K*x3 - y3 + C|/sqrt(K*K + 1)
 */
public static double GetMinDistance(IPoint pt1, IPoint pt2, IPoint 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;
}

posted @ 2014-04-02 09:08  oftenlin  阅读(4597)  评论(0编辑  收藏  举报