简体   繁体   English

多边形内的测试点

[英]Test point inside a polygon

I'm trying to test if a point is insde a polygon o not, and looking in SO. 我正在尝试测试一个点是否是多边形o并且查看SO。 i found some codes that does it, but I tried, and I don't know what I'm doing wrong... 我找到了一些代码,但是我试过了,我不知道我做错了什么......

I got to Vectors to save x and y points: 我得到了Vector来保存x和y点:

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

thats my method "contains" 多数民众赞成我的方法“包含”

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;

And when I test it, I do with "easy polygons": (There are to arrays os points, that I convert in vectors inside my class) 当我测试它时,我使用“简单的多边形”:(有数组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");

Output: --> Outside but if I try with the point (110,111) the Output --> Inside. 输出: - >外部,但如果我尝试使用点(110,111)输出 - >内部。

I don't kwow what's going on..... :S 我不知道发生了什么.....:S

The problem is in the definition of your square used in your test. 问题在于测试中使用的方块的定义。 The vertices are the wrong order. 顶点是错误的顺序。 Change the order of the third and fourth vertex and it the test should work. 更改第三个和第四个顶点的顺序,测试应该起作用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM