【问题标题】:Alternative tests for finding whether a point is inside a 2D polygon as standard tests fail由于标准测试失败,用于查找点是否在 2D 多边形内的替代测试
【发布时间】:2016-02-23 02:47:33
【问题描述】:

我编写了以下代码来确定一个点是否在多边形内——该算法使用交叉数和绕组数测试。但是,我发现当我的多边形是:(10,10),(10,20),(20,20)和(20,10)以及我想查找它是否在多边形中的点时,这些测试会失败是:(20,15)。我这里的点代表位置的(纬度,经度)。

(20,15) 位于边界上,因此应该在多边形内

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;

typedef struct {int x, y;} Pt;

inline int
isLeft( Pt P0, Pt P1, Pt P2 )
{
    return ( (P1.x - P0.x) * (P2.y - P0.y)
            - (P2.x -  P0.x) * (P1.y - P0.y) );
}
int crossingNumTest( Pt P, Pt* V, int n )
{
    int    cn = 0;    

    for (int i=0; i<n; i++) {    
       if (((V[i].y <= P.y) && (V[i+1].y > P.y)) 
        || ((V[i].y > P.y) && (V[i+1].y <=  P.y))) { 
            float vt = (float)(P.y  - V[i].y) / (V[i+1].y - V[i].y);
            if (P.x <  V[i].x + vt * (V[i+1].x - V[i].x)) 
                 ++cn;   
        }
    }
    return (cn&1);    
}
int windingNumTest( Pt P, Pt* V, int n )
{
    int    wn = 0;    

    for (int i=0; i<n; i++) {   
        if (V[i].y <= P.y) {    
            if (V[i+1].y  > P.y)  
                 if (isLeft( V[i], V[i+1], P) > 0)  
                     ++wn;           
        }
        else {                       
            if (V[i+1].y  <= P.y)    
                 if (isLeft( V[i], V[i+1], P) < 0)  
                     --wn;          
        }
    }
    return wn;
}
//===================================================================
int main(int argc, char** argv) {

    Pt arr[11];
    arr[0].x=10;
    arr[0].y=10;
    arr[1].x=10;
    arr[1].y=20;
    arr[2].x=20;
    arr[2].y=20;
    arr[3].x=20;
    arr[3].y=10;

    Pt test;
    test.x=20; test.y=15;

    cout<<"1.polygon inside="<<crossingNumTest(test,arr,4)<<"\n";
    cout<<"2.polygon inside="<<windingNumTest(test,arr,4)<<"\n";

    return (EXIT_SUCCESS);
}

【问题讨论】:

  • 如果您希望点在边界上,您可以将0.01 之类的内容添加到多边形中的每个点。
  • 您的输入是什么 - 精确数字还是浮点数?
  • @maxim1000 浮点数
  • 发布的代码将 C 头文件与 C++ 头文件混合在一起,因此,充其量这是 C++ 代码。请删除“c”标签
  • 在计算中,允许直线和点之间的差异为 0,因此如果差异为 0,则可以在多边形内考虑点。但是请注意,不应比较浮点数精确 (==) 值,但是,相对值()是可以接受的。

标签: c++ c geolocation geometry geo


【解决方案1】:

这很可能是浮点精度问题。

您正在检查的点正好在多边形的线上,因此很可能浮点无法正确表示它并在不正确时将其显示为在外面。

您可以接受这种行为,也可以在检查时添加一个小的容错。请参阅What is the most effective way for float and double comparison? 以获得更大的图景。实际上,您需要确定您将接受的容差,并在检查时稍微扩展您的多边形。

【讨论】:

  • @JannatArora 查看扩展答案。
【解决方案2】:

不需要float 并会导致潜在的舍入问题。
坚持使用精确(可能更快)的整数数学。将测试改成数学等价。
(注意:这种新方法不可能除以 0,因为没有进行除法)

// float vt = (float)(P.y  - V[i].y) / (V[i+1].y - V[i].y);
// if (P.x <  V[i].x + vt * (V[i+1].x - V[i].x)) ++cn;   

//  Note: Use long long math if needed
int dy0 = P.y  - V[i].y;
int dy1 = V[i+1].y - V[i].y;
int dx0 = P.x  - V[i].x;
int dx1 = (V[i+1].x - V[i].x;
if (dy1*dx0 < dy0*dx1) ++cn;

[编辑]@user3629249 comment 指出了数组访问冲突。建议:

int crossingNumTest( Pt P, Pt* V, int n ) {
    // for (int i=0; i<n; i++) {    
    for (int i=1; i<n; i++) {    
       // if (((V[i].y <= P.y) && (V[i+1].y > P.y)) 
       if (((V[i-1].y <= P.y) && (V[i].y > P.y)) 
    ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    • 1970-01-01
    • 2022-06-14
    相关资源
    最近更新 更多