Boost C++ Libraries: Ticket #7813: Bug in boost::geometry::centroid with trivially small polygons https://svn.boost.org/trac10/ticket/7813 <p> If you try the following program, the returned centroid is outside the polygon. This only happens with trivially small polygons. </p> <pre class="wiki">#include &lt;boost/geometry.hpp&gt; #include &lt;boost/geometry/geometries/point_xy.hpp&gt; #include &lt;boost/geometry/geometries/polygon.hpp&gt; #include &lt;iostream&gt; #include &lt;iomanip&gt; int main() { typedef boost::geometry::model::d2::point_xy&lt;double&gt; point_type; typedef boost::geometry::model::polygon&lt;point_type&gt; polygon_type; polygon_type p; boost::geometry::read_wkt("POLYGON((139.64436 35.856411,139.64436 35.856391,139.6444 35.856397,139.64439 35.856417,139.64436 35.856411))", p); boost::geometry::correct(p); point_type centroid; boost::geometry::centroid(p, centroid); std::cout &lt;&lt; std::fixed &lt;&lt; std::setprecision(6) &lt;&lt; boost::geometry::wkt(centroid) &lt;&lt; std::endl; return 0; } </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/7813 Trac 1.4.3 Barend Gehrels Wed, 19 Dec 2012 21:29:51 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/7813#comment:1 https://svn.boost.org/trac10/ticket/7813#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">wontfix</span> </li> </ul> <p> According to the docs: <em>"Note that the centroid might be located in a hole or outside a polygon, easily."</em> </p> <p> The centroid is the centre of mass, and can be inside or outside the polygon (also outside of larger polygons). </p> Ticket wuxiaqing2000@… Wed, 19 Dec 2012 21:49:19 GMT <link>https://svn.boost.org/trac10/ticket/7813#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7813#comment:2</guid> <description> <p> if you visualize the polygon and point, there is no way that the centroid computed is correct. How can a convex shape without hole have its centroid outside of itself? And it is far away from the shape itself. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Barend Gehrels</dc:creator> <pubDate>Wed, 19 Dec 2012 22:22:11 GMT</pubDate> <title>status changed; resolution deleted https://svn.boost.org/trac10/ticket/7813#comment:3 https://svn.boost.org/trac10/ticket/7813#comment:3 <ul> <li><strong>status</strong> <span class="trac-field-old">closed</span> → <span class="trac-field-new">reopened</span> </li> <li><strong>resolution</strong> <span class="trac-field-deleted">wontfix</span> </li> </ul> <p> I will look to the specific example. The original message was that it was outside, which might happen. Being far outside a convex polygon may not happen. </p> Ticket Barend Gehrels Sat, 22 Dec 2012 18:02:35 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/7813#comment:4 https://svn.boost.org/trac10/ticket/7813#comment:4 <ul> <li><strong>status</strong> <span class="trac-field-old">reopened</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">wontfix</span> </li> </ul> <p> I reproduced the problem. The polygon is indeed very small, near the limits of floating point resolution. The division (used for centroid) by a similar small value results in the centroid being outside the polygon. </p> <p> If you really want to use results for this resolution, please use ttmath_big. The problem is than not there. </p> <p> Summary: </p> <p> <em>centroid (double) is: POINT(139.6527006820726300 35.8585406437098440)</em> </p> <p> <em>centroid (ttmath) is: POINT(139.64437785388127853881278548429355447 35.85640358447488584474885847218077831)</em> </p> <p> <em>centroid (sql server) is: POINT (139.64437785388145 35.856403584474876)</em> </p> <p> So the double-version is indeed a bit off, and indeed outside the convex polygon. </p> Ticket