Getting the bounding box of a vector of points?

前端 未结 3 804
遥遥无期
遥遥无期 2020-12-31 15:31

I have a vector of points stored in a std::vector instance. I want to calculate the bounding box of these points. I\'ve tried with this code:

bo         


        
相关标签:
3条回答
  • 2020-12-31 15:41

    Simply iterate over all elements and keep track of the current min/max. You can use boost::minmax to update both at the same time. Really no need to iterate twice over your dataset.

    0 讨论(0)
  • 2020-12-31 15:58

    I think the problem is that your comparison functions are making too strong an assumption about the shape of the bounding box. Consider these two points:

              1
             /
            /
           /
          2
    

    The correct bounding box is

          +---1
          |  /|
          | / |
          |/  |
          2---+
    

    Notice that the bounding box corners aren't actually points in your vector. Instead, they're points formed by combining coordinates from different points in the vector. Moreover, if you look at your two comparison functions, you'll find that given these two points, neither point compares less than or greater than the other point, since each one has one coordinate that is higher than the other and one that is lower than the other.

    To get your bounding box, you should do the following:

    1. Find the point with the min x value.
    2. Find the point with the max x value.
    3. Find the point with the min y value.
    4. Find the point with the max y value.
    5. Combine the x and y from the points with the min x and y value into one corner point.
    6. Combine the x and y from the points with the max x and y value into one corner point.

    You can do this using the new C++11 std::minmax_element algorithm, along with lambdas:

    auto xExtremes = std::minmax_element(v.begin(), v.end(),
                                         [](const ofPoint& lhs, const ofPoint& rhs) {
                                            return lhs.x < rhs.x;
                                         });
    
    auto yExtremes = std::minmax_element(v.begin(), v.end(),
                                         [](const ofPoint& lhs, const ofPoint& rhs) {
                                            return lhs.y < rhs.y;
                                         });
    
    ofPoint upperLeft(xExtremes.first->x, yExtremes.first->y);
    ofPoint lowerRight(xExtremes.second->x, yExtremes.second->y);
    

    Hope this helps!

    0 讨论(0)
  • 2020-12-31 16:04

    If you don't have c++11, you can use boost::algorithm::minmax_element.

    #include <boost/algorithm/minmax_element.hpp>
    bool compareX(ofPoint lhs, ofPoint rhs) { return lhs.x < rhs.x; };
    bool compareY(ofPoint lhs, ofPoint rhs) { return lhs.y < rhs.y; };
    
    // ....
        pair<vector<ofPoint>::iterator, vector<ofPoint>::iterator> xExtremes, yExtremes;
        xExtremes = boost::minmax_element(overlap_point.begin(), overlap_point.end(), compareX);
        yExtremes = boost::minmax_element(overlap_point.begin(), overlap_point.end(), compareY);
        ofPoint upperLeft(xExtremes.first->x, yExtremes.first->y);
        ofPoint lowerRight(xExtremes.second->x, yExtremes.second->y);
    
    0 讨论(0)
提交回复
热议问题