I have an std::vector
of rectangle
's. The class definition is quite simple:
class rectangle
{
public:
rectangle();
rectangle(int leftX, int topY, int width, int height);
rectangle(const rectangle &other);
int x() const;
int y() const;
int width() const;
int height() const;
// Returns the bounding rectangle of this rectangle and the given rectangle.
rectangle united(const rectangle &r) const;
rectangle intersected(const rectangle &r) const;
// Returns true if rectangle intersects with given rectagle else false.
bool intersects(const rectangle &r) const;
};
For each rectangle, I'd like to see if intersects another rectangle in the vector and then 'unite' them (find the bounding rectangle) if they intersect.
I'm quite keen to see if there is a way to use a function in <algorithm>
to perform the search/combining on a range of rectangles. Can anyone advise on a possible solution? Looking for something that is concise without reinventing the wheel.
[EDIT:] I should mention that I have already implemented 'intersects()' and 'united'.
My end goal is to implement a function that works on my range of rectangles like so:
/// For each rectangle in the vector it test if it intersects with another and return the set of united rectangles.
/// \param v A set of rectangles
/// \return A united set of rectangles.
std::vector<rectangle> test_intersect_and_unite(const std::vector<rectangle> &v)
{
std::vector<rectangle> united;
// ...
return united;
}
I'd probably be handling less than 20 rectangles.