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

  • Home
  • SEARCH
  • 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 3445116
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:00:06+00:00 2026-05-18T09:00:06+00:00

I have a theoretical grid of overlapping rectangles that might look something like this:

  • 0

I have a theoretical grid of overlapping rectangles that might look something like this:

grid layout

But all I have to work with is a collection of Rectangle objects:

var shapes = new List<Rectangle>();
shapes.Add(new Rectangle(10, 10, 580, 380));
shapes.Add(new Rectangle(15, 20, 555, 100));
shapes.Add(new Rectangle(35, 50, 40, 75));
// ...

What I’d like to do is build a DOM-like structure where each rectangle has a ChildRectangles property, which contains the rectangles that are contained within it on the grid.

The end result should allow me to convert such a structure into XML, something along the lines of:

<grid>
  <shape rectangle="10, 10, 580, 380">
    <shape rectangle="5, 10, 555, 100">
      <shape rectangle="20, 30, 40, 75"/>
    </shape>
  </shape>
  <shape rectangle="etc..."/>
</grid>

But it’s mainly that DOM-like structure in memory that I want, the output XML is just an example of how I might use such a structure.

The bit I’m stuck on is how to efficiently determine which rectangles belong in which.

NOTE No rectangles are partially contained within another, they’re always completely inside another.

EDIT There will typically be hundreds of rectangles, should I just iterate through every rectangle to see if it’s contained by another?

EDIT Someone has suggested Contains (not my finest moment, missing that!), but I’m not sure how best to build the DOM. For example, take the grandchild of the first rectangle, the parent does indeed contain the grandchild, but it shouldn’t be a direct child, it should be the child of the parent’s first child.

  • 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-18T09:00:07+00:00Added an answer on May 18, 2026 at 9:00 am

    As @BeemerGuy points out, Rect.Contains will tell you whether one rectangle contains another. Building the hierarchy is a bit more involved…

    There’s an O(N^2) solution in which for each rectangle you search the list of other rectangles to see if it fits inside. The “owner” of each rectangle is the smallest rectangle that contains it. Pseudocode:

    foreach (rect in rectangles)
    {
        owner = null
        foreach (possible_owner in rectangles)
        {
            if (possible_owner != rect)
            {
                if (possible_owner.contains(rect))
                {
                    if (owner === null || owner.Contains(possible_owner))
                    {
                        owner = possible_owner;
                    }
                }
            }
        }
        // at this point you can record that `owner` contains `rect`
    }
    

    It’s not terribly efficient, but it might be “fast enough” for your purposes. I’m pretty sure I’ve seen an O(n log n) solution (it is just a sorting operation, after all), but it was somewhat more complex.

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

Sidebar

Related Questions

Have you managed to get Aptana Studio debugging to work? I tried following this,
This is entirely theoretical at this point, but I've been trying to wrap my
This is more a theoretical question than a practical one, but given I undestand
This is a more theoretical question, not a specific scenario: Let's assume, we have
I have this question on and of for a year or two, but it
Lets say, that problems are fairly simple - something, that pre-degree theoretical physics student
I'm sorry... (premature) optimization is the root of all evil, but I'd like to
I have task to prepare two windows with swing. One contains grid of squares,
This is a lot more complicated than it might seem at first. What I
I have a theoretical question about the .net framework. As I see it Microsoft

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.