-
Notifications
You must be signed in to change notification settings - Fork 0
/
overlappingRectangles.py
222 lines (205 loc) · 6.03 KB
/
overlappingRectangles.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import unittest
def find_rectangular_overlap(rect1, rect2):
# Calculate the overlap between the two rectangles
left = {}
right = {}
#lapPossible = False
overlap = {
'left_x': None,
'bottom_y': None,
'width': None,
'height': None,
}
if rect1['left_x'] <=rect2['left_x']:
#rect1 is to left of rect 2
left = rect1
right = rect2
else:
#rect1 is to left of rect 2
left = rect2
right = rect1
if not(left['bottom_y'] < right['bottom_y']+right['height']) :#and right[bottom_y]+right[height] <= left[bottom_y]+left[height] :
# left is below right
#lapPossible = True
#else:
# right is below left
# and no intesection so return empty
return overlap
if left['left_x']+left['width'] >right['left_x'] : #and lapPossible :
overlap['left_x'] =right['left_x']
if left['left_x']+left['width'] >right['left_x']+right['width'] :
overlap['width'] = right['width'] # left is bigger than right
else:
overlap['width'] = left['left_x']+left['width'] - right['left_x']
# right is bigger than left
else:
#no overlap
return overlap
if left['bottom_y'] > right['bottom_y'] :
overlap['bottom_y']= left['bottom_y']
if left['bottom_y'] + left['height'] < right['bottom_y']+ right['height'] :
overlap['height']= left['height'] # left is smaller in height to right rectangle
else:
overlap['height'] = right['bottom_y']+ right['height'] - left['bottom_y']
else: #right rectangle is above left
overlap['bottom_y']= right['bottom_y']
if right['bottom_y']>= left['bottom_y']+left['height']: # left is below right and not touching
# no overlap
return {'left_x': None,'bottom_y': None,'width': None,'height': None,}
else:
if right['bottom_y'] + right['height'] < left['bottom_y']+ left['height'] :
overlap['height']= right['height'] # right is smaller in height to left rectangle
else:
overlap['height'] = left['bottom_y']+ left['height'] - right['bottom_y']
#overlap[height'] = left[bottom_y']+left['height'] - right['bottom']
return overlap
# Tests
class Test(unittest.TestCase):
def test_overlap_along_both_axes(self):
rect1 = {
'left_x': 1,
'bottom_y': 1,
'width': 6,
'height': 3,
}
rect2 = {
'left_x': 5,
'bottom_y': 2,
'width': 3,
'height': 6,
}
expected = {
'left_x': 5,
'bottom_y': 2,
'width': 2,
'height': 2,
}
actual = find_rectangular_overlap(rect1, rect2)
self.assertEqual(actual, expected)
def test_one_rectangle_inside_another(self):
rect1 = {
'left_x': 1,
'bottom_y': 1,
'width': 6,
'height': 6,
}
rect2 = {
'left_x': 3,
'bottom_y': 3,
'width': 2,
'height': 2,
}
expected = {
'left_x': 3,
'bottom_y': 3,
'width': 2,
'height': 2,
}
actual = find_rectangular_overlap(rect1, rect2)
self.assertEqual(actual, expected)
def test_both_rectangles_the_same(self):
rect1 = {
'left_x': 2,
'bottom_y': 2,
'width': 4,
'height': 4,
}
rect2 = {
'left_x': 2,
'bottom_y': 2,
'width': 4,
'height': 4,
}
expected = {
'left_x': 2,
'bottom_y': 2,
'width': 4,
'height': 4,
}
actual = find_rectangular_overlap(rect1, rect2)
self.assertEqual(actual, expected)
def test_touch_on_horizontal_edge(self):
rect1 = {
'left_x': 1,
'bottom_y': 2,
'width': 3,
'height': 4,
}
rect2 = {
'left_x': 2,
'bottom_y': 6,
'width': 2,
'height': 2,
}
expected = {
'left_x': None,
'bottom_y': None,
'width': None,
'height': None,
}
actual = find_rectangular_overlap(rect1, rect2)
self.assertEqual(actual, expected)
def test_touch_on_vertical_edge(self):
rect1 = {
'left_x': 1,
'bottom_y': 2,
'width': 3,
'height': 4,
}
rect2 = {
'left_x': 4,
'bottom_y': 3,
'width': 2,
'height': 2,
}
expected = {
'left_x': None,
'bottom_y': None,
'width': None,
'height': None,
}
actual = find_rectangular_overlap(rect1, rect2)
self.assertEqual(actual, expected)
def test_touch_at_a_corner(self):
rect1 = {
'left_x': 1,
'bottom_y': 1,
'width': 2,
'height': 2,
}
rect2 = {
'left_x': 3,
'bottom_y': 3,
'width': 2,
'height': 2,
}
expected = {
'left_x': None,
'bottom_y': None,
'width': None,
'height': None,
}
actual = find_rectangular_overlap(rect1, rect2)
self.assertEqual(actual, expected)
def test_no_overlap(self):
rect1 = {
'left_x': 1,
'bottom_y': 1,
'width': 2,
'height': 2,
}
rect2 = {
'left_x': 4,
'bottom_y': 6,
'width': 3,
'height': 6,
}
expected = {
'left_x': None,
'bottom_y': None,
'width': None,
'height': None,
}
actual = find_rectangular_overlap(rect1, rect2)
self.assertEqual(actual, expected)
unittest.main(verbosity=2)