Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine if one rectangle is completely contained within another?

Tags:

c#

.net

geometry

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.

like image 613
Matt Brindley Avatar asked Nov 24 '10 19:11

Matt Brindley


People also ask

How will you determine if a rectangle is contained within another?

Well, by definition one rectangle is inside of another if all the points of the inner rectangle are within the outer rectangle. Using a bit of geometry you can boil it down to checking whether the two opposite corners of the inner rectangle are in the outer rectangle.

How do you check if a point is in a rectangle?

If a point p = (x, y) lies inside the rectangle, then the dot product (p - p1). (p2 - p1) will lie between 0 and |p2 - p1|^2, and (p - p1).


2 Answers

Use the Contains() of a Rectangle.

Rectangle rect1, rect2;
// initialize them
if(rect1.Continas(rect2))
{
    // do...
}

UPDATE:
For future reference...
It's interesting to add that Rectangle also has IntersectsWith(Rectangle rect) in case you want to check if a rectangle partially collides with another rectangle.

like image 136
BeemerGuy Avatar answered Oct 19 '22 17:10

BeemerGuy


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.

like image 41
Jim Mischel Avatar answered Oct 19 '22 18:10

Jim Mischel