GIS中的基本算法之判断点是否在直线上

        注:算法的原理描述均来自《地理信息系统算法基础》一书;

        原理:设点为Q,线段为P1P2,判断点Q在线段上的依据是:(Q-P1)×(P2-P1)=0(注意这里的减法和乘法均是向量算法,有不懂向量运算原理的可以在网上搜一下)且Q在以P1P2为对角定点的矩形内。前者保证Q点在直线上,后者保证Q点不在线段的延长线或反向延长线上,这一步骤其实就是分别判断一下Q点的x、y坐标是否在P1、P2点的x、y坐标之间;

       实现:

    #region==判断点是否在直线上 ==
    /// <summary>
    /// Determines whether [is point on line] [the specified points].
    /// </summary>
    /// <param name="points">The point.</param>
    /// <param name="lineStartPoint">The line start point.</param>
    /// <param name="lineEndPoint">The line end point.</param>
    /// <returns>
    ///   <c>true</c> if [is point on line] [the specified points]; otherwise, <c>false</c>.
    /// </returns>
    public static bool isPointOnLine(Point point,Point lineStartPoint,Point lineEndPoint )
    {
        //(P-P1)*(P2-P1)=0 向量差乘是否为0; 
        double X21,Y21,X10,Y10;
        X21 = lineEndPoint.X-lineStartPoint.X;
        Y21 = lineEndPoint.Y-lineStartPoint.Y;
        X10 = point.X-lineStartPoint.X;
        Y10 = point.Y-lineStartPoint.Y;

        //向量乘积为0则该点在线上
        double vectorValue = X21*Y10-X10*Y21;
        if (vectorValue != 0.0)
	        return  false;
        else
        {
            double xMin = Math.Min(lineStartPoint.X,lineEndPoint.X);
            double xMax = Math.Max(lineEndPoint.X,lineStartPoint.X);
            double yMin = Math.Min(lineStartPoint.Y,lineEndPoint.Y);
            double yMax = Math.Max(lineEndPoint.Y,lineStartPoint.Y);

            //判断点是否是在该线的延长线上
            if (xMin <= point.X && point.X <= xMax && yMin <= point.Y && point.Y <= yMax)
                return true;
            else
                return false;
        }   
    }
    #endregion


转载自:https://blog.csdn.net/kangming310/article/details/9397287

You may also like...

退出移动版