-
Notifications
You must be signed in to change notification settings - Fork 1
/
visualiser.py
156 lines (124 loc) · 5.12 KB
/
visualiser.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
#########################################
# Algorytmy Grafowe 2019/2020 #
# Wizualizator w matplotlib #
# Krzysztof Podsiadlo #
# https://github.com/Podsiadlo/GoGUI2/ #
#########################################
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.collections as mcoll
import matplotlib.colors as mcolors
from matplotlib.widgets import Button
import json as js
class _Button_callback(object):
def __init__(self, scenes):
self.i = 0
self.scenes = scenes
def set_axis(self, ax):
self.ax = ax
def next(self, event):
self.i = (self.i + 1) % len(self.scenes)
self.draw()
def prev(self, event):
self.i = (self.i - 1) % len(self.scenes)
self.draw()
def draw(self):
self.ax.clear()
for collection in self.scenes[self.i].points:
if len(collection.points) > 0:
self.ax.scatter(*zip(*(np.array(collection.points))), c=collection.color, marker=collection.marker)
for collection in self.scenes[self.i].lines:
self.ax.add_collection(collection.get_collection())
self.ax.autoscale()
plt.draw()
class Scene:
def __init__(self, points=[], lines=[]):
self.points = points
self.lines = lines
class PointsCollection:
def __init__(self, points=[], color=None, marker=None):
self.points = np.array(points)
self.color = color
self.marker = marker
class LinesCollection:
def __init__(self, lines=[], color=None):
self.color = color
self.lines = lines[:]
def add(self, line):
self.lines.append(line)
def get_collection(self):
if self.color:
return mcoll.LineCollection(self.lines, colors=mcolors.to_rgba(self.color))
else:
return mcoll.LineCollection(self.lines)
class Plot:
def __init__(self, scenes=[], json=None):
if json is None:
self.scenes = scenes
else:
self.scenes = [Scene([PointsCollection(pointsCol) for pointsCol in scene["points"]],
[LinesCollection(linesCol) for linesCol in scene["lines"]])
for scene in js.loads(json)]
def __configure_buttons(self, callback):
plt.subplots_adjust(bottom=0.2)
axprev = plt.axes([0.6, 0.05, 0.15, 0.075])
axnext = plt.axes([0.76, 0.05, 0.15, 0.075])
bnext = Button(axnext, 'Następny')
bnext.on_clicked(callback.next)
bprev = Button(axprev, 'Poprzedni')
bprev.on_clicked(callback.prev)
return [bprev, bnext]
def draw(self):
plt.close()
callback = _Button_callback(self.scenes)
self.widgets = self.__configure_buttons(callback)
callback.set_axis(plt.axes())
# plt.show()
callback.draw()
def toJSON(self):
return js.dumps([{"points": [pointCol.points.tolist() for pointCol in scene.points],
"lines": [linesCol.lines for linesCol in scene.lines]}
for scene in self.scenes])
if __name__ == '__main__':
scenes = [Scene([PointsCollection([(1, 2), (3, 1.5), (2, -1)]),
PointsCollection([(5, -2), (2, 2), (-2, -1)], 'green', marker="^")],
[LinesCollection([[(1, 2), (2, 3)], [(0, 1), (1, 0)]], 'orange')]),
Scene([PointsCollection([(1, 2), (-15, 1.5), (2, -1)], 'red'),
PointsCollection([(5, -2), (2, 2), (-2, 1)], 'black')],
[LinesCollection([[(-1, 2), (-2, 3)], [(0, -1), (-1, 0)]])])]
plot = Plot(scenes)
plot.draw()
#########################################
# Algorytmy Grafowe 2019/2020 #
# Wizualizator KD-drzewa #
# Stanislaw Denkowski #
# Maciej Tratnowiecki #
#########################################
class KdtreeVisualiser:
def __init__(self):
self.points = []
self.lines = []
self.scenes = []
self.rects = []
self.res = []
def put_points(self, points):
self.points = points
self.scenes.append(Scene([PointsCollection(self.points)]))
def add_line(self, line, mark_points):
self.lines.append(line)
self.scenes.append(Scene([PointsCollection(self.points), PointsCollection(mark_points, 'orange')],
[LinesCollection(self.lines)]
))
def get_scenes(self):
res = []
res, self.scenes = self.scenes, res
return res
def add_search(self, scope, res=None):
lowerleft, upperright = scope.get_tuple()
upperleft = (lowerleft[0], upperright[1])
lowerright = (upperright[0], lowerleft[1])
self.rects += [[lowerleft,upperleft],[lowerright,upperright],[lowerleft,lowerright],[upperleft,upperright]]
if res is not None:
self.res += res
self.scenes.append(Scene([PointsCollection(self.points), PointsCollection(self.res, 'red', marker='x')],
[LinesCollection(self.rects), LinesCollection(self.rects[-4:],color='orange')]))