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
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:
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!