Question

I'm coding a function giving the diameter of a previously defined polygon using the boost::geometry library.

This diameter is defined as the maximal distance between two of its points. Thus, I'm coding a double loop computing every distance of every pair of points but I don't know how to have access to the point coordinates inside the polygon, or even the point objects and then using the distance function between two points brought by the library (by the way, which one should be faster?).

After looking at Boost docs on polygon I tried my_polygon.PointList without success...

My solution at the end was to use a modified version of Barend's proposal:

for(auto it1 = boost::begin(boost::geometry::exterior_ring(poly)); 
    it1 != boost::end(boost::geometry::exterior_ring(poly));
    ++it1)
{
    for(auto it2 = it1+1; 
        it2 != boost::end(boost::geometry::exterior_ring(poly));
        ++it2)
   {
       double distance = boost::geometry::distance(*it1, *it2);
       if(my_diameter < distance){my_diameter = distance;}
   }
}

I just suppressed the redundancy of calculating twice the same distance;

Was it helpful?

Solution

A polygon consists of rings. You want to have the exterior ring (outer ring). That is accessable using exterior_ring(aPolygon);

So you could use something like this code to iterate over the points of a polygon (for simplicity I use auto, otherwise declare an iterator):

for(auto it1 = boost::begin(boost::geometry::exterior_ring(poly)); 
    it1 != boost::end(boost::geometry::exterior_ring(poly));
    ++it1)
{
    for(auto it2 = boost::begin(boost::geometry::exterior_ring(poly)); 
        it2 != boost::end(boost::geometry::exterior_ring(poly));
        ++it2)
   {
       // You might skip cases where it1==it2, distance is zero anyway
       double distance = boost::geometry::distance(*it1, *it2);
       // Compare with a max distance, if larger, assign, etc.
   }
}

(loops often also loop over interior rings, but for diameter that is not necessary if the polygon is well-defined).

By the way, about your question: PointList is the name of the template-parameter (see doc). The member function is outer(), for the outer ring. The code above uses a free function "exterior_ring" to use the Polygon Concept, which should work for any polygon type within Boost.Geometry

OTHER TIPS

You can check only the "corners" distance to one another as the maximal distance will be between two "corners".

think about it with just one point "A" and one segment. You'll see that no matter how you place the segment, the point of the segment with maximal distance to A, will be one of the two ends.

by the way, every points ? at what granularity? there are infinitely many of them!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top