-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualizer.py
More file actions
266 lines (229 loc) · 7.98 KB
/
visualizer.py
File metadata and controls
266 lines (229 loc) · 7.98 KB
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
"""
Visualize different sorting algorithms
Author: Teodor Dahl Knutsen <teodor@dahlknutsen.no>
"""
from random import randint
import pygame
def float_to_color(val, red_val=0, blue_val=1):
"""
Turn a floating point to a color
red_val is the lower bound
blue_val is the upper bound
"""
int_val = int(1023 * (val - red_val) / (blue_val - red_val))
if int_val < 256:
# from red to yelow
return (255, int_val, 0)
if int_val < 512:
# yellow to green
int_val -= 256
return (255 - int_val, 255, 0)
if int_val < 768:
# green to aqua
int_val -= 512
return (0, 255, int_val)
# aqua to blue
int_val -= 768
return (0, 255 - int_val, 255)
class Visualize:
"""
The visualize class
"""
def __init__(self, dim=None, mode="rainbow", block_size=None, num=None, step_through=False, width=1000, height=1000):
"""
dim: with and height dimensions of the screen, a single integer
mode: string reprecenting the mode. default: 'rainbow'
block_size: the size of the blocks/bars/rainbow stripes.
num: the number of blocks/stripes/bars.
only one of num and block_size needs to be set, sice the other
one may be derived from the information of the window dimensions
If the block_size and num does not multiply to the dim,
dim gets set to the product of num and block_size
"""
if dim is None:
dim = width
if num is None and block_size is None:
self.num = dim
self.block_size = 1
else:
if num is None:
self.block_size = block_size
self.num = int(dim / block_size)
else:
self.num = num
self.block_size = int(dim / num)
dim = self.block_size * self.num
self.width = dim
self.height = height
self.data = []
self.draw_funcs = {"bars": self.draw_bars,
"grayscale": self.draw_grayscale,
"boxes": self.draw_boxes,
"rainbow": self.draw_rainbow,
"bars_ordered": self.draw_bars_order,
"ellipses": self.draw_ellipses}
self._mode = mode
self._vis_func = self.draw_funcs[self.mode]
#pylint: disable=no-member
pygame.init()
self.screen = pygame.display.set_mode((self.width, self.height))
self._bg = (0, 0, 0)
self._fg = (255, 255, 255)
self.step_through = step_through
@property
def visualize(self):
"""
Visualize a sorting function
"""
return self.visualization
@visualize.setter
def visualize(self, val):
if len(val) != 2:
return
self.draw_funcs[val[0]] = val[1]
@property
def mode(self):
"""
The visualization mode
"""
return self._mode
@mode.setter
def mode(self, val):
if self.draw_funcs[val] is not None:
self._vis_func = self.draw_funcs[val]
self._mode = val
def test_data(self):
"""
Check if data is sorted
"""
for i in range(len(self.data) - 1):
if self.data[i] > self.data[i + 1]:
print('array not sorted!')
exit()
print('Data is sorted!')
def visualization(self, sortfn):
"""
Visualize the sorting function
"""
self.gen_data()
sortfn(self.data, self.draw)
self.test_data()
def gen_data(self):
"""
Regenerate the data array
"""
self.data = [randint(0, self.height) for _ in range(self.num)]
def draw_ellipses(self, data, imp=None):
"""
Visualizes data as ellipses
"""
#pylint: disable=invalid-name
for x, y in enumerate(data):
x *= self.block_size / 2
col = float_to_color(y / self.height)
dim = self.width - x * 2
pygame.draw.ellipse(self.screen, col, (x, x, dim, dim))
def draw_bars(self, data, imp=None):
"""
Visualizes the data as bars
"""
#pylint: disable=invalid-name
for x, y in enumerate(data):
if imp is not None and imp.__contains__(x):
col = (255, 0, 0)
else:
col = self._fg
if self.block_size == 1:
pygame.draw.line(self.screen, col, (x, self.height), (x, self.height - y))
else:
pygame.draw.rect(self.screen, col,
(x * self.block_size, self.height - y, self.block_size, y))
def draw_bars_order(self, data, imp=None):
"""
Visualizes the data as bars
"""
prev = None
#pylint: disable=invalid-name
for x, y in enumerate(data):
if prev is None or y >= prev:
col = (0, 255, 0)
else:
col = (255, 0, 0)
prev = y
if self.block_size == 1:
pygame.draw.line(self.screen, col, (x, self.height), (x, self.height - y))
else:
pygame.draw.rect(self.screen, col,
(x * self.block_size, self.height - y, self.block_size, y))
def draw_boxes(self, data, imp=None):
"""
Visualizes the data as bars
"""
#pylint: disable=invalid-name
for x, y in enumerate(data):
if imp is not None and imp.__contains__(x):
col = (255, 0, 0)
else:
col = self._fg
pygame.draw.rect(self.screen, col,
(x * self.block_size, self.height - y,
self.block_size, self.block_size))
def draw_rainbow(self, data, imp=None):
"""
Grayscale visualization of the data
"""
#pylint: disable=invalid-name
for x, y in enumerate(data):
col = float_to_color(y / self.height)
#pygame.draw.line(self.screen, (col, col, col), (x, 0), (x, self.height))
if self.block_size == 1:
pygame.draw.line(self.screen, col, (x, 0), (x, self.height))
else:
pygame.draw.rect(self.screen, col,
(x * self.block_size, 0, self.block_size, self.height))
def draw_grayscale(self, data, imp=None):
"""
Grayscale visualization of the data
"""
#pylint: disable=invalid-name
for x, y in enumerate(data):
col = y / self.height
col *= 255
#pygame.draw.line(self.screen, (col, col, col), (x, 0), (x, self.height))
if self.block_size == 1:
pygame.draw.line(self.screen, (col, col, col), (x, 0), (x, self.height))
else:
pygame.draw.rect(self.screen, (col, col, col),
(x * self.block_size, 0, self.block_size, self.height))
def draw(self, data=None, imp=None):
"""
Draws the data array
"""
if data is None:
data = self.data
pygame.draw.rect(self.screen, self._bg, (0, 0, self.width, self.height))
self._vis_func(data, imp)
pygame.display.update()
events = pygame.event.get()
#pylint: disable=no-member
for event in events:
if event.type == pygame.QUIT:
exit()
while self.step_through:
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
exit()
if event.type == pygame.KEYDOWN or event.type == pygame.MOUSEBUTTONDOWN:
return
def example():
"""
An example usage
"""
from sort import radixsort, quicksort_hoare
vis = Visualize(block_size=4)
vis.visualize(radixsort)
vis.mode = "boxes"
vis.visualize(quicksort_hoare)
if __name__ == '__main__':
example()