-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
72 lines (64 loc) · 2.2 KB
/
Copy pathmain.py
File metadata and controls
72 lines (64 loc) · 2.2 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
from sorting import Sorting
import pygame_menu
import pygame,math,random,time
from pygame.display import update
from pygame.draw import rect
import matplotlib.cm as cm
import numpy as np
# TODO : make the window resizable
pygame.init()
running = True
w = 1200
h = 800
screen = pygame.display.set_mode((w,h),pygame.RESIZABLE)
pygame.display.set_caption('Sorting Visualiser')
num = 500
arr = [random.randint(0,num - 2) for _ in range(num)]
cycle = []
maximum = max(arr)
height = h / maximum
width = int(w / num ) + 0.5
print(width)
sortingClass = Sorting(width,height,screen,cycle)
isSorted = False
func = sortingClass.bubbleSort
def selectAlgo(_,i):
global sortingClass,arr,func
l = ["","bubbleSort","insertSort","selectionSort","mergeSort","quickSort","heapSort"]
func = getattr(sortingClass,l[i])
def selectColor(_,i):
global sortingClass
cycle = []
l = ["","rainbow","gray","summer","prism"]
f = getattr(cm,l[i])
for [r,g,b,_] in f(np.linspace(0, 1, num)):
r *= 255
g *= 255
b *= 255
cycle.append((r,g,b))
sortingClass.cycle = cycle
selectColor(None,1)
def sortIt():
func(arr)
def reset():
global arr
arr = [random.randint(0,num - 2) for _ in range(num)]
menu = pygame_menu.Menu('Sorting Visualiser', w, h,
theme=pygame_menu.themes.THEME_DARK) #type:ignore
menu.add.button('Start', sortIt)
menu.add.button('Reset array', reset)
menu.add.dropselect('Algorithm :', [('Bubble Sort', 1), ('Insert Sort', 2),("Selection Sort",3),("Merge Sort",4),("Quick Sort",5),("Heap Sort",6)], onchange=selectAlgo)
menu.add.dropselect('Color Scheme :', [('Rainbow', 1), ('Black / White', 2),("Summer",3),("Prism",4)], onchange=selectColor)
menu.add.button('Quit', pygame_menu.events.EXIT) #type:ignore
menu.enable()
while running:
menu.mainloop(screen)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
running = False
if event.type == pygame.VIDEORESIZE:
# There's some code to add back window content here.
pygame.display.set_mode((event.w, event.h),pygame.RESIZABLE)
menu._window_size