【问题标题】:How to check route by coordinates如何通过坐标检查路线
【发布时间】:2012-10-15 15:38:12
【问题描述】:

我有一个问题:我有两个坐标(起点和终点位置)和一个多边形。我想检查这条路线是(1)从多边形的外部到内部,(2)从内部到外部,(3)仅在内部,还是(4)仅在外部。

非常感谢任何帮助。

【问题讨论】:

  • 您可能有一个algorithm for that。你有没有尝试实现它?
  • Google for 点多边形算法

标签: java algorithm polygon


【解决方案1】:

你很幸运! Java 包含一个Polygon 类,它有一个方法contains(double x, double y)。使用它!

【讨论】:

    【解决方案2】:

    查看此页面: http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html

    这个函数取自那里。

    这是基于观察到测试点在多边形内,如果在 y 轴上投影时,它的 x 值低于多边形边的奇数。适用于凸多边形和凹多边形。

    int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy)
    {
      int i, j, c = 0;
      for (i = 0, j = nvert-1; i < nvert; j = i++) {
        if ( ((verty[i]>testy) != (verty[j]>testy)) &&
         (testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
           c = !c;
      }
      return c;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-19
      • 2013-10-31
      • 2019-11-29
      • 1970-01-01
      • 1970-01-01
      • 2018-01-01
      • 1970-01-01
      • 2021-07-21
      相关资源
      最近更新 更多