Created
May 27, 2016 07:42
-
-
Save espdev/94b4a1ef9c75da42fb6b0573e8b1c8d3 to your computer and use it in GitHub Desktop.
Rects overlapping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Rects overlapping | |
A rect representation: | |
rect = [x1, y1, x2, y2] | |
""" | |
def range_overlap(a_min, a_max, b_min, b_max): | |
return not ((a_min > b_max) or (a_max < b_min)) | |
def overlap_rects(rect1, rect2): | |
r1_x1, r1_y1, r1_x2, r1_y2 = rect1 | |
r2_x1, r2_y1, r2_x2, r2_y2 = rect2 | |
if (range_overlap(r1_x1, r1_x2, r2_x1, r2_x2) and | |
range_overlap(r1_y1, r1_y2, r2_y1, r2_y2)): | |
c_x1 = max(r1_x1, r2_x1) | |
c_y1 = max(r1_y1, r2_y1) | |
c_x2 = min(r1_x2, r2_x2) | |
c_y2 = min(r1_y2, r2_y2) | |
return [c_x1, c_y1, c_x2, c_y2] | |
return None | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment