197 template <
typename Container>
constexpr auto detect_overlap(
const Container& rectangles)
199 std::pair<typename Container::value_type, typename Container::value_type>> {
200 using RectT =
typename Container::value_type;
201 using T =
typename RectT::value_type;
203 if (rectangles.size() < 2) {
207 using Event = std::tuple<T, int, size_t>;
208 std::vector<Event> events;
211 for (
const auto& rect : rectangles) {
212 if (rect.xcoord().is_invalid() || rect.ycoord().is_invalid()) {
216 events.emplace_back(rect.xcoord().lb(), 1, idx);
217 events.emplace_back(rect.xcoord().ub(), -1, idx);
221 std::sort(events.begin(), events.end(),
222 [](
const Event& a,
const Event& b) { return std::get<0>(a) < std::get<0>(b); });
224 std::vector<std::pair<size_t, Interval<T>>> active;
226 for (
const auto& [x, event_type, rect_idx] : events) {
227 const auto& rect = rectangles[rect_idx];
229 if (event_type == 1) {
230 for (
const auto& [other_idx, other_y] : active) {
231 if (rect.ycoord().overlaps(other_y)) {
232 return std::make_optional(std::make_pair(rect, rectangles[other_idx]));
235 active.emplace_back(rect_idx, rect.ycoord());
237 for (
size_t i = 0; i < active.size(); ++i) {
238 if (active[i].first == rect_idx) {
240 active[i] = active.back();
constexpr auto detect_overlap(const Container &rectangles) -> std::optional< std::pair< typename Container::value_type, typename Container::value_type > >
Detect if any pair of rectangles overlap using the line sweep algorithm.
Definition recti.hpp:197