I'm currently learning about classes and I'm stuck on this problem that requires me to find if two rectangles overlap. Here is my code:
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def overlap(l1, r1, l2, r2):
if r1.x <= l1.x or l1.y <= r1.y or r2.x <= l2.x or l2.y <= r2.y:
print("Invalid")
elif l1.x >= r2.x or l2.x >= r1.x:
print("Does not intersect")
elif l1.y <= r2.y or l2.y <= r1.y:
print("Does not intersect")
elif l1.x == l2.x and l1. y == l2.y and r1.x == r2.x and r1.y == r2.y:
print("They're the same")
else:
print("Intersects")
This is the code to find whether the two rectangles overlap or not. But that's not really my problem, my problem is when I input the variables, the output is wrong. When I do it like this:
l1 = Point(0, 5)
r1 = Point(3, 10)
l2 = Point(0, 6)
r2 = Point(3, 2)
overlap(l1, r1, l2, r2)
>>> Invalid
The answer is correct. But when I input the same variables like this:
l1 = Point(input(), input())
r1 = Point(input(), input())
l2 = Point(input(), input())
r2 = Point(input(), input())
overlap(l1, r1, l2, r2)
>>> Intersects
The answer is different. I don't know what's wrong. Can anyone help me figure out how to correct this code? And if there are suggestions to improve my code to find whether the two rectangles overlap, please tell me. Thank you.