zoukankan      html  css  js  c++  java
  • [转]判断一个点是否落在多边形内

    转自:

    http://www.cnblogs.com/gis_gps/archive/2009/02/12/1389071.html

     备用

     

    一、背景:

      如何判断一个指定的经纬度点是否落在一个多边形区域内?

    二、实现代码(delphi)

     


    Type
      TMyPoint 
    = packed record
        X : double;
        Y : double;
      
    end;

    {*------------------------------------------------------------------------------
      判断指定的经纬度坐标点是否落在指定的多边形区域内
      @param ALon   指定点的经度
      @param ALat   指定点的纬度
      @param APoints   指定多边形区域各个节点坐标
      @return True 落在范围内 False 不在范围内
    ------------------------------------------------------------------------------*
    }
    function IsPtInPoly(ALon, ALat: double; APoints: array of TMyPoint): Boolean;
    var
      iSum, iCount, iIndex: Integer;
      dLon1, dLon2, dLat1, dLat2, dLon: double;
    begin
      Result :
    = False;
      
    if (Length(APoints) < 3then
      
    begin
        Result :
    = False;
        Exit;
      
    end;
      iSum :
    = 0;
      iCount :
    = Length(APoints);
      
    for iIndex :=0 to iCount - 1 do
      
    begin
        
    if (iIndex = iCount - 1then
        
    begin
          dLon1 :
    = APoints[iIndex].X;
          dLat1 :
    = APoints[iIndex].Y;
          dLon2 :
    = APoints[0].X;
          dLat2 :
    = APoints[0].Y;
        
    end
        
    else
        
    begin
          dLon1 :
    = APoints[iIndex].X;
          dLat1 :
    = APoints[iIndex].Y;
          dLon2 :
    = APoints[iIndex + 1].X;
          dLat2 :
    = APoints[iIndex + 1].Y;
        
    end;
        
    if ((ALat >= dLat1) and (ALat < dLat2)) or ((ALat>=dLat2) and (ALat < dLat1)) then
        
    begin
          
    if (abs(dLat1 - dLat2) > 0then
          
    begin
            dLon :
    = dLon1 - ((dLon1 -dLon2) * (dLat1 -ALat)) / (dLat1 - dLat2);
            
    if (dLon < ALon) then
              Inc(iSum);
          
    end;
        
    end;

      
    end;
      
    if (iSum mod 2 <> 0then
        Result :
    = True;
    end;

     

    ____________________________________________________________________________________________________________________________

     C#算法:

      /// <summary>
            ///  判断指定的经纬度坐标点是否落在指定的多边形区域内
            /// </summary>
            /// <param name="ALon">指定点的经度</param>
            /// <param name="ALat">指定点的纬度</param>
            /// <param name="APoints">指定多边形区域各个节点坐标</param>
            /// <returns>True 落在范围内 False 不在范围内</returns>
            public bool isPtInPoly(double ALon, double ALat, Point[] APoints)
            {
                int iSum, iCount, iIndex;
                double dLon1, dLon2, dLat1, dLat2, dLon;
                if (APoints.Length < 3)
                {
                    return false;
                }
                iSum = 0;
                iCount = APoints.Length;
                for (iIndex = 0; iIndex < iCount - 1; iIndex++)
                {
                    if (iIndex == iCount - 1)
                    {
                        dLon1 = APoints[iIndex].X;
                        dLat1 = APoints[iIndex].Y;
                        dLon2 = APoints[0].X;
                        dLat2 = APoints[0].Y;
                    }
                    else
                    {
                        dLon1 = APoints[iIndex].X;
                        dLat1 = APoints[iIndex].Y;
                        dLon2 = APoints[iIndex + 1].X;
                        dLat2 = APoints[iIndex + 1].Y;
                    }
                    if (((ALat >= dLat1) && (ALat < dLat2)) || ((ALat >= dLat2) && (ALat < dLat1)))
                    {
                        if (Math.Abs(dLat1 - dLat2) > 0)
                        {
                            dLon = dLon1 - ((dLon1 - dLon2) * (dLat1 - ALat)) / (dLat1 - dLat2);
                            if (dLon < ALon)
                                iSum++;
                        }
                    }

                }
                if ((iSum % 2) != 0)
                    return true;
                return false;
            }

     


  • 相关阅读:
    工业设计之美
    狠挖用户需求与用户分析——赫志中
    《必然》
    在一周内学会使用 AUTO CAD
    可控硅调光知识总结
    PADS Logic Decal、Layout Decal绘制
    BUCK-BOOST反激变压器设计
    RCC BUCK-BOOST变压器设计
    产品生产
    由《旧制度与大革命》提取的5个感触
  • 原文地址:https://www.cnblogs.com/zhuor/p/2113238.html
Copyright © 2011-2022 走看看