-
Notifications
You must be signed in to change notification settings - Fork 1
/
p3_interactive.py
91 lines (62 loc) · 2.01 KB
/
p3_interactive.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
import sys
import random
import pickle
import traceback
import Tkinter
import p3_pathfinder
if len(sys.argv) != 4:
print "usage: %s map.gif map.mesh.pickle subsample_factor" % sys.argv[0]
sys.exit(-1)
_, MAP_FILENAME, MESH_FILENAME, SUBSAMPLE = sys.argv
SUBSAMPLE = int(SUBSAMPLE)
with open(MESH_FILENAME, 'rb') as f:
mesh = pickle.load(f)
master = Tkinter.Tk()
big_image = Tkinter.PhotoImage(file=MAP_FILENAME)
small_image = big_image.subsample(SUBSAMPLE,SUBSAMPLE)
BIG_WIDTH, BIG_HEIGHT = big_image.width(), big_image.height()
SMALL_WIDTH, SMALL_HEIGHT = small_image.width(), small_image.height()
canvas = Tkinter.Canvas(master, width=SMALL_WIDTH, height=SMALL_HEIGHT)
canvas.pack()
def shrink(values):
return [v/SUBSAMPLE for v in values]
source_point = None
destination_point = None
visited_boxes = []
path = []
def redraw():
canvas.delete(Tkinter.ALL)
canvas.create_image((0,0), anchor=Tkinter.NW, image=small_image)
for box in visited_boxes:
x1,x2,y1,y2 = shrink(box)
canvas.create_rectangle(y1,x1,y2,x2,outline='pink')
for segment in path:
x1,y1 = shrink(segment[0])
x2,y2 = shrink(segment[1])
canvas.create_line(y1,x1,y2,x2,width=2.0,fill='red')
if source_point:
x,y = shrink(source_point)
canvas.create_oval(y-5,x-5,y+5,x+5,width=2,outline='red')
if destination_point:
x,y = shrink(destination_point)
canvas.create_oval(y-5,x-5,y+5,x+5,width=2,outline='red')
def on_click(event):
global source_point, destination_point, visited_boxes, path
if source_point and destination_point:
source_point = None
destination_point = None
visited_boxes = []
path = []
elif not source_point:
source_point = event.y*SUBSAMPLE, event.x*SUBSAMPLE
else:
destination_point = event.y*SUBSAMPLE, event.x*SUBSAMPLE
try:
path, visited_boxes = p3_pathfinder.find_path(source_point, destination_point, mesh)
except:
destination_point = None
traceback.print_exc()
redraw()
canvas.bind('<Button-1>', on_click)
redraw()
master.mainloop()