Asked • 03/26/19

Difference (XOR) between two rectangles, as rectangles?

I'm looking for a simple way to calculate the difference between two rectangles. I mean all points which belong to one of the rectangles, but not to both (so it's like XOR). The rectangles are axis-aligned in this case, so there will be only right angles. I believe the difference region can be expressed in 0-4 rectangles (0 if both rectangles are the same, 1 if just one edge is different, 4 in the general case), and I'd like to get the difference region as a list of rectangles. You can also think of it as the areas of the screen that have to be updated when a solid rectangle is moved/resized. Examples: Doubling the width of rectangle "a" - I want the added region (R). +----+----+ | a | R | | | | +----+----+ Intersecting rectangles (a and b) - I want the area given by T, L, R and B in rectangles (other partitioning possible), but excluding X: +------------+ a | T | |·····+------+-----+ b | L | X | R | | | | | +-----+------+·····| | B | +------------+ I'd prefer a python solution/library, but any robust algorithm would be helpful.

1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.