-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskyline.py
376 lines (311 loc) · 9.88 KB
/
skyline.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import matplotlib.pyplot as plt
import datetime
import random
class Skyline:
def __init__(self, id, xmin, height, xmax):
b1 = (xmin, height, xmax)
bl = [b1]
self.id = id
self.bl = bl # bl = buildings list
# ##########SETTERS########### #
def setID(self, id2):
self.id = id2
# ##########GETTERS########### #
# returns the skyline's buildings list
def getBuildingsList(self):
return self.bl
# returns the skyline's id
def getID(self):
return self.id
# retunrs the slyline's area
def getArea(self):
self.noOverlapping()
suma = 0
for k in self.bl:
s = self.buildingArea(k)
suma += s
return suma
# returns the skyline's height
def getHeight(self):
maxi = 0
for k in self.bl:
if k[1] > maxi:
maxi = k[1]
return maxi
# ########OPERATIONS########## #
# add a building to the current skyline
def addBuilding(self, xmin, height, xmax):
self.bl += [(xmin, height, xmax)]
# returns the union of two skylines
def addSkyline(self, skyobj):
l1 = (self.bl).copy()
l2 = skyobj.getBuildingsList()
l3 = l1+l2
elem = l3[0]
l3.pop(0)
s = Skyline('null', elem[0], elem[1], elem[2])
for k in l3:
s.addBuilding(k[0], k[1], k[2])
s.noOverlapping()
return s
# return the intersection between skyobj and current skyline
def intersecSkyline(self, skyobj):
bl1 = (self.bl).copy()
bl2 = skyobj.getBuildingsList()
lim11 = bl1[0][0]
lim12 = bl1[-1][2]
lim21 = bl2[0][0]
lim22 = bl2[-1][2]
limLeft = max(lim11, lim21)
limRight = min(lim12, lim22)
edges1 = []
edges2 = []
edges1.extend([building[0], building[2]] for building in bl1)
edges1 = (sum(edges1, []))
edges2.extend([building[0], building[2]] for building in bl2)
edges2 = (sum(edges2, []))
edaux = edges1 + edges2
edaux = sorted(edaux)
edges = [edg for edg in edaux if(edg >= limLeft and edg <= limRight)]
edges = set(edges)
edges = list(edges)
current = 0
points = []
buildings1bis = [b for b in bl1 if((b[0] > limLeft and b[0] < limRight) or (b[2] > limLeft and b[2] < limRight) or (b[0] <= limLeft and b[2] >= limRight))]
buildings2bis = [b for b in bl2 if((b[0] > limLeft and b[0] < limRight) or (b[2] > limLeft and b[2] < limRight) or (b[0] <= limLeft and b[2] >= limRight))]
buildings = buildings1bis + buildings2bis
for i in edges:
active = []
active.extend(building for building in buildings if ((building[0] <= i and building[2] > i) or (building[0] < i and building[2] > i)))
if len(active) <= 1:
current = 0
points.append((i, 0))
continue
min_h = min(building[1] for building in active)
min_ed = i
if min_h != current:
current = min_h
points.append((i, min_h))
last = edges[-1]
points.append((last, 0))
l = []
for index, item in enumerate(points):
if index < len(points)-1:
a = item[0]
b = item[1]
elem = points[index+1]
c = elem[0]
if b != 0:
l.append((a, b, c))
elem = l[0]
l.pop(0)
s = Skyline('null', elem[0], elem[1], elem[2])
for k in l:
s.addBuilding(k[0], k[1], k[2])
lr = s.getBuildingsList()
return s
# returns the refelction of the current skyline
def mirrorSkyline(self):
l = (self.bl).copy()
l2 = []
w = 0
minx = l[0][0]
maxx = l[0][2]
for elem in l:
if elem[0] < minx:
minx = elem[0]
if elem[2] > maxx:
maxx = elem[2]
center = (minx+maxx)/2
for elem in l:
# edificio a la izquierda del centro
if (elem[0] < center) and (elem[2] <= center):
dist0 = center - elem[0]
pos2 = center + dist0
dist2 = center - elem[2]
pos0 = center + dist2
pos1 = elem[1]
l2.append((pos0, pos1, pos2))
# edificio a la derecha del centro
if (elem[0] >= center) and (elem[2] > center):
dist0 = elem[0] - center
pos2 = center - dist0
dist2 = elem[2] - center
pos0 = center - dist2
pos1 = elem[1]
l2.append((pos0, pos1, pos2))
# edificio a la derecha e izquierda del centro
if (elem[0] < center) and (elem[2] > center):
dist0 = center - elem[0]
pos2 = center + dist0
dist2 = elem[2] - center
pos0 = center - dist2
pos1 = elem[1]
l2.append((pos0, pos1, pos2))
elem = l2[0]
l2.pop(0)
s = Skyline('null', elem[0], elem[1], elem[2])
for k in l2:
s.addBuilding(k[0], k[1], k[2])
lr = s.getBuildingsList()
return s
# replicate the skyline n times
def replicateSkyline(self, n):
l = (self.bl).copy()
lr = (self.bl).copy()
w = 0
minx = l[0][0]
maxx = l[0][2]
for elem in l:
if elem[0] < minx:
minx = elem[0]
if elem[2] > maxx:
maxx = elem[2]
w = maxx - minx
ampOrig = w
for k in range(n-1):
laux = []
for j in l:
j = list(j)
j[0] += w
j[2] += w
j = tuple(j)
laux.append(j)
w = w + ampOrig
lr += laux
elem = lr[0]
lr.pop(0)
s = Skyline('null', elem[0], elem[1], elem[2])
for k in lr:
s.addBuilding(k[0], k[1], k[2])
lr2 = s.getBuildingsList()
return s
# shift skyline n positions to the right
def shiftRight(self, n):
l = []
aux = (self.bl).copy()
for k in aux:
k = list(k)
k[0] += n
k[2] += n
k = tuple(k)
l.append(k)
elem = l[0]
l.pop(0)
s = Skyline('null', elem[0], elem[1], elem[2])
for k in l:
s.addBuilding(k[0], k[1], k[2])
lr2 = s.getBuildingsList()
return s
# shift skyline n positions to the left
def shiftLeft(self, n):
l = []
aux = (self.bl).copy()
for k in aux:
k = list(k)
k[0] -= n
k[2] -= n
k = tuple(k)
l.append(k)
elem = l[0]
l.pop(0)
s = Skyline('null', elem[0], elem[1], elem[2])
for k in l:
s.addBuilding(k[0], k[1], k[2])
lr2 = s.getBuildingsList()
return s
# returns the especified random skyline
def randomFunc(self, n, h, w, xmin, xmax):
a = random.randint(xmin, xmax-w)
b = random.randint(0, h)
ww = random.randint(1, w)
c = a+ww
s = Skyline('null', a, b, c)
it = 0
nn = n-1
while it < n-1:
a2 = random.randint(xmin, xmax-1)
b2 = random.randint(0, h)
ww2 = random.randint(1, w)
c2 = a2+ww2
s.addBuilding(a2, b2, c2)
it += 1
l = s.getBuildingsList()
s.noOverlapping()
return s
# ########PROCESSING########## #
# creates the skyline's plot
def plotProcessing(self):
if self.id == 'null':
id = str(datetime.datetime.now())
id = id.replace(" ", "")
id = id.replace(".", "")
id = id.replace(":", "")
id = id.replace("-", "")
self.id = id
xl = []
widthl = []
heightl = []
for k in self.bl:
w = k[2] - k[0]
xl += [k[0]]
widthl += [w]
heightl += [k[1]]
plt.bar(xl, heightl, widthl, 0, align='edge')
plt.savefig(str(self.id))
plt.clf()
# ###########UTILS############ #
def graterThan(self, a, b): # a > b
if (a[0] > b[0]):
return True
else:
return False
def equals(self, a, b): # a == b
if (a[0] == b[0]):
return True
else:
return False
def lowerEq(self, a, b): # a <= b
if(a[0] <= b[0]):
return True
else:
return False
def graterEq(self, a, b): # a >= b
if(a[0] >= b[0]):
return True
else:
return False
def buildingArea(self, b):
y = b[1]
z = b[2]
x = b[0]
a = (y+1)*(z-x)
return a
def noOverlapping(self):
buildings = self.bl
edges = []
edges.extend([building[0], building[2]] for building in buildings)
edges = sorted(sum(edges, []))
current = 0
points = []
for i in edges:
active = []
active.extend(building for building in buildings if (building[0] <= i and building[2] > i))
if not active:
current = 0
points.append((i, 0))
continue
max_h = max(building[1] for building in active)
if max_h != current:
current = max_h
points.append((i, max_h))
l = []
for index, item in enumerate(points):
if index < len(points)-1:
a = item[0]
b = item[1]
elem = points[index+1]
c = elem[0]
if b != 0:
l.append((a, b, c))
self.bl = l