Something went wrong. Try again.
CMU Coding Bootcamp
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930def rectanglesOverlap( ax1: int, ay1: int, aw: float, ah: float, bx1: int, by1: int, bw: float, bh: float) -> bool: """Check if two rectangles overlap.""" ax2 = ax1 + aw ay2 = ay1 + ah bx2 = bx1 + bw by2 = by1 + bh return (ax1 <= bx2 and ax2 >= bx1) and (ay1 <= by2 and ay2 >= by1)
print("Testing rectanglesOverlap()...", end="")# Intersect at right of rectangle 1assert rectanglesOverlap(1, 1, 5, 1, 6, 1, 2, 2) == True# Intersect at top of rectangle 1assert rectanglesOverlap(1, 4, 5, 3, 1, 5, 8, 3) == True# Intersect at left of rectangle 1assert rectanglesOverlap(1, 5, 6, 6, -4, 7, 5, 3) == True# Intersect at bottom of rectanagle 1assert rectanglesOverlap(10, 10, 3, 3, 9, 7, 3, 3) == True# Partially overlapping rectanglesassert rectanglesOverlap(1, 7, 3, 6, 3, 4, 2, 5) == True# Don't intersectassert rectanglesOverlap(1, 4, 3, 3, 10, 10, 5, 5) == False# Don't intersect, but x-coordinates overlapassert rectanglesOverlap(1, 4, 30, 3, 10, 10, 5, 5) == False# Don't intersect, but y-coordinates overlapassert rectanglesOverlap(1, 4, 3, 15, 10, 10, 5, 5) == Falseprint("Passed!")