-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneral.py
More file actions
154 lines (101 loc) · 5.05 KB
/
Copy pathgeneral.py
File metadata and controls
154 lines (101 loc) · 5.05 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
import pygame,time
from math import hypot
import math
def arrow(screen, start, end, lcolor = (255,255, 255), tricolor = (255,255,255), trirad= 8, thickness=5):
pygame.draw.line(screen, lcolor, start, end, thickness)
rotation = (math.atan2(start[1] - end[1], end[0] - start[0])) + math.pi/2
rad = 180/math.pi
pygame.draw.polygon(screen, tricolor, (
(end[0] + trirad * math.sin( rotation ), end[1] + trirad * math.cos( rotation )),
(end[0] + trirad * math.sin(rotation - 120*rad), end[1] + trirad * math.cos(rotation - 120*rad)),
(end[0] + trirad * math.sin(rotation + 120*rad), end[1] + trirad * math.cos(rotation + 120*rad))
)
)
pygame.display.update()
def distance(node1,node2):
return hypot(node1[0]-node2[0], node1[1]-node2[1])
def show_weight(screen,peso,position,font,peso_color = (0,255,255),radius = 10):
text = font.render(str(peso),True,peso_color,radius)
screen.blit(text,position)
def median_point(p1,p2):
return ((p1[0] + p2[0])/2, (p1[1] + p2[1])/2)
def memorize(screen,node_center,radius=10,Time = 0.1,show=True):
pygame.draw.circle(screen,(255,0,0),node_center,radius)
if show:
pygame.display.update()
time.sleep(Time)
def visit(screen,node_center,visit_color = (0,255,0),radius = 10,show=True,Time = 0.1):
pygame.draw.circle(screen,visit_color,node_center,radius)
if show:
pygame.display.update()
time.sleep(Time)
def visited(screen,node_center,radius = 10,show=True,Time = 0.1):
Cyan = (0,255,255)
pygame.draw.circle(screen, Cyan , node_center , radius)
if show:
pygame.display.update()
time.sleep(Time)
def mark(screen,node_center,color,radius,show=True,Time = 0.1):
pygame.draw.circle(screen, color, node_center , radius)
if show:
pygame.display.update()
time.sleep(Time)
# name of the algorithm
def top_info(screen,info_text,center_position = (400,30),size = 30):
font = pygame.font.Font('freesansbold.ttf',size)
text = font.render(info_text,True,(150,150,150))
screen.blit(text,text.get_rect(center = center_position))
pygame.display.update()
def informative_nodes(screen,
memory_color = (255, 0 , 0 ), memory_text ="memorized nodes",
visited_color = ( 0 ,255,255), visited_text ="visited nodes",
current_color = ( 0 ,255, 0 ), current_text = "current"):
font = pygame.font.Font('freesansbold.ttf',15)
pygame.draw.circle(screen,memory_color, (850,200),7)
text = font.render(memory_text,True,memory_color)
screen.blit(text,text.get_rect(center = (915,200)))
font = pygame.font.Font('freesansbold.ttf',15)
pygame.draw.circle(screen, (255,255,255), (850,150) , 7)
pygame.draw.circle(screen,visited_color,(850,150),5)
text = font.render(visited_text,True,visited_color) # informative node
screen.blit(text,text.get_rect(center = (925,150)))
pygame.draw.circle(screen,current_color,(850,175),7)
text = font.render(current_text,True,current_color) # informative node
screen.blit(text,text.get_rect(center = (925,175)))
pygame.display.update()
def display_graph(screen, node_position,graph,
node_color = (0, 0 ,255),node_radius = 5 ,
source = 0, source_color = (0,255,127),source_radius = 8,
target = 1,target_color = (0,255, 0 ),target_radius = 8):
#Springgreen = (0,255,127)
#Green = (0,180,0)
#draw lines (edges)
for node in range(len(graph)):
for neighbour in graph[node]:
pygame.draw.line(screen,(255,255,255), node_position[node], node_position[neighbour],2)
#draw circles (nodes)
for node_number in range(len(graph)): # draw nodes
if node_number == source:
pygame.draw.circle(screen,source_color,node_position[node_number],source_radius)
elif node_number == target:
pygame.draw.circle(screen,target_color,node_position[node_number],target_radius)
else:
pygame.draw.circle(screen, node_color, node_position[node_number], node_radius)
pygame.display.update()
def see_position(screen,position,color = (0,255,0)):
pygame.draw.circle(screen,color,position,5)
pygame.display.update()
if __name__ == "__main__":
pause = True
while True:
# pygame stuff:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit() #exit pygame,
quit() #exit() program
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE: #press breakspace to pause or play
pause = not pause
time.sleep(0.2)
if pause:
continue