Boost C++ Libraries: Ticket #5312: polygon contains() gives wrong values for axis-aligned rectangles https://svn.boost.org/trac10/ticket/5312 <p> It appears that when a polygon is an axis-aligned rectangle, all (!) points along the projection of the Y-axis sides (i.e. with the same X coordinate as an edge of the polygon) show up as "inside" the polygon. </p> <p> A workaround for many cases is to pass consider_touch==true, which fixes the clearly incorrect results at the expense of proper edge sensitivity. </p> <p> I didn't test other refinements than polygon, so I don't know if this behavior is shared or specific. </p> <p> Tested against Boost 1.46.0 and SVN as of submission date. </p> <pre class="wiki">#include &lt;boost/polygon/polygon.hpp&gt; #include &lt;assert.h&gt; namespace gtl = boost::polygon; typedef gtl::polygon_data&lt;int&gt; Polygon; typedef gtl::polygon_traits&lt;Polygon&gt;::point_type Point; int main() { // Axis-aligned rectangle const Point rect[] = { Point(0, 0), Point(0, 1), Point(2, 1), Point(2, 0) }; Polygon p; gtl::set_points(p, rect, rect+(sizeof(rect)/sizeof(rect[0]))); // Points inside the polygon: these cases work. assert(gtl::contains(p, Point(1, 0))); assert(gtl::contains(p, Point(1, 1))); // Points outside the polygon: these cases work. assert(!gtl::contains(p, Point(1, -1))); assert(!gtl::contains(p, Point(1, 2))); assert(!gtl::contains(p, Point(3, 2))); assert(!gtl::contains(p, Point(-1, 2))); // Point outside the polygon with consider_touch false, this works assert(!gtl::contains(p, Point(2, 2), false)); // Points outside the polygon: these cases FAIL. Note X // coordinate lies along the projection of the vertical edges. assert(!gtl::contains(p, Point(0, 2))); assert(!gtl::contains(p, Point(2, -1))); assert(!gtl::contains(p, Point(0, -1))); assert(!gtl::contains(p, Point(2, 2))); return 0; } </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/5312 Trac 1.4.3 anonymous Thu, 24 Mar 2011 06:53:11 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/5312#comment:1 https://svn.boost.org/trac10/ticket/5312#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> I overlooked the vertical edge case. point on-above-or-below line segment returns on the line for vertical case even if the point is past the end of the line. Now vertical segments are special cased and handled correctly. </p> Ticket