c++ - Boost::geometry 在点和多边形上使用方法内遇到问题?

标签 c++ boost boost-geometry

我似乎在正确使用 boost 几何库( Boost::geometry : calculation of the centroid of a polygon )时遇到了麻烦。我非常感谢您对我上一个问题的帮助,并且想询问一些有关 boost::geometry::within 方法的问题,回答一个几何图形是否包含在另一个几何图形中。

我在代码中使用它来检查一个点是否包含在多边形中,并且遇到了奇怪的结果,其中一个点绝对不应该位于远处的多边形内,但该方法仍然返回True 被调用时。

我正在考虑在声明我的多边形时缺少的一个微妙之处,我真的很想解决这个问题。虽然来回查看我的代码,但我缺乏调试的想法,感觉就像是目光短浅。这就是为什么我想对这个具体示例进行提示:

我的点的坐标:221.703 , 256

多边形的坐标是,逐点:

266.158 256 266.447 256.5 267.024 256.5 267.313 257 267.024 257.5 267.313 258

这显然不应该包含上面给出的观点。

我很抱歉问这个迂腐的问题,但我真的很感谢任何愿意参与其中的人

我的代码:

#include <iostream>
#include <boost/geometry.hpp>

using namespace std;
namespace bg = boost::geometry;
typedef bg::model::point<float, 2, bg::cs::cartesian> point;
typedef bg::model::box<point> box;
typedef bg::model::polygon<point, false, true> polygon;


int main(int argc, char * argv[]){
polygon pol;
pol.outer().push_back(point(266.158,256));
pol.outer().push_back(point(266.447,256.5));
pol.outer().push_back(point(267.024,256.5));
pol.outer().push_back(point(267.313,257));
pol.outer().push_back(point(267.024,257.5));
pol.outer().push_back(point(267.313,258));

double x = atof(argv[1]);
double y = atof(argv[2]);
cout << "Is inside: " << ((bg::within(point(x,y),pol)) ? "yes" : "no") << endl;
return 0;
}

最佳答案

当您使用 bg::model::polygon<point, false, true> 时您正在定义一个使用 point 的多边形作为其点类型,它的点按逆时针顺序排列,并且是闭合(意味着它的最后一个点等于它的第一个点)。如果您“关闭”多边形或使用开放多边形, bg::within 的行为似乎是你所期望的:

Running on Coliru

#include <iostream>
#include <boost/geometry.hpp>

using std::cout;
using std::endl;
namespace bg = boost::geometry;
typedef bg::model::point<float, 2, bg::cs::cartesian> point;
typedef bg::model::box<point> box;
typedef bg::model::polygon<point, false, true> closed_polygon;
typedef bg::model::polygon<point, false, false> open_polygon;



int main(int argc, char * argv[])
{
    {
        closed_polygon pol;
        pol.outer().push_back(point(266.158,256));
        pol.outer().push_back(point(266.447,256.5));
        pol.outer().push_back(point(267.024,256.5));
        pol.outer().push_back(point(267.313,257));
        pol.outer().push_back(point(267.024,257.5));
        pol.outer().push_back(point(267.313,258));
        pol.outer().push_back(point(266.158,256));//you need to close the polygon

        double x = 222;
        double y = 257;
        cout << "Is " << bg::wkt<point>(point(x,y)) << " inside: " << ((bg::within(point(x,y),pol)) ? "yes" : "no") << endl;

        x = 267;
        y = 257;
        cout << "Is " << bg::wkt<point>(point(x,y)) << " inside: " << ((bg::within(point(x,y),pol)) ? "yes" : "no") << endl;
    }

    {
        open_polygon pol;
        pol.outer().push_back(point(266.158,256));
        pol.outer().push_back(point(266.447,256.5));
        pol.outer().push_back(point(267.024,256.5));
        pol.outer().push_back(point(267.313,257));
        pol.outer().push_back(point(267.024,257.5));
        pol.outer().push_back(point(267.313,258));

        double x = 222;
        double y = 257;
        cout << "Is " << bg::wkt<point>(point(x,y)) << " inside: " << ((bg::within(point(x,y),pol)) ? "yes" : "no") << endl;

        x = 267;
        y = 257;
        cout << "Is " << bg::wkt<point>(point(x,y)) << " inside: " << ((bg::within(point(x,y),pol)) ? "yes" : "no") << endl;
    }

    return 0;
}

关于c++ - Boost::geometry 在点和多边形上使用方法内遇到问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23519104/

相关文章:

c++ - 为什么 std::list::splice 不是一个自由函数?

c++ - 使用私有(private)成员 boost 派生类的序列化

c++ - 查询从内存映射文件中检索到的 Rtree 时出现段错误

c++ - 使用 C++ 在 Linux 中使用 select 读取 ICMP 回复

全局命名空间的 C++ 别名

c++ - STL 迭代器 - 目的

c++ - 如何使用 Boost::Geometry _union 与整数

c++ - 是否有像 boosts bcp for Eigen 这样的工具?

c++ - Boost spirit 解析器属性类型不起作用。

c++ - 在 Boost.Geometry rtree 中存储 OGRPoint