Reputation: 551
I have a std::list
of 2D points and would like to test if one point p
lies inside the polygon given by the vector. I already found that boost::geometry
has a function within
to test this. Unfortunately I seem to use it in the wrong way:
void someFunction(...) {
namespace bpl = boost::polygon;
typedef bpl::polygon_data<double> Polygon;
typedef bpl::polygon_traits<Polygon>::point_type Point;
Polygon polygon;
std::vector<Point> points;
for (std::list<MyPointType>::const_iterator it = myPolygonPoints.begin(); it != myPolygonPoints.end(); ++it) {
points.push_back(Point(it->GetX(),it->GetY()));
}
polygon.set(points.begin(),points.end());
// ...
if (!boost::geometry::within(Point(someX,someY),polygon)) {
doSomething();
}
//...
}
I get various compile errors, starting by unmatched types in the call of within
.
So what is the correct way to build a polygon and use it with within
?
Greetings
Upvotes: 1
Views: 1729
Reputation: 3713
Why not stick with the types that are given in the examples? See this page, for instance.
typedef boost::geometry::model::d2::point_xy<double> Point;
typedef boost::geometry::model::polygon<Point> Polygon;
For me it works then.
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
//...
Polygon poly;
//... construct the polygon
Point p(4, 1);
std::cout << "within: " << (boost::geometry::within(p, poly) ? "yes" : "no") << std::endl;
Upvotes: 1
Reputation: 7225
There is an example from http://www.boost.org/doc/libs/1_47_0/libs/geometry/doc/html/geometry/reference/algorithms/within/within_2.html
Based on what you mean, the code:
if (!boost::geometry::within(Point(someX,someY),polygon)) {
doSomething();
}
should be:
Point point(someX,someY);
if (!boost::geometry::within(point,polygon)) {
doSomething();
}
or:
Point *ppoint = new point(someX,someY);
if (!boost::geometry::within(*ppoint,polygon)) {
doSomething();
}
Because direct call for constructor Point(someX,someY) as a function without class instantiation is not allowed in C++.
Upvotes: 0