Garret Ulrich - 1 year ago 81
Python Question

# Checking whether two rectangles overlap in python using two bottom left corners and top right corners

``````class Point:

def __init__(self, xcoord=0, ycoord=0):
self.x = xcoord
self.y = ycoord

class Rectangle:
def __init__(self, bottom_left, top_right, colour):
self.bottom_left = bottom_left
self.top_right = top_right
self.colour = colour

def intersects(self, other):
``````

I am trying to see if two triangles intersect based on the upper right and lower left corners however when I make the function:

``````def intersects(self, other):
return self.top_right.x>=other.top_right.x>=self.bottom_left.x and self.top_right.x>=other.bottom_left.x>=self.bottom_left.x and self.top_right.y>=other.top_right.y>=self.bottom_left.y and self.top_right.x>=other.bottom_left.x>=self.bottom_left.x
``````

The function will return false when inputting:

``````r1=Rectangle(Point(1,1), Point(2,2), 'blue')
r3=Rectangle(Point(1.5,0), Point(1.7,3), 'red')
r1.intersects(r3)
``````

into the shell.

``````def intersects(self, other):