【问题标题】:Given a list of GPS coordinates making up a boundary, how can I calculate if my location is within that boundary?给定构成边界的 GPS 坐标列表,我如何计算我的位置是否在该边界内?
【发布时间】:2016-02-19 17:20:14
【问题描述】:

假设我有数百甚至数千个 GPS 坐标、纬度和经度,它们构成了一个国家的边界​​。

我也有我当前的位置纬度和经度。

我如何确定(使用 C#,为 Windows10 UWP 编程)我的位置是否在一个国家/地区的边界内?

例如假设我有构成下图中红线的所有点。如果我在 X 位置,我的函数将返回 true。如果我在 Y 位置,我的函数将返回 false。

【问题讨论】:

标签: latitude-longitude computational-geometry point-in-polygon


【解决方案1】:

定义一个肯定在外面,或者肯定在里面的点。

现在创建一条从您的位置到该已知点的线,并将其与圆周的每个部分相交。计算交叉点的数量。 如果偶数,您就是您之前定义的点(内部或外部) 如果奇怪,则与您定义的点相反

https://en.wikipedia.org/wiki/Point_in_polygon应该解释得更好!

【讨论】:

    【解决方案2】:

    多边形中的点 (PIP) 是计算几何中的一个众所周知的问题。 Wikipedia 提到了两种常见的解决方案/算法,光线投射算法和绕组数算法。您可以尝试自己实现它们或搜索一些库(Java Topology Suite 提供了 Java 的解决方案,有一个名为 NetTopologySuite 的 .NET 端口)。

    【讨论】:

      【解决方案3】:

      感谢收到的两个答案,我发现了我正在尝试做的事情的实际名称......“多边形中的点”。

      知道了这一点,我找到了这个Stack Overflow question and answer

      如果上述内容消失,以下是代码:

      /// <summary>
      /// Determines if the given point is inside the polygon
      /// </summary>
      /// <param name="polygon">the vertices of polygon</param>
      /// <param name="testPoint">the given point</param>
      /// <returns>true if the point is inside the polygon; otherwise, false</returns>
      public static bool IsPointInPolygon4(PointF[] polygon, PointF testPoint)
      {
          bool result = false;
          int j = polygon.Count() - 1;
          for (int i = 0; i < polygon.Count(); i++)
          {
              if (polygon[i].Y < testPoint.Y && polygon[j].Y >= testPoint.Y || polygon[j].Y < testPoint.Y && polygon[i].Y >= testPoint.Y)
              {
                  if (polygon[i].X + (testPoint.Y - polygon[i].Y) / (polygon[j].Y - polygon[i].Y) * (polygon[j].X - polygon[i].X) < testPoint.X)
                  {
                      result = !result;
                  }
              }
              j = i;
          }
          return result;
      }
      

      它需要一个组成多边形的点数组和一个要测试的点并返回真或假。在我的所有测试中运行良好。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-02-20
        • 2012-08-25
        • 1970-01-01
        • 1970-01-01
        • 2010-09-19
        • 2011-02-07
        • 1970-01-01
        • 2015-04-03
        相关资源
        最近更新 更多