我正在使用http://www.ecse.rpi.edu/~wrf/Research/Short_Notes/pnpoly.html中的算法,
但是当输入点在边界上时,这个算法就错了。有人能帮我解决边界问题吗?
如有任何帮助,我们将不胜感激。
这是主要功能

#include <iostream>
#include <Polygon.h>
using namespace std;

int main()
{
    vector<Point> v;
    //v.push_back(make_pair(3.0,3.0));
    v.push_back(make_pair(1.0,1.0));
    v.push_back(make_pair(1.0,5.0));
    v.push_back(make_pair(5.0,5.0));
    v.push_back(make_pair(5.0,1.0));
    Polygon *p = new Polygon(v);
    cout << "A: " << p->IsInside(make_pair(1.0,3.0)) << endl;
    cout << "B: " << p->IsInside(make_pair(3.0,1.0)) << endl;
    cout << "C: " << p->IsInside(make_pair(5.0,3.0)) << endl;
    cout << "D: " << p->IsInside(make_pair(3.0,5.0)) << endl;
    delete p;
    return 0;
}

这是检查功能
bool Polygon::IsInside(Point p)
{
    /*determine whether a point is inside a polygon or not
     *  polygon's vertices need to be sorted counterclockwise
     *  source :
     *      http://www.ecse.rpi.edu/~wrf/Research/Short_Notes/pnpoly.html
    */
    bool ans = false;
    for(size_t c=0,d=this->vertices.size()-1; c<this->vertices.size(); d=c++)
    {
        if( ((this->vertices[c].y > p.y) != (this->vertices[d].y > p.y)) &&
            (p.x < (this->vertices[d].x - this->vertices[c].x) * (p.y - this->vertices[c].y) /
                (this->vertices[d].y - this->vertices[c].y) + this->vertices[c].x) )
           ans = !ans;
    }
    return ans;
}

最佳答案

从网站文档:
“PNPOLY将平面划分为多边形内的点和多边形外的点边界上的点分为内部点和外部点。“…”
请再次阅读网站上提供的文档。它回答了您的问题。
最后,您可能不得不忍受浮点计算的模糊性。

关于c++ - 指向多边形的内部或边界,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23379295/

10-11 22:58
查看更多