/
Interface.py
593 lines (494 loc) · 19.2 KB
/
Interface.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
import sys
import pygame
import pygame.display as pydisplay
import pygame.mouse as pymouse
import pygame.draw as pydraw
from pygame.locals import *
from main import *
pygame.init()
"""Globals"""
button_unselected = (255, 255, 255)
not_selected_color = (0, 0, 255)
selected_color = (255, 0, 0)
selected_algorithm = "" # used for determining which algorithm to use
start = 0 # default starting node
# add_node_mode = 0 --> adding edges
# add_node_mode = 1 --> adding nodes
# add_node_mode = 2 --> deleting nodes <-- this has been removed
# add_node_mode = 3 --> deleting edges
# add_node_mode = 4 --> select starting node
add_node_mode = 1 # differentiate between adding nodes or edges
screen = pydisplay.set_mode((1400, 800)) # display surface for graph creation
graph_screen = pygame.Rect((120, 0, 1400, 800))
font = pygame.font.Font(None, 28) # font to use
# used to add edges
primary = -1
secondary = -1
node_name = 0 # used to name nodes
class Node:
"""
Class to represent nodes
Attributes
----------
name : int
name of the node
colour : tuple
colour of node in the form of (r, g, b)
position : tuple
position of node in the form of (x, y)
state : int
decides whether or not node has been selected
Methods
-------
is_selected()
changes the state and colour of node to being selected
not_selected()
changes the state and colour of node to being not selected
draw()
draws the node on pygame screen
"""
def __init__(self, name, colour, position, state):
"""
Parameters
----------
name : int
Name of the node
colour : tuple
Triplet of integers in the form of (r, g, b)
position : tuple
Position of node in the form of (x, y)
state : int
Decides whether or not node has been selected
True means it is selected, False means unselected
"""
self.name = name
self.colour = colour
self.position = position
self.state = state
def is_selected(self):
"""
Changes the state and colour of node to being selected
"""
self.state = 1
self.colour = selected_color
def not_selected(self):
"""
Changes the state and colour of node to being not selected
"""
self.state = 0
self.colour = not_selected_color
def draw(self):
"""
Draws the node on pygame screen
"""
if self.state != -1:
pydraw.circle(screen, self.colour, self.position, 10)
class Edge:
"""
Class to represent edges. Each edge is between u and v.
Attributes
----------
u : Node
Node object. Vertex in edge u--v.
v: Node
Node object. Vertex in edge u--v.
colour : tuple
Triplet of integers in the form of (r, g, b)
weight : float
Euclidean distance between the coordinates of nodes u and v
Methods
-------
get_edge()
Returns the edge object
get_edge_data()
Returns the edge as a tuple with ints and floats
is_selected()
Changes colour to be selected
not_selected()
Changes colour to not be selected
draw()
Draws the edge on the pygame screen
"""
def __init__(self, u, v, colour):
"""
Parameters
----------
u : Node
one of the nodes that the edge will be between
v : Node
one of the nodes that the edge will be between
colour : tuple
colour of edge in the form of (r, g, b)
"""
self.u = u
self.v = v
self.colour = colour
x1, y1 = self.u.position
x2, y2 = self.v.position
self.weight = ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5
self.edge = (self.u, self.v, self.weight)
def get_edge(self):
"""
Returns the edge object
"""
return self.edge
def get_edge_data(self):
"""
Returns the edge as a tuple with ints and floats
"""
return self.u.name, self.v.name, self.weight
def is_selected(self):
"""
changes colour to be selected
"""
self.colour = selected_color
def not_selected(self):
"""
changes colour to not be selected
"""
self.colour = not_selected_color
def draw(self):
"""
draws the edge on the pygame screen
"""
pydraw.line(screen, self.colour, self.u.position, self.v.position, 2)
class Graph:
"""
Class to represent a graph
Attributes
----------
graph : dict
Adjacency list in the form of a python dictionary with keys as nodes and values as lists
containing neighbors in the form (distance, node)
edge_list : list
List of all edges in the graph as triplets (u, v, weight)
Methods
-------
add_node(node)
Adds a node to the graph by creating a key for it in the adjacency list
add_bi_edge(edge)
Adds an edge by updating the adjacency list and also appending it to edge_list
del_edge(edge)
Deletes the edge from edge_list and updates the adjacency list
get_graph()
Returns the adjacency list with node objects
get_edges()
Returns edge_list but with ints and floats instead of edge objects
get_positions()
Returns a dictionary with node names as keys and positions as values
draw()
draws the graph by calling draw functions for all nodes and edges in the graph
"""
def __init__(self):
self.graph = {}
self.edge_list = []
def add_node(self, node):
"""
Adds a node to the graph by creating a key for it in the dictionary
Parameters
----------
node : Node
The node object that needs to be added to the graph
"""
self.graph[node] = []
def add_bi_edge(self, edge):
"""
Adds an edge by updating the adjacency list and also appending it to edge_list
Parameters
----------
edge : Edge
Edge object
"""
self.graph[edge.u].append((edge.v, edge.weight))
self.graph[edge.v].append((edge.u, edge.weight))
self.edge_list.append(edge)
self.edge_list = list(set(self.edge_list)) # insure no duplicates
def del_edge(self, edge):
"""
Deletes the edge from edge_list and updates the adjacency list
Parameters
----------
edge : Edge
Edge object that is being removed
"""
self.edge_list.remove(edge)
self.graph[edge.u].remove((edge.v, edge.weight))
self.graph[edge.v].remove((edge.u, edge.weight))
def get_graph(self):
"""
Returns the adjacency list with all node objects
"""
return self.graph
def get_nodes(self):
"""
Return the graph in adjacency list form with ints and floats excluding nodes with state equal to -1
Returns
-------
nodes : dict
Adjacency list in the form of a python dictionary with nodes as keys and lists of edges as values
"""
nodes = {}
for node in self.graph:
if node.state != -1:
nodes[node.name] = set()
for adjacent in self.graph[node]:
if adjacent[0].state != -1:
nodes[node.name].add((adjacent[0].name, adjacent[1]))
return nodes
def get_edges(self):
"""
Returns edge_list with ints and floats instead of edge objects
"""
# (v, u, weight) and (u, v, weight) will not be treated as the same
edges = []
for edge in self.edge_list:
edges.append((edge.u.name, edge.v.name, edge.weight))
return list(set(edges))
def get_positions(self):
"""
Returns a dictionary with node names as keys and positions as values
Ignores nodes with state of -1
Returns
-------
pos : dict
Keys are nodes, values are positions in the form (x, y)
"""
pos = {}
for node in self.graph:
if node.name not in pos.keys() and node.state != -1:
pos[node.name] = node.position
return pos
def not_within_min(self, mouse_pos):
"""
Checks if the mouse click was within a certain distance from a node
Parameters
----------
mouse_pos : tuple
Coordinates of mouse click
Returns
-------
not_within : boolean
True if mouse click was far enough away (> min_distance) from a node, false otherwise
"""
not_within = True
min_distance = 20
x_mpos, y_mpos = mouse_pos[0], mouse_pos[1]
for node in self.graph:
if node.state != -1:
x_node, y_node = node.position
distance = ((x_node - x_mpos) ** 2 + (y_node - y_mpos) ** 2) ** .5
if distance < min_distance:
not_within = False
if add_node_mode != 1:
node.is_selected() # set node as being selected
return not_within
def draw(self):
"""
Draws the graph by calling draw functions for all nodes and edges in the graph
"""
for node in self.graph:
node.draw()
for edge in self.edge_list:
edge.draw()
my_graph = Graph() # create our graph
class Button(pygame.Rect):
"""
Class to represent Buttons that inherits for Rect class in pygame
Attributes
----------
x : int
x-coordinate of top left corner of button
y : int
y-coordinate of top left corner of button
width : int
Width of the button
height : int
Height of the button
text : str
String that is displayed on the button and is used to identify the button
colour : tuple
Colour of the button in the form of (r, g, b)
shown : int
Determines if the button is displayed or not
Methods
-------
draw()
Draws the button on the pygame screen
is_clicked(mouse_pos)
Determines if a button was clicked or not, handles what happens if a certain button is clicked
"""
def __init__(self, x, y, width, height, text, colour, shown):
"""
Parameters
----------
x : int
x co-ordinate of top left corner of button
y : int
y co-ordinate of top left corner of button
width : int
width of the button
height : int
height of the button
text : str
string that is displayed on the button and is used to identify the button
colour : tuple
colour of the button in the form of (r, g, b)
shown : int
determines if the button is displayed or not
"""
self.x = x
self.y = y
self.height = height
self.width = width
self.text = text
self.colour = colour
self.shown = shown
def draw(self):
"""
Draws the button on the screen if it is being shown
"""
if self.shown:
button_text = font.render(self.text, True, (0, 0, 0))
width = button_text.get_width()
height = button_text.get_height()
pygame.draw.rect(screen, self.colour, self)
screen.blit(button_text, (self.x - (width - self.width) / 2, self.y - (height - self.height) / 2))
def is_clicked(self, mouse_pos):
"""
Returns whether or not a button was clicked on, True if it was, False otherwise
Also handles what happens if certain buttons are clicked
Parameters
----------
mouse_pos : tuple
Position of mouse click in form of (x, y)
"""
global add_node_mode, selected_algorithm
algo_buttons = ["Bfs", "Dfs", "Dijkstra", "Kruskal"]
if self.collidepoint(mouse_pos): # different things happen if different buttons are clicked
if self.text == "Add Edge":
add_node_mode = 0
elif self.text == "Add Node":
add_node_mode = 1
elif self.text == "Del Node":
add_node_mode = 2
elif self.text == "Del Edge":
add_node_mode = 3
elif self.text == "Selection": # open selection menu
bfs_mode.shown = 1
dfs_mode.shown = 1
dij_mode.shown = 1
kru_mode.shown = 1
elif self.text in algo_buttons: # if one of the algorithms is selected
selected_algorithm = self.text
# change add_node_mode and shown prompt
add_node_mode = 4
select_start_prompt.shown = 1
elif self.text == "Select Start node": # enables selection of start node
add_node_mode = 4
elif self.text == "Run": # Run button is responsible for animating the algorithms
positions = my_graph.get_positions()
nodes = [node for node in my_graph.get_nodes().keys()]
edges = my_graph.get_edges()
create_networkx_graph(positions, nodes, edges)
fig, ax = plt.subplots(figsize=(14, 7))
if selected_algorithm == "":
print("UH OH")
elif selected_algorithm == "Bfs":
ani_mst = mpa.FuncAnimation(fig, update_bfs, fargs=(start,), interval=1000)
elif selected_algorithm == "Dfs":
ani_mst = mpa.FuncAnimation(fig, update_dfs, fargs=(start,), interval=1000)
elif selected_algorithm == "Dijkstra":
ani_mst = mpa.FuncAnimation(fig, update_dijk, fargs=(start,), interval=1000)
elif selected_algorithm == "Kruskal":
ani_mst = mpa.FuncAnimation(fig, update_mst, interval=1000)
fig.tight_layout()
plt.show()
self.colour = selected_color
return True
self.colour = button_unselected
return False
"""Create buttons"""
add_node = Button(10, 10, 100, 50, "Add Node", button_unselected, 1)
add_edge = Button(10, 70, 100, 50, "Add Edge", button_unselected, 1)
del_edge = Button(10, 190, 100, 50, "Del Edge", button_unselected, 1)
select_algorithm = Button(10, 250, 100, 50, "Selection", button_unselected, 1)
bfs_mode = Button(10, 310, 100, 50, "Bfs", button_unselected, 0)
dfs_mode = Button(10, 370, 100, 50, "Dfs", button_unselected, 0)
dij_mode = Button(10, 430, 100, 50, "Dijkstra", button_unselected, 0)
kru_mode = Button(10, 490, 100, 50, "Kruskal", button_unselected, 0)
run_visual = Button(10, 740, 100, 50, "Run", button_unselected, 1)
select_start_prompt = Button(130, 740, 200, 50, "Select Start node", button_unselected, 0)
buttons = [add_node, add_edge, del_edge, select_algorithm, bfs_mode, dfs_mode, dij_mode, kru_mode, run_visual,
select_start_prompt] # list of all buttons
def main():
"""
The main function. The driving function for Pygame,
and the one which pieces functions and classes together to run the program.
"""
global primary, secondary, node_name, start
pydisplay.init()
pydisplay.set_caption("Create your graph")
while True: # will always be running
pygame.draw.rect(screen, (192, 192, 192), graph_screen) # draw background for graph screen
for event in pygame.event.get():
if event.type in (QUIT, KEYDOWN): # exit screen check
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN: # button click check
for button in buttons: # check if a button is clicked
button.is_clicked(event.pos)
mouse_pos = pymouse.get_pos()
# adding nodes -> must be far enough away, on screen, correct mode
if my_graph.not_within_min(mouse_pos) and graph_screen.collidepoint(mouse_pos) and add_node_mode == 1:
new_node = Node(node_name, not_selected_color, mouse_pos, 0)
my_graph.add_node(new_node)
node_name += 1
# adding edges -> a node must be selected and correct mode
if not my_graph.not_within_min(mouse_pos) and not add_node_mode:
for node in my_graph.get_graph():
if primary == -1 and node.state == 1: # if no primary node has been selected
primary = node
elif primary != -1 and node.state == 1 and primary != node: # cannot make a edge with itself
secondary = node
if primary != -1 and secondary != -1: # add the edge and reset primary and secondary
new_edge = Edge(primary, secondary, not_selected_color)
if new_edge.get_edge_data() not in my_graph.get_edges(): # no duplicate edges allowed
my_graph.add_bi_edge(new_edge)
primary.not_selected()
secondary.not_selected()
primary = -1
secondary = -1
# deleting nodes -> correct mode, and node selected
if add_node_mode == 2 and not my_graph.not_within_min(mouse_pos):
for node in my_graph.get_graph(): # find which node was selected
if node.state == 1:
my_graph.del_node(node)
# deleting edges -> correct mode, edge selected NEEDS FIXING
if add_node_mode == 3 and not my_graph.not_within_min(mouse_pos):
for node in my_graph.get_graph():
if primary == -1 and node.state == 1: # if no primary node has been selected
primary = node
elif primary != -1 and node.state == 1 and primary != node: # cannot make a edge with itself
secondary = node
if primary != -1 and secondary != -1: # add the edge and reset primary and secondary
for edge in my_graph.edge_list:
if (edge.u is primary and edge.v is secondary) or (
edge.v is primary and edge.u is secondary):
my_graph.del_edge(edge)
primary.not_selected()
secondary.not_selected()
primary = -1
secondary = -1
if add_node_mode == 4 and not my_graph.not_within_min(mouse_pos):
num_selected = 0 # make sure only one start node can be selected at a time
for node in my_graph.get_graph():
if node.state == 1:
start = node.name
node.not_selected()
for node in my_graph.get_graph():
if node.name == start:
node.colour = selected_color
for button in buttons: # draw all buttons
button.draw()
my_graph.draw()
pydisplay.update()
main()