Getting the bounding box of a vector of points?

前端 未结 3 829
遥遥无期
遥遥无期 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 16:04

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

    #include 
    bool compareX(ofPoint lhs, ofPoint rhs) { return lhs.x < rhs.x; };
    bool compareY(ofPoint lhs, ofPoint rhs) { return lhs.y < rhs.y; };
    
    // ....
        pair::iterator, vector::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);
    

提交回复
热议问题