-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualizer.py
150 lines (130 loc) · 5.6 KB
/
visualizer.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
import os, sys, time
import import_algorithms as algorithms
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
from pygame.locals import *
import pygame
# Pygame Initialisation
pygame.init()
screen = pygame.display.set_mode((1024, 512), HWSURFACE|DOUBLEBUF|RESIZABLE)
screen.fill((255, 255, 255))
#algorithm names to be displayed
algorithm_names = [["Selection", "Bubble", "Merge", "Quick"],
["Comb", "Shell", "Heap", "Insertion"],
["Bitonic", "Cocktail", "Cycle", "Tim"],
["Gnome", "Stooge", "Radix", "Pancake"]]
def draw(size, pos):
width = size[0]
height = size[1]
cols = rows = 4
w = width/cols
h = height/rows
for row in range(rows+1):
if row != 0:
thickness = 3
color = (0, 0, 0)
else:
thickness = 1
color = (160, 160, 160)
pygame.draw.lines(screen, color, False, [(row*w, 0), (row*w, height)], thickness)
pygame.draw.lines(screen, color, False, [(0, row*h), (width, row*h)], thickness)
font = pygame.font.SysFont("cambria", 40)
for i, row in enumerate(range(rows)):
for j, col in enumerate(range(cols)):
x = col * (width/cols)
y = row * (height/rows)
text = font.render(algorithm_names[i][j], 1, (0, 0, 0))
screen.blit(text, (x + (w/2 - text.get_width()/2), y + (h/2 - text.get_height()/2)))
if pos != None:
row = int(pos[1]/h)
col = int(pos[0]/w)
pygame.draw.rect(screen, (0, 255, 0), (col*w, row*h, w, h), 4)
return(algorithm_names[row][col])
def events():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type==VIDEORESIZE:
screen=pygame.display.set_mode(event.dict['size'],HWSURFACE|DOUBLEBUF|RESIZABLE)
pygame.display.flip()
def sleep_time(algorithm):
if algorithm in ["Bubble Sort", "Cocktail Sort", "Gnome Sort", "Stooge Sort"]:
return 0
if algorithm in ["Comb Sort", "Radix Sort", "Shell Sort", "Tim Sort"]:
return 0.05
if algorithm in ["Bitonic Sort", "Heap Sort", "Merge Sort", "Quick Sort"]:
return 0.03
if algorithm in ["Cycle Sort", "Insertion Sort", "Selection Sort", "Pancake Sort"]:
return 0.1
def update_screen(algorithm, swap_index_1=None, swap_index_2=None, pivot=None, screen=screen):
screen.fill((255, 255, 255))
pygame.display.set_caption("Sorting Visualizer Algorithm: {} Status: Sorting Delay: {} ms".format(algorithm.name, int(1000*sleep_time(algorithm.name))))
width, height = pygame.display.get_surface().get_size()
length = len(algorithm.array)
size = (width//length)//4
for i in range(4*length):
if i%4 != 0:
colour = (66, 134, 244, 0.8)
if pivot == i//4:
colour = (169, 92, 232, 0.8)
if swap_index_1 == i//4:
colour = (0, 255, 0, 0.8)
elif swap_index_2 == i//4:
colour = (255, 0, 0, 0.8)
pygame.draw.rect(screen, colour, (i*size, height, size, -algorithm.array[i//4]))
else:
colour = (255, 255, 255)
pygame.draw.rect(screen, colour, (i*size, height, size, 0))
events()
pygame.display.update()
time.sleep(sleep_time(algorithm.name))
def fixed_update_screen(algorithm, status, screen=screen):
screen.fill((255, 255, 255))
if status == "Start":
pygame.display.set_caption("Sorting Visualizer Algorithm: {} Status: Generated Random Unsorted Array".format(algorithm.name))
elif status == "End":
pygame.display.set_caption("Sorting Visualizer Algorithm: {} Status: Array Sorted".format(algorithm.name))
width, height = pygame.display.get_surface().get_size()
length = len(algorithm.array)
size = (width//length)//4
for i in range(4*length):
if i%4 != 0:
colour = (66, 134, 244, 0.8)
pygame.draw.rect(screen, colour, (i*size, height, size, -algorithm.array[i//4]))
else:
colour = (255, 255, 255)
pygame.draw.rect(screen, colour, (i*size, height, size, 0))
events()
pygame.display.update()
def main():
while True:
pos = None
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type==VIDEORESIZE:
screen=pygame.display.set_mode(event.dict['size'],HWSURFACE|DOUBLEBUF|RESIZABLE)
pygame.display.flip()
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
algorithm_name = draw(pygame.display.get_surface().get_size(), pos)
if pos != None and algorithm_name != None:
pygame.display.update()
time.sleep(1.5)
#here array length is set to the width of the screen
array_length = pygame.display.get_surface().get_size()[1]
algorithm = algorithms.algorithms[algorithm_name]
algorithm.start(array_length)
while True:
events()
pygame.display.update()
break
screen.fill((255, 255, 255))
pygame.display.set_caption("Sorting Visualizer Select an Algorithm:")
draw(pygame.display.get_surface().get_size(), pos)
pygame.display.update()
if __name__=='__main__':
main()
pygame.quit()
sys.exit()