I have a list of rectangles (they cannot rotate):
struct Rectangle {
double centerX;
double centerY;
double width;
double height;
Rectangle(double x, double y, double w, double h):centerx(x),centery(y),width(w),height(h){}
};
std::vector<Rectangle> rectangles;
rectangles.push_back(Rectangle(0 , 0 , 1 , 1 );
rectangles.push_back(Rectangle(0.5, 0.5, 1 , 1 );
rectangles.push_back(Rectangle(3 , 2 , 0.5, 0.6);
rectangles.push_back(Rectangle(0.2, 0.5, 0.5, 0.6);
rectangles.push_back(Rectangle(2 , -0.3, 0.4, 0.4);
rectangles.push_back(Rectangle(0.2, -0.4, 0.3, 0.4);
I want to calculate a group of rectangles having at least one intersection with the transitivity property. those. if r1 intersect r2 and r2 intersect r3, r1, r2, r3 are part of the same group.
In this example, the group will be
{{3}, {4, 1, 2}, {5, 6}}
The group order and the order of the elements within the group are not important.
How can I calculate these groups? If necessary, I can change the data structure.
For example, I can change std::vectorto std::setand arrange the rectangles with the left X coordinate of the vertex. That way I can use the sweep algorithm. But I cannot find a pattern that can be applied to my problem.
How to get overlapping groups?
