我似乎无法正确使用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;
}
答案 0 :(得分:3)
当您使用bg::model::polygon<point, false, true>
时,您定义了一个使用point
作为其点类型的多边形,其点以逆时针顺序排列,并且关闭(意味着它的最后一点等于它的第一个)。如果你要么&#34;关闭&#34;您的多边形或使用开放多边形,bg::within
的行为似乎是您所期望的:
#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;
}