Opened 10 years ago

Closed 10 years ago

#7813 closed Bugs (wontfix)

Bug in boost::geometry::centroid with trivially small polygons

Reported by: anonymous Owned by: Barend Gehrels
Milestone: To Be Determined Component: geometry
Version: Boost 1.52.0 Severity: Problem
Keywords: Cc:

Description

If you try the following program, the returned centroid is outside the polygon. This only happens with trivially small polygons.

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <iostream>
#include <iomanip>

int main()
{
    typedef boost::geometry::model::d2::point_xy<double> point_type;
    typedef boost::geometry::model::polygon<point_type> 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 << std::fixed << std::setprecision(6) << boost::geometry::wkt(centroid) << std::endl;

    return 0;
}

Change History (4)

comment:1 by Barend Gehrels, 10 years ago

Resolution: wontfix
Status: newclosed

According to the docs: "Note that the centroid might be located in a hole or outside a polygon, easily."

The centroid is the centre of mass, and can be inside or outside the polygon (also outside of larger polygons).

comment:2 by wuxiaqing2000@…, 10 years ago

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.

comment:3 by Barend Gehrels, 10 years ago

Resolution: wontfix
Status: closedreopened

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.

comment:4 by Barend Gehrels, 10 years ago

Resolution: wontfix
Status: reopenedclosed

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.

If you really want to use results for this resolution, please use ttmath_big. The problem is than not there.

Summary:

centroid (double) is: POINT(139.6527006820726300 35.8585406437098440)

centroid (ttmath) is: POINT(139.64437785388127853881278548429355447 35.85640358447488584474885847218077831)

centroid (sql server) is: POINT (139.64437785388145 35.856403584474876)

So the double-version is indeed a bit off, and indeed outside the convex polygon.

Last edited 10 years ago by Barend Gehrels (previous) (diff)
Note: See TracTickets for help on using tickets.