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:
bool _compare1(ofPoint const &p1, ofPoint const &p2) {
return p1.x < p2.x && p1.y < p2.y;
}
bool _compare4(ofPoint const &p1, ofPoint const &p2) {
return p1.x > p2.x && p1.y > p2.y;
}
vector<ofPoint> points;
// ...
if(points.size()>1) {
ofPoint p_min = *std::min_element(points.begin(), points.end(), &_compare1);
ofPoint p_max = *std::min_element(points.begin(), points.end(), &_compare4);
}
But this code produces weird results. In reality I'm interested only the first and last points of my bounding box:
1------2
|\ |
| \ |
| \ |
| \ |
| \ |
| \|
3------4
If my points represent the diagonal line I'm interested only in point 1 and 4.
Are there smart ways to get this with the standard libraries or Boost?
CURRENT SOLUTION:
bool _compare_min_x(ofPoint const &p1, ofPoint const &p2) { return p1.x < p2.x; }
bool _compare_min_y(ofPoint const &p1, ofPoint const &p2) { return p1.y < p2.y; }
// ....
if(points.size()>1) {
min_x = (*std::min_element(points.begin(), points.end(), &_compare_min_x)).x;
min_y = (*std::min_element(points.begin(), points.end(), &_compare_min_y)).y;
max_x = (*std::max_element(points.begin(), points.end(), &_compare_min_x)).x;
max_y = (*std::max_element(points.begin(), points.end(), &_compare_min_y)).y;
}