I have a big list of points that define a boundary of some (not necessarily convex) shape. I then have some query point (x, y)
and I want to determine whether (x, y)
is within the region defined by my boundary of points.
So, simple enough question. How do I determine whether the query point would be inside the shape formed by my boundary points? And is there a nice boost module for this? I am looking through boost::geometry
but haven't found anything yet..
How to check if a point is inside a polygon in Python. To perform a Point in Polygon (PIP) query in Python, we can resort to the Shapely library's functions . within(), to check if a point is within a polygon, or . contains(), to check if a polygon contains a point.
A segment cuts a polygon if it has at least one intersection that is not the end point of the segment. A segment is inside a polygon if every point of the segment is inside the polygon (end points of the segment can lay on the boundary of the polygon). These edges of a polygon are not inside this polygon.
in = inpolygon( xq , yq , xv , yv ) returns in indicating if the query points specified by xq and yq are inside or on the edge of the polygon area defined by xv and yv . [ in , on ] = inpolygon( xq , yq , xv , yv ) also returns on indicating if the query points are on the edge of the polygon area.
Pick a point outside the polygon check and see if a line from that point to your point intersects an odd number of lines that define the perimeter of the polygon. poly[i]. y should be poly[i][1] at the end of line 3. Also that function checks if the point is inside the polygon not if the point belongs to the polygon.
Seems you are looking for within
, no?
http://www.boost.org/libs/geometry/doc/html/geometry/reference/algorithms/within/within_2.html
The example they give on the page is, in fact, point-in-polygon:
#include <iostream>
#include <list>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/domains/gis/io/wkt/wkt.hpp>
int main()
{
typedef boost::geometry::model::d2::point_xy<double> point_type;
typedef boost::geometry::model::polygon<point_type> polygon_type;
polygon_type poly;
boost::geometry::read_wkt(
"POLYGON((2 1.3,2.4 1.7,2.8 1.8,3.4 1.2,3.7 1.6,3.4 2,4.1 3,5.3 2.6,5.4 1.2,4.9 0.8,2.9 0.7,2 1.3)"
"(4.0 2.0, 4.2 1.4, 4.8 1.9, 4.4 2.2, 4.0 2.0))", poly);
point_type p(4, 1);
std::cout << "within: " << (boost::geometry::within(p, poly) ? "yes" : "no") << std::endl;
return 0;
}
UPDATE: as @ildjarn points out, you might rather use covered_by
if you wanted points that lie on the polygon edge itself to count:
http://www.boost.org/libs/geometry/doc/html/geometry/reference/algorithms/covered_by/covered_by_2.html
The behavior of within
w.r.t. edges "depends", so take note of that nuance in the documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With