Source code for rectangle_overlap

"""This program checks whether two rectangles with coordinates given
in lists overlap or not. Instead of checking for overlap, it checks
when they do not overlap first and returns false if this so. If those
checks fail, then it returns true. 

"""

[docs]def rectangle_overlap(A, B): """ Assumes A, B are lists of the form [x1,y1,x2,y2] and checks if they are not overlapping, and returns false if so. Otherwise it returns true. A rectangle A=[x1,y1,x2,y2] has its lower left corner at (x1,y1) and upper right corner at (x2,y2). """ if (B[2] < A[0]) or (B[0]>A[2]): ##B.x2 < A.x1 or B.x1 > A.x2 return False elif (B[3]<A[1] or B[1]>A[3]): ## B.y2 < A.y1 or B.y1 > A.y2 return False return True
A = [0,0,10,10] B = [0,5,5,12] if rectangle_overlap(A,B): print "Rectangles", A,B, "overlap" else: print "Rectangles", A,B, "do not overlap."