-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
51 lines (39 loc) · 1.64 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Point:
def __init__(self, x, y):
"""
Point on a 2D cartesian plane
:param x: X-coordinate
:param y: Y-coordinate
"""
self.x = x
self.y = y
class Rectangle:
def __init__(self, top_left, bottom_right):
"""
Rectangle created using top left and bottom right points (Left to Right Diagonal)
:param top_left: Point on top left corner of rectangle
:param bottom_right: Point on bottom right corner of rectangle
"""
self.top_left = top_left
self.bottom_right = bottom_right
if self.top_left.x > self.bottom_right.x or self.top_left.y < self.bottom_right.y:
raise ValueError('Invalid coordinates for rectangle')
def does_intersect(self, rectangle):
"""
Check if a rectangle intersects with another
:param rectangle: The other rectangle
:return: If rectangles intersect
"""
# Check if they intersect on X-Axis
if self.top_left.x > rectangle.bottom_right.x or rectangle.top_left.x > self.bottom_right.x:
return False
# Check if they intersect on Y-Axis
if self.top_left.y < rectangle.bottom_right.y or rectangle.top_left.y < self.bottom_right.y:
return False
# Check if 1st rectangle is inside the 2nd
if self.top_left.x < rectangle.top_left.x and rectangle.bottom_right.x < self.bottom_right.x:
return False
# Check if 2nd rectangle is inside the 1st
if rectangle.top_left.x < self.top_left.x and self.bottom_right.x < rectangle.bottom_right.x:
return False
return True