I think the problem is that your comparison functions make too strong an assumption about the shape of the bounding box. Consider these two points:
1 / / / 2
Correct bounding box
+---1 | /| | / | |/ | 2---+
Note that the corners of the bounding box are not points of your vector. Instead, they are points formed by combining coordinates from different points of the vector. Moreover, if you look at your two comparison functions, you will find that, given these two points, not one point is compared less or more than the other point, since each of them has one coordinate that is higher than the other, and lower than the other.
To get your bounding box, you must do the following:
- Find the point with the minimum x value.
- Find the point with the maximum value.
- Find the point with the minimum value.
- Find the point with the maximum value.
- Combine x and y from points with a minimum value of x and y into one corner point.
- Combine x and y from the points with the maximum value of x and y into one corner point.
You can do this using the new C ++ 11 std::minmax_element
, 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!
source share