Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6919325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:58:32+00:00 2026-05-27T09:58:32+00:00

Best way to group overlapping rectangles? I’ve tried using OpenCV but the grouprectangles method

  • 0

Best way to group overlapping rectangles? I’ve tried using OpenCV but the grouprectangles method doesn’t work as intended.

I’ve thought about doing something like this:

L = [every rectangle]
L_next = []
while not L.empty():
    for rectangle in L:
        L.remove(rectangle)
        for other_rectangle in L:
            if rectangle overlaps with other_rectangle:
                L_next += rectangle + other_rectangle
    L = L_next
    L_next = []

Since every rectangle that isn’t merged would be dropped from the next list, in the worst case scenario, I’d have n/2 iterations of the outer loop. The two inner loops should execute n and n - 1 times, so that algorithm should be roughly O(n^3) in a worst case scenario, assuming I didn’t miss anything and that each step only takes O(1).

Problems:

1) Need to use Equivalence Classes or something to that extent to merge the groups of rectangles correctly. Does Boost have anything like that?

2) This seems like the kind of operation that would have to be done frequently, so I’m surprised about not finding more material on it. What’s up with that?

3) Assuming there really is not something already implemented to do this, does anyone have some advice to improve my method?

4) What is the best way to see if two rectangles overlap?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:58:32+00:00Added an answer on May 27, 2026 at 9:58 am

    I’d look at pygame.Rect.collide methods for a response to your problem. Since rectangle overlapping detection is so common in games, I guess their implementation is quite good in terms of computational complexity.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Whats the best way to add a user/group in linux using C++ is there
I'm wondering what would be the best way to group contacts by their company.
What is the best way to group and still maintain order with in groups?
I'm retrieving a group of customers and orders from sql, what's the best way
What is the best way to implement group/batch execution within a single web services
I'm looking the best way to create a group of four buttons. Each button
What is the best way to solve this: I have a group of arrays
I was wondering what is the best way to get a group of names
What is the best way to password protect folder using php without a database
What is the best way to group records together by one field, and get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.