-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph_vis.py
258 lines (210 loc) · 7.4 KB
/
graph_vis.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
import pygame
def update_dis(points,lines,screen):
screen.fill((0,0,0))
draw_points(points)
draw_lines(lines)
pygame.display.update()
def draw_points(points):
for p in points:
#print(p)
#print(points)
pygame.draw.circle(screen, (0,150,150), p[0], radius)
pygame.display.update()
def get_n():
global n_vertex
return n_vertex+1
def add_n():
global n_vertex
n_vertex += 1
return n_vertex
def point_with_edge(p):
global lines
pwe = []
for [p1,p2,vxs] in lines:
if p[1] in vxs:
pwe.append([p1,p2,vxs])
if len(pwe) == 0:
return False
else:
return pwe
def remove_edge(line):
global lines
p1,p2 = line[2]
for i,l in enumerate(lines):
if p1 in l[2] and p2 in l[2]:
del lines[i]
def update_edge(line,new_p):
global points
p1 = new_p[0]
p2 = (0,0)
for p in points:
if p[1] in line[2] and p[1] != new_p[1]:
p2 = p[0]
pygame.draw.line(screen, (0,200,0), p1, p2)
remove_edge(line)
lines.append([p1, p2,line[2]])
break
def moving_point(p):
global lines, points,screen
n = p[1]
while True:
#print('bla')
#print(pygame.mouse.get_pressed())
if pygame.mouse.get_pressed() == (0, 0, 1):
print('blabla')
points.remove(p)
new_p = [pygame.mouse.get_pos(),n]
p = new_p
points.append(new_p)
update_dis(points,lines,screen)
#print('dentro')
trash = pygame.event.get()
pygame.event.clear()
edges = point_with_edge(p)
if edges:
print('KKKKKKKKKKK')
for ed in edges:
update_edge(ed,new_p)
else:
pygame.event.clear()
return 0
#break
def draw_lines(lines):
for p1,p2,coord in lines:
#print(lines)
pygame.draw.line(screen, (0,200,0), p1, p2)
'''
def drag_line(p,points,lines):
count = 0
pygame.event.clear()
while True:
count += 1
#print(pygame.mouse.get_pressed())
#if pygame.mouse.get_pressed() == (0, 0, 1):
pygame.draw.line(screen, (0,200,0), p, pygame.mouse.get_pos())
print('l')
if count%10 == 0:
update_dis(points,lines,screen)
pygame.display.update()
#trash = pygame.event.get()
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
(x,y) = pos
for other_p in points:
(px,py) = other_p
if (((x-px)**2 + (y-py)**2)**(1/2)) < radius+5:
pygame.draw.line(screen, (0,200,0), p, other_p)
lines.append([p,other_p])
update_dis(points,lines,screen)
return pos
update_dis(points,lines,screen)
return 0
'''
def drag_line(p):
global lines, points
pygame.event.clear()
count = 0
while True:
#print(pygame.mouse.get_pressed())
if pygame.mouse.get_pressed() == (1, 0, 0):
#print('ckaa')
pygame.event.get()
if count == 10:
update_dis(points,lines,screen)
pygame.draw.line(screen, (0,200,0), p[0], pygame.mouse.get_pos())
count = 0
pygame.display.update()
count += 1
else:
pos = pygame.mouse.get_pos()
(x,y) = pos
for other_p in points:
[(px,py),n] = other_p
if (((x-px)**2 + (y-py)**2)**(1/2)) < radius+5:
#pygame.draw.line(screen, (0,200,0), p, other_p)
sorted_points = sorted([p[1],other_p[1]])
print(([p[0],other_p[0],sorted_points]))
lines.append([p[0],other_p[0],sorted_points])
update_dis(points,lines,screen)
print('entered')
break
update_dis(points,lines,screen)
break
# constants
display_width = 800
display_height = 600
radius = 5 # node size
n_vertex = -1
pygame.init()
screen = pygame.display.set_mode((display_width, display_height))
clock = pygame.time.Clock()
screen.fill((0,0,0)) # param is color tuple
LEFT = 1
RIGHT = 3
count_double = False
points = []
lines = []
running = True
last_click = -2
while running:
for event in pygame.event.get():
# salir
if event.type == pygame.QUIT:
running = False
# create vertex or edge
if event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
now = pygame.time.get_ticks()
pos = pygame.mouse.get_pos()
toque = False
(x,y) = pos
for p in points:
[(px,py),n] = p
# create edge
if (((x-px)**2 + (y-py)**2)**(1/2)) < radius+5:
#print("toqué puntito")
toque = True
#rapid = True
# borrando puntitos (doble click)
'''
double_click = False
if now - last_click <= 500:
double_click == True
last_click = pygame.time.get_ticks()
if now - last_click <= 500: # miliseconds
print(now,last_click)
print('fast')
points.remove(p)
update_dis(points,lines,screen)
'''
#if now - last_click >= 500:
print('línea')
drag_line(p)
#pygame.event.clear()
#rapid = False
#print(pygame.mouse.get_pressed())
#if rapid:
#last_click = pygame.time.get_ticks()
# create vertex
if not toque:
points.append([pos,add_n()])
pygame.draw.circle(screen,
(0,150,150), pos, radius)
pygame.display.update()
#clicked_sprites = [s for s in sprites if s.rect.collidepoint(pos)]
# moving vertex
if event.type == pygame.MOUSEBUTTONDOWN and event.button == RIGHT:
for p in points:
[(px,py),n] = p
(x,y) = pygame.mouse.get_pos()
if (((x-px)**2 + (y-py)**2)**(1/2)) < radius:
print('aksdjfalfjalsk') ## ¡Por alguna razón entra aquí y se cicla si al mover un punto dejo el mouse sobre él al soltar el click!
new_p = pygame.mouse.get_pos()
#p = new_p
moving_point(p)
pygame.event.clear()
#print(last_click)
# clickear puntitos
print(points)
print(lines)
#print(clicked_sprites)