I have a set of rectangles in specified space, and they can have different dimensions and positions.
I need to detect collision clusters, group of rectangles that intersect each others, like in the attached figure, in which I have two clusters (into red boxes).
The trivial way is to have a vector of these rectangles and, with a double for
loop check intersections, like something
for (size_t i = 0; i < rectangles.size(); i++) {
for (size_t j = i + 1; j < rectangles.size(); j++) {
if (rectangles.at(i).intersect(rectangles.at(j)) {
// Add rectangle[j] to cluster in which rectangle[i] is
}
}
I don't think that is the most efficient way in order to perform calculus.
How can I efficiently calculate these clusters? I've read something about plane partition using quad-tree, but I don't know how use them in this case. Is it the appropriate data structure or there's a more efficient way (I must handle it in soft real-time).