-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVisualiser.py
661 lines (560 loc) · 23.3 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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
import pygame
import os
import sys
from Algorithms.nodes.nodes import *
from Algorithms.nodes.button import *
from Algorithms.prims import prims
from Algorithms.astar import astar
from Algorithms.dijkstra import dijkstra
from Algorithms.kruskal import kruskal
from Algorithms.sidewinder import sidewinder
from Algorithms.recursive_backtracking import backtrack
from Algorithms.ellers import ellers
from Algorithms.aldous_broder import aldous
from Algorithms.wilson import wilson
from Algorithms.hunt_and_kill import hunt
from Algorithms.growing_tree import tree
from Algorithms.binary_tree import binary
from Algorithms.recursive_division import division
FILEPATH = os.getcwd()
WIDTH = 1584
COLUMNS = 99
HEIGHT = 784
ROWS = 49
GREY = (128,128,128)
WHITE = (255,255,255)
stage = 3
rainbow = [(255, 0, 0), (255, 255, 0), (0, 255, 0), (0, 0, 255), (75, 0, 130), (150, 0, 255)]
col1, col2, col3 = rainbow[stage]
count = 6
MODE = 0
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Visualiser")
pygame.display.set_icon(pygame.image.load(os.path.join(FILEPATH, 'Fonts', 'icon.png')))
def make_grid():
grid = []
gap = WIDTH // COLUMNS
for i in range(ROWS):
grid.append([])
for j in range(COLUMNS):
node = Nodes(i, j, gap, ROWS, COLUMNS, MODE)
grid[i].append(node)
return grid
def draw_grid(win):
gap = WIDTH // COLUMNS
for i in range(ROWS):
pygame.draw.line(win, GREY, (0, i*gap), (WIDTH, i*gap))
for i in range(COLUMNS):
pygame.draw.line(win, GREY, (i*gap, 0), (i*gap, HEIGHT))
def draw(win, grid, t, w=''):
for row in grid:
for node in row:
if w == 'wil':
# node.drawwil(win)
node.draw(win)
else:
node.draw(win)
if t:
draw_grid(win)
pygame.display.update()
def get_clicked(pos):
gap = WIDTH // COLUMNS
y, x = pos
row = y // gap
col = x // gap
return row, col
def main():
global MODE
grid = make_grid()
pygame.init()
start = None
end = None
togglegrid = True
run = True
started = False
sim = False
while run:
if not started:
draw(WIN, grid, togglegrid)
for event in pygame.event.get():
if event.type == pygame.QUIT:
return True
if pygame.mouse.get_pressed()[0] and not started:
if sim:
start = None
end = None
grid = make_grid()
pygame.display.set_caption("Visualiser: Edit")
sim = False
pos = pygame.mouse.get_pos()
row, col = get_clicked(pos)
spot = grid[col][row]
if not start and spot != end:
start = spot
start.make_start()
elif not end and spot != start:
end = spot
end.make_end()
elif spot != end and spot != start:
spot.make_barrier()
elif pygame.mouse.get_pressed()[2]and not started:
pos = pygame.mouse.get_pos()
row, col = get_clicked(pos)
spot = grid[col][row]
spot.reset()
if spot == start:
start = None
elif spot == end:
end = None
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_z and not started and start and end:
started = True
pygame.display.set_caption("Visualiser: A * Algorithm")
astar(lambda : draw(WIN, grid, togglegrid), grid, start, end, MODE)
started = False
sim = True
if event.key == pygame.K_x and not started and start and end:
started = True
pygame.display.set_caption("Visualiser: Dijkstra Algorithm")
dijkstra(lambda : draw(WIN, grid, togglegrid), grid, start, end, MODE)
started = False
sim = True
if event.key == pygame.K_4 and not started and not start and not end:
started = True
pygame.display.set_caption("Visualiser: Recursive backtracking Algorithm")
run = backtrack(lambda : draw(WIN, grid, togglegrid), grid, MODE)
started = False
if event.key == pygame.K_2 and not started and not start and not end:
started = True
pygame.display.set_caption("Visualiser: Kruskal's Algorithm")
run = kruskal(lambda : draw(WIN, grid, togglegrid), grid, MODE)
started = False
if event.key == pygame.K_3 and not started and not start and not end:
started = True
pygame.display.set_caption("Visualiser: Sidewinder Algorithm")
run = sidewinder(lambda : draw(WIN, grid, togglegrid), grid, MODE)
started = False
if event.key == pygame.K_1 and not started and not start and not end:
started = True
pygame.display.set_caption("Visualiser: Prims's Algorithm")
run = prims(lambda : draw(WIN, grid, togglegrid), grid, MODE)
started = False
if event.key == pygame.K_q and not started and not start and not end:
started = True
pygame.display.set_caption("Visualiser: Ellers's Algorithm")
run = ellers(lambda : draw(WIN, grid, togglegrid), grid, MODE)
started = False
if event.key == pygame.K_5 and not started and not start and not end:
started = True
pygame.display.set_caption("Visualiser: Aldous Broder Algorithm(VERY SLOW!!!)")
run = aldous(lambda : draw(WIN, grid, togglegrid), grid, MODE)
started = False
if event.key == pygame.K_0 and not started and not start and not end:
started = True
pygame.display.set_caption("Visualiser: Wilson's Algorithm")
run = wilson(lambda : draw(WIN, grid, togglegrid, 'wil'), grid, MODE)
started = False
if event.key == pygame.K_6 and not started and not start and not end:
started = True
pygame.display.set_caption("Visualiser: Hunt and Kill algorithm")
run = hunt(lambda : draw(WIN, grid, togglegrid), grid, MODE)
started = False
if event.key == pygame.K_7 and not started and not start and not end:
started = True
pygame.display.set_caption("Visualiser: Growing Tree algorithm")
run = tree(lambda : draw(WIN, grid, togglegrid), grid, MODE)
started = False
if event.key == pygame.K_8 and not started and not start and not end:
started = True
pygame.display.set_caption("Visualiser: Binary Tree algorithm")
run = binary(lambda : draw(WIN, grid, togglegrid), grid, MODE)
started = False
if event.key == pygame.K_9 and not started and not start and not end:
started = True
pygame.display.set_caption("Visualiser: Recursive Division algorithm")
run = division(lambda : draw(WIN, grid, togglegrid), grid, MODE)
started = False
if event.key == pygame.K_BACKSPACE:
start = None
end = None
grid = make_grid()
pygame.display.set_caption("Visualiser: Edit")
sim = False
if event.key == pygame.K_ESCAPE:
pygame.display.set_caption("Visualiser: Main Page")
return False
if event.key == pygame.K_TAB:
togglegrid = not togglegrid
if event.key == pygame.K_h:
helppage()
pygame.display.set_caption("Visualiser: Edit")
return True
def invert(color):
a, b, c = color
return (abs(255 - a), abs(255 - b), abs(255 - c))
def helppage(k=False):
if k:
return
pygame.init()
Quit = False
inv = False
background1 = pygame.image.load(os.path.join(FILEPATH, 'Fonts', 'Help1.png'))
nextpage = button((0, 0, 0), 1440, 350, 75, 100, FILEPATH, (0, 0, 0), text='>')
while not Quit:
if k:
return
for event in pygame.event.get():
pos = pygame.mouse.get_pos()
if event.type == pygame.QUIT:
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
return
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if nextpage.isOver(pos):
k = helppage2()
continue
if event.type == pygame.MOUSEMOTION:
if nextpage.isOver(pos):
inv = True
else:
inv = False
WIN.blit(background1, (0, 0))
fillhelp(inv, nextpage)
def helppage2():
pygame.init()
Quit = False
inv = False
inv1 = False
page = 1
background2 = pygame.image.load(os.path.join(FILEPATH, 'Fonts', 'Help2.png'))
prevpage = button((0, 0, 0), 1440, 350, 75, 100, FILEPATH, (0, 0, 0), text='<')
while not Quit:
for event in pygame.event.get():
pos = pygame.mouse.get_pos()
if event.type == pygame.QUIT:
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
return True
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if prevpage.isOver(pos):
page = 1
helppage()
if event.type == pygame.MOUSEMOTION:
if prevpage.isOver(pos):
inv1 = True
else:
inv1 = False
WIN.blit(background2, (0, 0))
fillhelp(inv1, prevpage)
def fillhelp(inv2, btn2):
color = WHITE
if inv2:
btn2.textcolor = invert(color)
btn2.color = color
else:
btn2.textcolor = color
btn2.color = invert(color)
btn2.draw(WIN, btn2.textcolor)
pygame.display.update()
# def mainpage(args):
# global stage, col1, col2, col3
# Quit = False
# pygame.init()
# invp = False
# invh = False
# background = pygame.image.load(os.path.join(FILEPATH, 'Fonts', 'background.jpg'))
# pygame.display.set_caption("Visualiser: Main Page")
# platbtn = button((0, 0, 0), 175, 500, 200, 100, FILEPATH, (0, 0, 0), text='Start')
# helpbtn = button((0, 0, 0), 385, 500, 200, 100, FILEPATH, (0, 0, 0), text='Help')
# while not Quit:
# WIN.blit(background, (0, 0))
# filltext(invp, platbtn, invh, helpbtn)
# for event in pygame.event.get():
# pos = pygame.mouse.get_pos()
# if event.type == pygame.QUIT:
# Quit = True
# continue
# if event.type == pygame.MOUSEBUTTONDOWN:
# if event.button == 1:
# if platbtn.isOver(pos):
# pygame.display.set_caption("Visualiser: Edit")
# Quit = main(args)
# invp = False
# if helpbtn.isOver(pos):
# pygame.display.set_caption("Visualiser: Help")
# helppage()
# invh = False
# if event.button == 4:
# stage = (stage + 1) % 6
# col1, col2, col3 = rainbow[stage]
# if event.button == 5:
# stage = (stage - 1) % 6
# col1, col2, col3 = rainbow[stage]
# if event.type == pygame.MOUSEMOTION:
# if platbtn.isOver(pos):
# invp = True
# else:
# invp = False
# if helpbtn.isOver(pos):
# invh = True
# else:
# invh = False
# def filltext(inv, btn, inv2, btn2):
# color = getcolor()
# Maintext = pygame.font.Font(os.path.join(FILEPATH, 'Fonts', 'Milton Keynes.ttf'), 78)
# welcome = Maintext.render("Algorithm Visualiser", True, color)
# s = pygame.Surface((720,700))
# s.set_alpha(200)
# s.fill(WHITE)
# WIN.blit(s, (40,50))
# s = pygame.Surface((740,720))
# s.set_alpha(10)
# s.fill(invert(color))
# WIN.blit(s, (30,40))
# s = pygame.Surface((760,740))
# s.set_alpha(5)
# s.fill(invert(color))
# WIN.blit(s, (20,30))
# s = pygame.Surface((780,760))
# s.set_alpha(2)
# s.fill(invert(color))
# WIN.blit(s, (10,20))
# WIN.blit(welcome, (75,100))
# if inv:
# btn.textcolor = invert(color)
# btn.color = color
# else:
# btn.textcolor = color
# btn.color = invert(color)
# btn.draw(WIN, btn.textcolor)
# if inv2:
# btn2.textcolor = invert(color)
# btn2.color = color
# else:
# btn2.textcolor = color
# btn2.color = invert(color)
# btn2.draw(WIN, btn2.textcolor)
# pygame.display.update()
# def invert(color):
# a, b, c = color
# return (abs(255 - a), abs(255 - b), abs(255 - c))
# def getcolor():
# global col1, col2, col3, count, stage
# count -= 1
# if count == 0:
# count = 6
# if stage == 0:
# if col2 < 255:
# col2 += 1
# if col2 == 255 :
# stage = 1
# col1 = 255
# col2 = 255
# col3 = 0
# elif stage == 1:
# if col1 > 0:
# col1 -= 1
# if col1 == 0:
# col1 = 0
# col2 = 255
# col3 = 0
# stage = 2
# elif stage == 2:
# if col3 < 255:
# col3 +=1
# if col2 > 0:
# col2 -= 1
# if col2 == 0 or col3 == 255:
# stage = 3
# col1 = 0
# col3 = 255
# col2 = 0
# elif stage == 3:
# if col1 < 75:
# col1 += 0.6
# if col3 > 130:
# col3 -= 1
# if col1 == 75 or col3 == 130:
# stage = 4
# col1 = 75
# col3 = 130
# col2 = 0
# elif stage == 4:
# if col3 < 255:
# col3 += 1
# if col1 < 150:
# col1 += 0.6
# if col1 == 150 or col3 == 255:
# stage = 5
# col1 = 150
# col3 = 255
# col2 = 0
# elif stage == 5:
# if col3 > 0:
# col3 -= 1
# if col1 < 254:
# col1 += 0.41
# if col3 == 0 and col1 <= 255:
# stage = 0
# col1 = 255
# col3 = 0
# col2 = 0
# return (col1, col2, col3)
# def helppage():
# pygame.init()
# global stage, col1, col2, col3
# Quit = False
# inv = False
# invh = False
# page = 1
# background = pygame.image.load(os.path.join(FILEPATH, 'Fonts', 'background.jpg'))
# backbtn = button((0, 0, 0), 650, 665, 100, 75, FILEPATH, (0, 0, 0), text='Back', size= 40)
# nextpage = button((0, 0, 0), 660, 350, 75, 100, FILEPATH, (0, 0, 0), text='>')
# prevpage = button((0, 0, 0), 65, 350, 75, 100, FILEPATH, (0, 0, 0), text='<')
# while not Quit:
# for event in pygame.event.get():
# pos = pygame.mouse.get_pos()
# if event.type == pygame.QUIT:
# pygame.quit()
# sys.exit()
# if event.type == pygame.MOUSEBUTTONDOWN:
# if event.button == 1:
# if backbtn.isOver(pos):
# pygame.display.set_caption("Visualiser: Main Page")
# Quit = True
# if nextpage.isOver(pos):
# page = 2
# invh = False
# if prevpage.isOver(pos):
# page = 1
# invh = False
# if event.button == 4:
# stage = (stage + 1) % 6
# col1, col2, col3 = rainbow[stage]
# if event.button == 5:
# stage = (stage - 1) % 6
# col1, col2, col3 = rainbow[stage]
# if event.type == pygame.MOUSEMOTION:
# if backbtn.isOver(pos):
# inv = True
# else:
# inv = False
# if nextpage.isOver(pos) and page == 1:
# invh = True
# elif prevpage.isOver(pos) and page == 2:
# invh = True
# else:
# invh = False
# WIN.blit(background, (0, 0))
# fillhelp(inv, backbtn, invh, nextpage, prevpage, page)
# def fillhelp(inv, btn, inv2, btn2, btn3, page):
# color = getcolor()
# Maintext = pygame.font.Font(os.path.join(FILEPATH, 'Fonts', 'Milton Keynes.ttf'), 40)
# welcome = Maintext.render("Help:", True, color)
# helptext = pygame.font.Font(os.path.join(FILEPATH, 'Fonts', 'Milton Keynes.ttf'), 20)
# lines = []
# lines.append(helptext.render("Different coloured squares represents the following:", True, color))
# lines.append(helptext.render("Color -> Representation", True, color))
# lines.append(helptext.render("------------------------", True, color))
# lines.append(helptext.render("White -> Empty Square", True, color))
# lines.append(helptext.render("Black -> Barrier", True, color))
# lines.append(helptext.render("Red -> Visited", True, color))
# lines.append(helptext.render("Green -> To be visited", True, color))
# lines.append(helptext.render("Orange -> Start Node", True, color))
# lines.append(helptext.render("Teal -> End Node", True, color))
# lines.append(helptext.render("Purple -> Shortest Path", True, color))
# lines.append(helptext.render("", True, color))
# lines.append(helptext.render("", True, color))
# lines.append(helptext.render("", True, color))
# lines.append(helptext.render("", True, color))
# lines.append(helptext.render("", True, color))
# lines.append(helptext.render("", True, color))
# lines.append(helptext.render("1)Draw starting and ending nodes", True, color))
# lines.append(helptext.render("2)Draw obstacles as you please or leave it to the pros(maze algorithms) ", True, color))
# lines.append(helptext.render("3)Use a path finding algorithm to find shortest path", True, color))
# line1 = []
# line1.append(helptext.render("Keys:", True, color))
# line1.append(helptext.render("Key Function", True, color))
# line1.append(helptext.render("------------------------------", True, color))
# line1.append(helptext.render("Mouse Scroll -> Change Color(Initial pages only.", True, color))
# line1.append(helptext.render(" Doesn't work in visualiser)", True, color))
# line1.append(helptext.render("Tab -> Switch off or on grid", True, color))
# line1.append(helptext.render("Escape -> Previous page", True, color))
# line1.append(helptext.render("Backspace -> Clear Screen", True, color))
# line1.append(helptext.render("Mouse Right Click -> Clear node or obstacle", True, color))
# line1.append(helptext.render("Mouse Left Click -> Add Node or obstacle", True, color))
# line1.append(helptext.render("p -> Draw maze using Prim'salgorithm", True, color))
# line1.append(helptext.render("k -> Draw maze using Kruskal's algorithm", True, color))
# line1.append(helptext.render("s -> Draw maze using Sidewinder algorithm", True, color))
# line1.append(helptext.render("b -> Draw maze using Recursive backtracking algorithm", True, color))
# line1.append(helptext.render("a -> Run A* algorithm", True, color))
# line1.append(helptext.render("d -> Run Dijkstra algorithm", True, color))
# line1.append(helptext.render("", True, color))
# line2 = []
# line2.append(helptext.render("", True, color))
# line2.append(helptext.render("", True, color))
# line2.append(helptext.render("Note:", True, color))
# line2.append(helptext.render("1) Maze algorithms don't work if start and end nodes are mentioned already.", True, color))
# line2.append(helptext.render("2) Path finding algorithms work only if start and end nodes are mentioned.", True, color))
# s = pygame.Surface((720,700))
# s.set_alpha(150)
# # s.fill(insvert(color))
# s.fill(WHITE)
# WIN.blit(s, (40,50))
# s = pygame.Surface((740,720))
# s.set_alpha(10)
# s.fill(invert(color))
# WIN.blit(s, (30,40))
# s = pygame.Surface((760,740))
# s.set_alpha(5)
# s.fill(invert(color))
# WIN.blit(s, (20,30))
# s = pygame.Surface((780,760))
# s.set_alpha(2)
# s.fill(invert(color))
# WIN.blit(s, (10,20))
# WIN.blit(welcome, (75,50))
# x = 160
# y = 70
# if page == 1:
# for li in lines:
# y += 25
# WIN.blit(li, (x, y))
# elif page == 2:
# for li in line1:
# y += 25
# WIN.blit(li, (x, y))
# x = 75
# for li in line2:
# y += 25
# WIN.blit(li, (x, y))
# if inv:
# btn.textcolor = invert(color)
# btn.color = color
# else:
# btn.textcolor = color
# btn.color = invert(color)
# btn.draw(WIN, btn.textcolor)
# if page == 1:
# if inv2:
# btn2.textcolor = invert(color)
# btn2.color = color
# else:
# btn2.textcolor = color
# btn2.color = invert(color)
# btn2.draw(WIN, btn2.textcolor)
# elif page == 2:
# if inv2:
# btn3.textcolor = invert(color)
# btn3.color = color
# else:
# btn3.textcolor = color
# btn3.color = invert(color)
# btn3.draw(WIN, btn3.textcolor)
# pygame.display.update()
if __name__ == "__main__":
main()