PDA

View Full Version : [SOLVED] [Python] Check if two rectangles overlap?



crazyfuturamanoob
October 25th, 2008, 09:36 AM
How to check if two rectangles overlap, and perhaps return the overlapping area?

pp.
October 25th, 2008, 09:42 AM
In this kind of problem, the sides of the rectangles usually are strictly horizontal and vertical, respectively.

Also, rectangles usually are given by four magnitudes: x ordinates of the left and right side, y ordinates of the top and bottom.

Take a piece of paper and draw a few rectangles, keeping in mind what I just wrote.

crazyfuturamanoob
October 25th, 2008, 10:42 AM
Umm... I define my rectangles with top left corner and bottom right corner. Ok?
Both corners are tuples, presenting the x and y coords.

pp.
October 25th, 2008, 11:02 AM
Umm... I define my rectangles with top left corner and bottom right corner. Ok?
Both corners are tuples, presenting the x and y coords.

Yes. Hence you know the top, right, bottom and left edge, don't you?

crazyfuturamanoob
October 25th, 2008, 11:32 AM
Never mind I found a module for rects and points with google that I can use. Solved.

fiddler616
October 25th, 2008, 12:04 PM
I'd still think about what pp. said, in the interest of tight code.



A,B
---------------
| |
| A',B'|----|----|
| | | |
|--------|----| |
| C,D |
|---------|
C',D'

Compare A' with A and C, and B' with B and D.

pp.
October 25th, 2008, 03:17 PM
Solved.

:lolflag: