-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrafos y algoritmos.py
358 lines (293 loc) · 10.4 KB
/
grafos y algoritmos.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
import pygame, sys, PYC
from pygame.constants import MOUSEBUTTONUP
from win32api import GetSystemMetrics
import time, os
SIZE = (int(GetSystemMetrics(0)*0.9),int(GetSystemMetrics(1)*0.9))
pygame.init()
pygame.font.init()
# Colores
NEGRO = (1, 0, 0)
BLANCO = (255, 255, 255)
ROJO = (255, 0, 0)
VERDE = (0, 255, 0)
AZUL = (0, 0, 255)
CELESTE =(50, 50, 200)
MORADO = (200, 0, 200)
AMARILLO = (255, 255, 0)
FONDO = (0, 0, 10)
GRIS = (20, 20, 20)
# Definir cosas importantes
ventana = pygame.display.set_mode(SIZE)
clock = pygame.time.Clock()
# Funcion para esperar x segundos
# Clases de un nodo y un grafo
class Nodo():
# Creacion de un nodo basico
# Tienes conexiones con otros nodos
# Y un color tanto exterior como interior(no es diferente de un borde el exterior)
def __init__(self, pos, name):
self.connects = []
self.mark = False
self.pos = pos
self.size = SIZE[1]*0.043
self.width = SIZE[1]*0.05
self.color_ext = BLANCO
self.color_in = GRIS
self.color_label = BLANCO
self.name = name
self.dibujarNodo()
def marcar(self, tiempo = 0):
self.color_in = BLANCO
self.color_label = NEGRO
self.mark = True
def desmarcar(self):
self.color_label = BLANCO
self.color_in = GRIS
self.mark = False
def conectar(self, nodo):
if nodo not in self.connects:
self.connects.append(nodo)
def desconectar(self, nodo):
#desconecta el nodo de otro dado
if nodo in self.connects:
self.connects.remove(nodo)
def dibujarNodo(self):
# Dibujo del nodo con todos sus componentes
self.size = SIZE[1]*0.043
self.draw_in = pygame.draw.circle(ventana, self.color_in, self.pos, self.size)
self.draw_ext = pygame.draw.circle(ventana, self.color_ext, self.pos, self.size, 5)
self.font = pygame.font.SysFont('Arial',40)
self.label = self.font.render(f'{self.name}', True, self.color_label)
if self.name < 10:
posTextX = self.pos[0]-8
else:
posTextX = self.pos[0]-18
posTextY = self.pos[1]-25
ventana.blit(self.label, (posTextX, posTextY))
def dibujarConexiones(self, color=BLANCO):
for nodo in self.connects:
pygame.draw.line(ventana, color , self.pos, nodo.pos, 5)
def cambiarNombre(self,nombre):
self.name = nombre
def aislar(self):
for nodoAd in self.connects:
nodoAd.connects.remove(self)
self.connects = []
def focus(self):
self.color_ext = MORADO
def unfocus(self):
self.color_ext = BLANCO
def isfocus(self):
if self.color_ext == BLANCO:
return False
else:
return True
class Grafo():
# Modos:
# 0 crear Nodo
# 1 conectar Nodo
# 2 eliminar Nodo
# 3 MOVER NODO (al final porque me da miedo hacerla)
'''
como el programa de python tenia el buffer activado no podia poner tiempos de espera
en una misma funcion por lo que cree una cola de animacion donde en cada frame anima
un nodo distinto
'''
def __init__(self):
self.nodos = []
self.modos = ["Crear", "Conectar", "Eliminar", "Mover"]
self.conectando = []
self.modoE = 0
self.pila = PYC.Pila()
self.cola = PYC.Cola()
self.colaAnimacion = PYC.Cola()
self.nodoIni = -1
self.move = False
self.animando = False
def agregarNodo(self, pos):
nodo = Nodo(pos, len(self.nodos))
self.nodos.append(nodo)
def quitarNodo(self,nodo):
if (nodo in self.nodos):
for nodoConnect in nodo.connects:
nodoConnect.connects.remove(nodo)
self.nodos.remove(nodo)
i = 0
for nodo in self.nodos:
nodo.cambiarNombre(i)
i +=1
def verificarMarcado(self,pos):
nodo = self.buscarNodoMouse(pos)
if nodo != -1:
if self.nodoIni != -1:
self.nodoIni.desmarcar()
nodo.marcar()
self.nodoIni = nodo
def buscarNodoMouse(self, pos):
for nodo in self.nodos:
collideCircle = nodo.draw_in
if collideCircle.collidepoint(pos):
return nodo
return -1
def cambiarModo(self, select):
if self.modoE == 1:
if len(self.conectando) == 1:
self.conectando[0].unfocus()
self.conectando = []
if select:
self.modoE += 1
else:
self.modoE -= 1
self.modoE = self.modoE % 4
def conectarNodos(self, nodo1, nodo2):
if nodo1 in self.nodos and nodo2 in self.nodos:
nodo1.conectar(nodo2)
nodo2.conectar(nodo1)
def accionMouse(self, pos):
if self.modoE == 0:
self.agregarNodo(pos)
elif self.modoE < 5:
nodo = self.buscarNodoMouse(pos)
if nodo != -1:
if self.modoE == 1:
if len(self.conectando) == 0:
self.conectando.append(nodo)
nodo.focus()
elif len(self.conectando) == 1 and nodo != self.conectando[0]:
self.conectando.append(nodo)
self.conectarNodos(self.conectando[0], nodo)
self.conectarNodos(nodo, self.conectando[0])
self.conectando[0].unfocus()
self.conectando = []
if self.modoE == 2:
self.quitarNodo(nodo)
if self.modoE == 3:
nodo.marcar()
self.move = nodo
def animar(self, tiempo):
if not self.colaAnimacion.vacia():
self.animando = True
variablesAnimation = self.colaAnimacion.obtener()
nodo = variablesAnimation[0]
mark = variablesAnimation[1]
focus = variablesAnimation[2]
if focus != -1:
if focus:
nodo.focus()
else:
nodo.unfocus()
if mark != -1:
if mark:
nodo.marcar()
else:
nodo.desmarcar()
time.sleep(tiempo)
else:
self.animando = False
def recorridoProfundidad(self, nodo, marks):
self.focusAnimation(nodo)
marks.append(nodo)
self.pila.insertar(nodo)
self.markAnimation(nodo)
for nodoAd in nodo.connects:
if nodoAd not in marks:
self.unfocusAnimation(nodo)
self.recorridoProfundidad(nodoAd, marks)
self.focusAnimation(nodo)
self.unfocusAnimation(nodo)
self.pila.eliminar()
def recorridoAncho(self, nodo):
self.cola.ingresar(nodo)
marks = []
while not self.cola.vacia():
nodoActual = self.cola.obtener()
self.focusAnimation(nodoActual)
self.markAnimation(nodoActual)
for nodoAd in nodoActual.connects:
if nodoAd not in marks:
marks.append(nodoAd)
self.cola.ingresar(nodoAd)
self.unfocusAnimation(nodoActual)
def limpiar(self):
self.nodos = []
def eliminarConexiones(self):
for nodo in self.nodos:
nodo.aislar()
def quitarMove(self):
if self.move != self.nodoIni:
self.move.desmarcar()
self.move = False
def desmarcarTodo(self):
self.nodoIni = -1
for nodo in self.nodos:
nodo.desmarcar()
nodo.unfocus()
def markAnimation(self, nodo):
self.colaAnimacion.ingresar((nodo, 1, -1))
def focusAnimation(self, nodo):
self.colaAnimacion.ingresar((nodo, -1, 1))
def unmarkAnimation(self, nodo):
self.colaAnimacion.ingresar((nodo, 0, -1))
def unfocusAnimation(self, nodo):
self.colaAnimacion.ingresar((nodo, -1, 0))
def actualizar(self):
self.font = pygame.font.SysFont('Arial',40)
labelModo = self.font.render(
f'Modo escogido: {self.modos[self.modoE]} Nodo',
True,
BLANCO
)
ventana.blit(labelModo, (0,0))
labelIns = self.font.render(
"DFS: P ,BFS: O , limpiar:Space",
True,
BLANCO
)
ventana.blit(labelIns, (0,SIZE[1]-45))
if self.move:
self.move.pos = pygame.mouse.get_pos()
for nodo in self.nodos:
nodo.dibujarConexiones()
for nodo in self.nodos:
nodo.dibujarNodo()
self.animar(0.1)
grafo = Grafo()
while True:
posMouse = pygame.mouse.get_pos()
for evento in pygame.event.get():
if evento.type == pygame.QUIT:
sys.exit(0)
if evento.type == pygame.MOUSEBUTTONDOWN:
if not grafo.animando:
if evento.button == 1:
grafo.accionMouse(posMouse)
if evento.button == 3:
grafo.verificarMarcado(posMouse)
if evento.type == MOUSEBUTTONUP:
if evento.button == 1 and grafo.move:
grafo.quitarMove()
if evento.button == 4:
grafo.cambiarModo(1)
if evento.button == 5:
grafo.cambiarModo(0)
if evento.type == pygame.KEYDOWN:
if not grafo.animando:
if evento.key == pygame.K_UP or evento.key == pygame.K_d:
grafo.cambiarModo(1)
if evento.key == pygame.K_DOWN or evento.key == pygame.K_a:
grafo.cambiarModo(0)
if evento.key == pygame.K_SPACE:
grafo.limpiar()
if evento.key == pygame.K_c:
grafo.eliminarConexiones()
if evento.key == pygame.K_x:
grafo.desmarcarTodo()
if grafo.nodoIni != -1:
if evento.key == pygame.K_p:
grafo.recorridoProfundidad(grafo.nodoIni,[])
if evento.key == pygame.K_o:
grafo.recorridoAncho(grafo.nodoIni)
ventana.fill(FONDO)
grafo.actualizar()
pygame.display.flip()
clock.tick(120)