Click here to Skip to main content
15,888,003 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
My situation

Input: a set of rectangles
each rect is comprised of 4 doubles like this: (x0,y0,w,h)
they are not "rotated" at any angle, all they are "normal" rectangles that go "up/down" and "left/right" with respect to the screen
they are randomly placed - they may be touching at the edges, overlapping , or not have any contact
I will have several hundred rectangles
this is implemented in C

I need to find

union of area A and B if overlapped

Example

The image below contains two rectangles: A,B
A and B overlap
What I am looking for is the total area of two region

<br />
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA <br />
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA <br />
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA <br />
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA <br />
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA <br />
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA <br />
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA <br />
AAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBB<br />
AAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBB <br />
AAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBB <br />
AAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBB <br />
AAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBB<br />
                             <br />
Posted

I like the problem, it is pretty funny purely algorithmic problem, not very much of programming. But you are supposed to be the author of the solution, which looks like a home assignment. Therefore, let's do this: I though just a bit at it and came to a pretty interesting simple idea. I'll share this idea with you, and you will try to continue.

Here is how it looks: your explanation and the pictures provokes the idea to solve the trivial problem first: for a pair of given rectangles, calculate the area of intersection (or can be zero or not) and subtract this area from the sum of rectangle's areas. But generalization not only cause problems, but looks very inefficient from the very beginning: the number of pairs grows as N2, where, let's say, N is the number of given rectangles.

Here is my idea:

First, consider just the left and right sides of rectangle. Draw infinite vertical straight lines where the sides are. You will get 2*N vertical lines which break the whole 2D space into 2*N + 1 vertical strips (leftmost and rightmost ones being semi-infinite, not intersecting with any of the rectangles). Excluding those semi-infinite strips, we get 2*N - 1 vertical strips.

Same way, considering top and bottom sides of out rectangles, we can break the whole space into 2*N - 1 horizontal strips. Both sets of strips will make the (2*N - 1)2 matrix of cells composed by these infinite vertical and horizontal straight lines build as continuations of all vertical and horizontal sides of all rectangles. Why this matrix is very useful to us? Those cells, unlike the initial rectangles, don't intersect!

Now, you can quickly calculate the total are of this matrix. Now, your problem is reduced to sorting those cells. Some of those cells "contain" the "matter" of one or more rectangles, others are "empty". You need to find out empty cells and subtract their areas from the area of the matrix. This problem is much easier than your initial problem.

Actually, I reduced initial problem to a very trivial one which anyone can easily solve.

—SA
 
Share this answer
 
v3
Comments
Matt T Heffron 3-Mar-15 13:18pm    
+5
Sergey Alexandrovich Kryukov 3-Mar-15 13:53pm    
Thank you, Matt.
—SA
Hi,

Have a look at the examples and the discussion going here:
http://stackoverflow.com/questions/244452/what-is-an-efficient-algorithm-to-find-area-of-overlapping-rectangles[^]

Hope this helps !! :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900