Reputation: 421
I'm trying to detect every coordinates possible for my polygon. (it's not everytime a triangle, it's for that).But when i'm trying to check every x & y possible, my program is not working at all.
Here is my program
int main()
{
int x = 0;
int y = 0;
typedef boost::geometry::model::d2::point_xy<double> point_type;
boost::geometry::model::polygon<point_type> poly2;
boost::geometry::read_wkt("POLYGON((375 200,700 900,1100 190))", poly2);
for (x = 0; x < 1200; x++)
{
if (x > 1150)
{
x = 0;
y++;
}
else if (y > 1000)
exit(0);
else
{
point_type p(x, y);
bool check_covered = boost::geometry::within(p, poly2);
if (check_covered)
{
std::cout << "in" << std::endl;
}
}
}
return 0;
}
But i don't have output. Basically, they're never entering in my "if (check_covered)
"
I don't understand why. When i'm using a draw to check if the polygon is working, i have my triangle
Upvotes: 2
Views: 828
Reputation: 20918
Geometry of polygon is not valid.
Template parameter(s)
bool Closed
Default true
You can check if a geometry is valid by boost::geometry::is_valid
, for your polygon it returns false.
You may add the last point:
"POLYGON((375 200,700 900,1100 190,375 200))"
or call boost::geometry::correct(poly2)
(it will add a missing point) before checking by within
.
Here's a live demo, also making the point iteration a bit more effective:
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <iostream>
namespace bg = boost::geometry;
namespace bgm = bg::model;
using Point = bgm::d2::point_xy<double>;
int main()
{
bgm::polygon<Point> poly;
bgm::box<Point> box;
bg::read_wkt("POLYGON((375 200,700 900,1100 190,375 200))", poly);
bg::envelope(poly, box);
for (int x = box.min_corner().x(); x < box.max_corner().x(); ++x)
for (int y = box.min_corner().y(); y < box.max_corner().y(); ++y)
if (Point p(x,y); bg::within(p, poly)) {
std::cout << "in: " << bg::wkt(p) << std::endl;
}
}
Prints
in: POINT(376 200)
in: POINT(376 201)
in: POINT(376 202)
in: POINT(377 200)
in: POINT(377 201)
in: POINT(377 202)
in: POINT(377 203)
in: POINT(377 204)
...
etc.
Upvotes: 2