Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm to take the union of rectangles and to see if the union is still a rectangle

I have a problem in which I have to test whether the union of given set of rectangles forms a rectangle or not. I don't have much experience solving computational geometry problems. What my approach to the problem was that since I know the coordinates of all the rectangles, I can easily sort the points and then deduce the corner points of the largest rectangle possible. Then I could sweep a line and see if all the points on the line falls inside the rectangle. But, this approach is flawed and this would fail because the union may be in the form of a 'U'. I would be a great help if you could push me in the right direction.

like image 757
praveen Avatar asked Feb 01 '12 07:02

praveen


People also ask

How do you find the union of a rectangle?

Sweep a vertical line from left to right over the rectangles. Divide the y axis into elementary y-intervals by considering all y coordinates from the input. Maintain a counter for each y-interval, which keeps track of how many rectangles currently cover this interval on the sweep line.

How do you find the intersection of two rectangles?

In order to check that two rectangles intersect all that's needed is x1 < x4 && x3 < x2 && y1 < y4 && y3 < y2 . That's it. (I used strict inequalities to exclude touching rectangles.)

How do you check if two rectangles intersect in Python?

Practical Data Science using Python If we have two (axis-aligned) rectangles, we have to check whether they overlap or not. So, if the input is like R1 = [0,0,2,2], R2 = [1,1,3,3], then the output will be True. otherwise, return True.

Does rectangle overlap?

Two rectangles overlap if the area of their intersection is positive. To be clear, two rectangles that only touch at the corner or edges do not overlap. Given two axis-aligned rectangles rec1 and rec2 , return true if they overlap, otherwise return false .


1 Answers

Your own version does not take into account that the edges of the rectangles can be non-parallel to each other. Therefore, there might not be "largest rectangle possible".

I would try this general approach:

1) Find the convex hull. You can find convex hull calculation algorithms here http://en.wikipedia.org/wiki/Convex_hull_algorithms.

2) Check if the convex hull is a rectangle. You can do this by looping through all the points on convex hull and checking if they all form 180 or 90 degree angles. If they do not, union is not a rectangle.

3) Go through all points on the convex hull. For each point check if the middle point between ThisPoint and NextPoint lies on the edge of any initially given rectangle. If every middle point does, union is a rectangle. If it does not, union is not a rectangle.

Complexity would be O(n log h) for finding convex hull, O(h) for the second part and O(h*n) for third part, where h is number of points on the convex hull.

Edit: If the goal is to check if the resulting object is a filled rectangle, not only edges and corners rectangle then add step (4).

4) Find all line segments that are formed by intersecting or touching rectangles. Note - by definition all of these line segments are segments of edges of given rectangles. If a rectangle does not touch/intersect other rectangles, the line segments are it's edges.

For each line segment check if it's middle point is

  • On the edge of the convex hull
  • Inside one of given rectangles
  • On the edge of two non-overlapping given rectangles.

If at least one of these is true for every line segment, resulting object is a filled rectangle.

like image 92
jva Avatar answered Nov 13 '22 22:11

jva