【问题标题】:Test point inside a polygon多边形内的测试点
【发布时间】:2013-04-10 14:35:44
【问题描述】:

我正在尝试测试一个点是否在多边形内,并查看 SO。我找到了一些可以做到的代码,但我试过了,我不知道我做错了什么......

我到 Vectors 来保存 x 和 y 点:

Vector<Double> vxpoints;
Vector<Double> vxpoints;

这就是我的方法“包含”

public boolean contains(double x, double y) {       
    int i,j = this.npoints - 1;  
    boolean oddNodes = false;  

    for(i=0;i<this.npoints;j=i++) {
        if ((((this.vypoints.get(i) <= y) && (y < this.vypoints.get(j))) ||
                ((this.vypoints.get(j) <= y) && (y < this.vypoints.get(i)))) &&
                (x < (this.vxpoints.get(j) - this.vxpoints.get(i)) * (y - this.vypoints.get(i)) / (this.vypoints.get(j) - this.vypoints.get(i)) + this.vxpoints.get(i)))
            oddNodes = !oddNodes;
    }   
    return oddNodes;

当我测试它时,我会使用“简单的多边形”: (有数组 os 点,我在我的类中转换成向量)

    double xpoints[] = {100,100,200,200}; //Square      
    double ypoints[] = {100,200,100,200};
    PolygonDouble test = new PolygonDouble(xpoints, ypoints);

    //System.out.println(test.getNumberOfCoordinates());
    if(test.contains(110,110))
        System.out.println("Inside");
    else
        System.out.println("Outside");

输出:--> 外部 但如果我尝试使用点 (110,111) 输出 --> 内部。

我不知道发生了什么事.....:S

【问题讨论】:

    标签: java android polygon


    【解决方案1】:

    问题在于测试中使用的正方形的定义。顶点的顺序错误。更改第三个和第四个顶点的顺序,测试应该可以工作。

    double xpoints[] = {100,100,200,200}; //Square      
    double ypoints[] = {100,200,200,100};
    

    【讨论】:

    • 已更改并工作,并使用其他“简单”多边形进行了测试。所以,如果我不理解不好......这意味着代码是正确的,但我必须按照时钟顺序定义点的顺序?
    • 多边形是顺时针还是逆时针都没有关系。但是,由于顶点的顺序,您的“矩形”被扭曲成蝴蝶结形状(因此它不是真正的矩形)确实很重要。
    • 不是顺时针或逆时针。你越过了广场的两条线。当测试失败时,有三个选项:您的测试代码错误,测试本身错误或两者都错误:-)
    • (1/2) 好吧,有了这个“简单”的多边形,就知道它是有效的。现在,另一个问题。我为此所做的一切,是因为我必须读取 KML 文件,提取坐标,然后测试一个点是否在该坐标内。我做所有这些。所以我的KML文件看起来像的坐标:<...> 2.190381090601514,41.40432338788778,0 2.193329807442894,41.40506539312711,0 2.193393168346678,41.40527938280415,0 2.191414958783979,41.40665429357642,0 2.189340368185269,41.4050514432114,0 2.190381090601514,41.40432338788778,0 坐标>
    • (2/2) 当我处理它并保存在向量中时,这些向量看起来像这样:Vx:[2.190381090601514, 2.193329807442894, 2.193393168346678,2.191414958783979, 2.189340368185269, 2.190381090601514] Vy:[41.40432338788778, 41.40506539312711, 41.40527938280415, 41.40665429357642, 41.4050514432114, 41.40432338788778] Test2: Outside And the tested point: test.contains(2.191680866734278,41.40505441788871) (Suposed to be inside!!) Test2: Outside
    猜你喜欢
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-28
    • 2019-07-10
    相关资源
    最近更新 更多