-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathfindingVisualizer.py
More file actions
330 lines (280 loc) · 9.67 KB
/
PathfindingVisualizer.py
File metadata and controls
330 lines (280 loc) · 9.67 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
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
import pygame
import sys
from queue import PriorityQueue
from tkinter import *
from tkinter import messagebox
import math
DISPLAY_SIZE = 1000
NUMBER_OF_ROWS = 50
START_COLOUR = (255,0,255)
END_COLOUR = (199,21,133)
VISITED = (39, 174, 96)
VISITING = (39, 174, 96)
DEFAULT_COLOUR = (26,26,26)
GRID_LINE_COLOUR = (31,31,31)
BARRIER_COLOUR = (0, 0, 0)
PATH_COLOUR = (255, 255, 0)
display = pygame.display.set_mode((DISPLAY_SIZE, DISPLAY_SIZE))
pygame.display.set_caption("Pathfinding Visualizer")
class Node:
# Node constructor
def __init__(self, row, col, blockSize):
self.row = row
self.col = col
self.x = row * blockSize
self.y = col * blockSize
self.blockSize = blockSize
self.colour = DEFAULT_COLOUR
def isNotBarrier(self):
return self.colour != BARRIER_COLOUR
def setColour(self, colour):
self.colour = colour
def isNotDefaultColour(self):
return self.colour != DEFAULT_COLOUR
# we will only redraw the node (not the whole display)
# The normal parameter is for the animation
# normal = True means draw the regular square
# normal = False means draw a circle instead
def drawNode(self, display, normal = True):
rect = pygame.Rect(self.x, self.y, self.blockSize, self.blockSize)
if normal:
pygame.draw.rect(display, self.colour, rect)
else:
centre = (self.x + self.blockSize // 2, self.y + self.blockSize // 2)
pygame.draw.circle(display, self.colour, centre, self.blockSize // 2.5)
pygame.draw.rect(display, GRID_LINE_COLOUR, rect, 1)
pygame.display.update(rect)
# basic defintion to compare nodes (ie. always return True)
def __lt__(self, other):
return True
def makeGrid(display, size, numRows):
grid = []
blockSize = size // numRows
for i in range(numRows):
grid.append([])
for j in range(numRows):
node = Node(i, j, blockSize)
node.drawNode(display)
grid[i].append(node)
return grid
def resetGrid(display, grid):
for row in grid:
for node in row:
if node.isNotDefaultColour():
node.setColour(DEFAULT_COLOUR)
node.drawNode(display)
def getMousePosition(position, size, numRows):
blockSize = size // numRows
y, x = position
return y // blockSize, x // blockSize
def resetSearchingAnimation(display, startNode, endNode, grid):
for row in grid:
for node in row:
if node.isNotDefaultColour() and node.isNotBarrier() and node is not startNode and node is not endNode:
node.setColour(DEFAULT_COLOUR)
node.drawNode(display)
def getListOfNeighbours(node, grid):
neighbourList = []
# up
if node.row - 1 >= 0:
neighbourList.append(grid[node.row - 1][node.col])
# down
if node.row + 1 < NUMBER_OF_ROWS:
neighbourList.append(grid[node.row + 1][node.col])
# left
if node.col - 1 >= 0:
neighbourList.append(grid[node.row][node.col - 1])
# right
if node.col + 1 < NUMBER_OF_ROWS:
neighbourList.append(grid[node.row][node.col + 1])
##########################################
# the following logic is if you want to include diagonals
# if not, use Manhattan distance instead of Euclidean distance
# # top left
# if node.row - 1 >= 0 and node.col - 1>= 0:
# neighbourList.append(grid[node.row - 1][node.col - 1])
# # top right
# if node.row - 1 >= 0 and node.col + 1 < NUMBER_OF_ROWS:
# neighbourList.append(grid[node.row - 1][node.col + 1])
# # bottom left
# if node.row + 1 < NUMBER_OF_ROWS and node.col - 1 >= 0:
# neighbourList.append(grid[node.row + 1][node.col - 1])
# # bottom right
# if node.row + 1 < NUMBER_OF_ROWS and node.col + 1 < NUMBER_OF_ROWS:
# neighbourList.append(grid[node.row + 1][node.col + 1])
##########################################
return neighbourList
def dijkstrasAlgorithm(display, startNode, endNode, grid):
queue = PriorityQueue()
queue.put((0, 0, startNode))
queueNodes = {startNode}
previousNode = {}
distanceOfNode = {node: float("inf") for row in grid for node in row}
distanceOfNode[startNode] = 0
numNodes = 0
clock = pygame.time.Clock()
while not queue.empty():
node = queue.get()[2]
queueNodes.remove(node)
if node is endNode:
resetSearchingAnimation(display, startNode, endNode, grid)
path = []
node = previousNode[endNode]
while node is not startNode:
path.append(node)
node = previousNode[node]
path = list(reversed(path))
return path, numNodes
listOfNeighbours = getListOfNeighbours(node, grid)
for neighbour in listOfNeighbours:
if neighbour.isNotBarrier():
distance = distanceOfNode[node] + 1
if (distance < distanceOfNode[neighbour]):
previousNode[neighbour] = node
distanceOfNode[neighbour] = distance
if neighbour not in queueNodes:
numNodes += 1
queue.put((distance, numNodes, neighbour))
queueNodes.add(neighbour)
if neighbour is not endNode:
neighbour.setColour(VISITING)
neighbour.drawNode(display, False)
if node is not startNode:
node.setColour(VISITED)
node.drawNode(display)
for event in pygame.event.get():
# check if the user exits the pygame display
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
clock.tick(60)
return None, numNodes
def getManhattanDistance(startNode, endNode):
return abs(startNode.row - endNode.row) + abs(startNode.col - endNode.col)
def getEuclideanDistance(startNode, endNode):
return math.sqrt(abs(startNode.row - endNode.row)**2 + abs(startNode.col - endNode.col)**2)
def aStarAlgorithm(display, startNode, endNode, grid):
queue = PriorityQueue()
queue.put((0, 0, startNode))
queueNodes = {startNode}
previousNode = {}
gScore = {node: float("inf") for row in grid for node in row}
gScore[startNode] = 0
fScore = {node: float("inf") for row in grid for node in row}
fScore[startNode] = getManhattanDistance(startNode, endNode)
numNodes = 0
clock = pygame.time.Clock()
while not queue.empty():
node = queue.get()[2]
queueNodes.remove(node)
if node is endNode:
resetSearchingAnimation(display, startNode, endNode, grid)
path = []
node = previousNode[endNode]
while node is not startNode:
path.append(node)
node = previousNode[node]
path = list(reversed(path))
return path, numNodes
listOfNeighbours = getListOfNeighbours(node, grid)
for neighbour in listOfNeighbours:
if neighbour.isNotBarrier():
tentativeGScore = gScore[node] + 1
if tentativeGScore < gScore[neighbour]:
previousNode[neighbour] = node
gScore[neighbour] = tentativeGScore
fScore[neighbour] = gScore[neighbour] + getManhattanDistance(neighbour, endNode)
if neighbour not in queueNodes:
numNodes += 1
queue.put((fScore[neighbour], numNodes, neighbour))
queueNodes.add(neighbour)
if neighbour is not endNode:
neighbour.setColour(VISITING)
neighbour.drawNode(display, False)
if node is not startNode:
node.setColour(VISITED)
node.drawNode(display)
for event in pygame.event.get():
# check if the user exits the pygame display
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
clock.tick(60)
return None, numNodes
def drawPath(display, path, colour, animationTime):
for node in path:
node.setColour(colour)
node.drawNode(display)
pygame.time.wait(animationTime)
def main(display, size, numRows):
pygame.init()
grid = makeGrid(display, size, numRows)
startNode = None
endNode = None
path = []
window = Tk()
window.eval('tk::PlaceWindow %s center' % window.winfo_toplevel())
window.withdraw()
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
# check if the user exits the pygame display
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# check if the user has typed
if event.type == pygame.KEYDOWN:
dKey = event.key == pygame.K_d
aKey = event.key == pygame.K_a
# if either a the d-key or a-key was pressed
if dKey or aKey:
if dKey:
# start finding the path with dijkstras
path, numNodes = dijkstrasAlgorithm(display, startNode, endNode, grid)
else:
# start finding the path with dijkstras
path, numNodes = aStarAlgorithm(display, startNode, endNode, grid)
if path is not None:
drawPath(display, path, PATH_COLOUR, 30)
msg = 'Path length: ' + str(len(path) + 1)
else:
msg = 'Path does not exist'
messagebox.showinfo('Pathfinding stats: ', 'Searched: ' + str(numNodes) + ' nodes' + '\n' + msg)
window.quit()
# reset the display
elif event.key == pygame.K_r:
for row in grid:
for node in row:
resetGrid(display, grid)
startNode = endNode = None
# check if the user left or right clicks the mouse
leftClick = pygame.mouse.get_pressed()[0]
rightClick = pygame.mouse.get_pressed()[2]
if leftClick or rightClick:
position = pygame.mouse.get_pos()
row, col = getMousePosition(position, size, numRows)
node = grid[row][col]
# left click
if leftClick:
if startNode is None and node is not endNode:
node.setColour(START_COLOUR)
node.drawNode(display)
startNode = node
elif endNode is None and node is not startNode:
node.setColour(END_COLOUR)
node.drawNode(display)
endNode = node
elif node is not startNode and node is not endNode:
node.setColour(BARRIER_COLOUR)
node.drawNode(display)
# right click
else:
if node is startNode:
startNode = None
elif node is endNode:
endNode = None
node.setColour(DEFAULT_COLOUR)
node.drawNode(display)
clock.tick(60)
if __name__ == "__main__":
main(display, DISPLAY_SIZE, NUMBER_OF_ROWS)