-
Notifications
You must be signed in to change notification settings - Fork 66
/
any_disks_intersect.py
304 lines (274 loc) · 8.24 KB
/
any_disks_intersect.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/env python
# we use 0 to mean red, 1 to mean black
from tree import Node, Tree
from heap import MaxHeap
def comparable(a, b):
"""Given two disks a and b that are comparable at x, determine whether a is above b or not."""
a_y = a[0][1]
b_y = b[0][1]
return a_y >= b_y
class disk(tuple):
def __init__(self, d):
super(disk, self).__init__(d)
self.pointer = None
class point(list):
def __init__(self, info, disk):
super(point, self).__init__(info)
self.disk = disk
class rb_node(Node):
def __init__(self, key, p, left, right, color):
Node.__init__(self, key, p, left, right)
self.color = color
if key is not None:
key.pointer = self
def minimum(self, nil):
x = self
y = x
while x != nil:
y = x
x = x.left
return y
def maximum(self, nil):
x = self
while x.right != nil:
x = x.right
return x
class rb_tree(Tree):
nil = rb_node(None, None, None, None, 1)
root = nil
def __init__(self):
pass
def above(self, s):
x = s.pointer
if x.right != self.nil:
return x.right.minimum(self.nil).key
else:
while x.p != self.nil and x.p.right == x:
x = x.p
return x.p.key
def below(self, s):
x = s.pointer
if x.left != self.nil:
return x.left.maximum(self.nil).key
else:
while x.p != self.nil and x.p.left == x:
x = x.p
return x.p.key
def minimum(self):
return self.root.minimum(self.nil)
def __getitem__(self, key):
return self.iterative_tree_search(key)
def left_rotate(self, x):
y = x.right
x.right = y.left
if y.left != self.nil:
y.left.p = x
y.p = x.p
if x.p == self.nil:
self.root = y
elif x.p.left == x:
x.p.left = y
else:
x.p.right = y
y.left = x
x.p = y
def right_rotate(self, y):
x = y.left
y.left = x.right
if x.right != self.nil:
x.right.p = y
x.p = y.p
if y.p == self.nil:
self.root = x
elif y.p.right == y:
y.p.right = x
else:
y.p.left = x
x.right = y
y.p = x
def insert(self, z):
""" the disk z will only be inserted when the left endpoint of z is being processed"""
# this is the x_coordinate of the left endpoint of z
x_coordinate = z[0][0]
z = rb_node(z, None, None, None, 0)
y = self.nil
x = self.root
while x != self.nil:
y = x
if comparable(z.key, x.key):
x = x.right
else:
x = x.left
if y == self.nil:
self.root = z
elif comparable(z.key, y.key):
y.right = z
else:
y.left = z
z.p = y
z.left = self.nil
z.right = self.nil
z.color = 0 # red
self.insert_fixed(z)
def insert_fixed(self, z):
while z.p.color == 0:
if z.p.p.left == z.p:
y = z.p.p.right
if y.color == 0:
z.p.color = 1
y.color = 1
z.p.p.color = 0
z = z.p.p
else:
if z.p.right == z:
z = z.p
self.left_rotate(z)
z = z.p.p
self.right_rotate(z)
z.color = 0
z.p.color = 1
else:
y = z.p.p.left
if y.color == 0:
z.p.color = 1
y.color = 1
z.p.p.color = 0
z = z.p.p
else:
if z.p.left == z:
z = z.p
self.right_rotate(z)
z = z.p.p
self.left_rotate(z)
z.color = 0
z.p.color = 1
self.root.color = 1
def transplant(self, u, v):
if u.p == self.nil:
self.root = v
elif u.p.left == u:
u.p.left = v
else:
u.p.right = v
v.p = u.p
def delete(self, z):
z = z.pointer
y = z
y_original_color = y.color
if z.left == self.nil:
x = z.right
self.transplant(z, z.right)
elif z.right == self.nil:
x = z.left
self.transplant(z, z.left)
else:
y = z.right.minimum(self.nil)
y_original_color = y.color
x = y.right
if y.p == z:
x.p = y
else:
self.transplant(y, y.right)
y.right = z.right
y.right.p = y
self.transplant(z, y)
y.left = z.left
y.left.p = y
y.color = z.color
if y_original_color == 1:
self.delete_fixup(x)
def delete_fixup(self, x):
while x != self.root and x.color == 1:
if x == x.p.left:
w = x.p.right
if w.color == 0:
w.color = 1
x.p.color = 0
self.left_rotate(x.p)
w = x.p.right
if w.left.color == 1 and w.right.color == 1:
w.color = 0
x = x.p
else:
if w.left.color == 0 and w.right.color == 1:
w.left.color = 1
w.color = 0
self.right_rotate(w)
w = x.p.right
w.color = x.p.color
x.p.color = 1
w.right.color = 1
self.left_rotate(x.p)
x = self.root
else:
w = x.p.left
if w.color == 0:
w.color = 1
x.p.color = 0
self.right_rotate(x.p)
w = x.p.left
if w.left.color == 1 and w.right.color == 1:
w.color = 0
x = x.p
else:
if w.left.color == 1 and w.right.color == 0:
w.right.color = 1
w.color = 0
self.left_rotate(w)
w = x.p.left
w.color = x.p.color
w.left.color = 1
x.p.color = 1
self.right_rotate(x.p)
x = self.root
x.color = 1
def any_disks_intersect(S):
"""
This algorithm takes as input a set S of n disks represented by its center point and radius, returning the boolean value TRUE if any pair of disks in S intersects, and FALSE otherwise.
:param S:
:return:
"""
T = rb_tree()
point_list = []
disk_list = []
for s in S:
disk_list.append(disk(s))
for s in disk_list:
center_point = s[0]
radius = s[1]
x = center_point[0]
y = center_point[1]
point_list.append(point([x - radius, 0, y], s))
point_list.append(point([x + radius, 1, y], s))
heap_point = MaxHeap(point_list)
heap_point.heapsort()
print(heap_point)
for p in heap_point:
if p[1] == 0:
s = p.disk
T.insert(s)
a = T.above(s)
b = T.below(s)
print("insert: ", a, b)
if (a is not None and disks_intersect(a, s)) or (b is not None and disks_intersect(b, s)):
return True
if p[1] == 1:
s = p.disk
a = T.above(s)
b = T.below(s)
if a is not None and b is not None and disks_intersect(a, b):
return True
T.delete(s)
return False
def disks_intersect(a, b):
print(a)
print(b)
a_x = a[0][0]
a_y = a[0][1]
a_r = a[1]
b_x = b[0][0]
b_y = b[0][1]
b_r = b[1]
print((a_x - b_x) ** 2 + (a_y - b_y) ** 2)
print((a_r + b_r) ** 2)
return ((a_x - b_x) ** 2 + (a_y - b_y) ** 2) <= ((a_r + b_r) ** 2)