-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualizer.py
148 lines (110 loc) · 4.84 KB
/
visualizer.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
from Tkinter import *
from tkFileDialog import *
#CHANGE ZOOM TO Fraction!!!!
class Vis:
def __init__(self, h=500, w=500, points=[],deltazoom=0.1,t=1,zoom=1.0,pic_button=False,paper_side=BOTTOM):
self.root=Tk()
self.paper=Canvas(self.root,background="white",
height=h,
width=w)
print self.paper['width'], self.paper['height']
self.center=[0,0]
self.points=points
self.zoom=zoom
self.h=h
self.w=w
self.t=t
self.deltazoom=deltazoom
self.drawnpoints=[]
self.paper.bind("<Button-1>", self.leftclick)
self.paper.bind("<ButtonRelease-1>", self.release)
self.paper.bind("<Double-Button-1>", self.doubleclickleft)
self.paper.bind("<Button-3>",self.rightclick)
self.paper.bind("<Button-5>",self.zoomout)
self.paper.bind("<Button-4>",self.zoomin)
self.drawPoints()
if pic_button:
frame=Frame(self.root)
frame.pack(side=TOP)
if pic_button:
photocam=PhotoImage(file="icons/camera.gif")
picboton=Button(frame,image=photocam,command=self.take_picture)
picboton.pack(side=LEFT)
picboton.image = photocam
self.paper.pack(fill=BOTH,expand=YES,side=paper_side)
def drawPoints(self):
self.destroypoints()
self.centers=[]
for p in self.points:
x=p[0]-self.center[0]
y=-(p[1]-self.center[1])
x=x*self.zoom
y=y*self.zoom
x=x+self.paper.winfo_width()/2
y=y+self.paper.winfo_height()/2
#change the implementation of colored pointsets! so that
# say p[2]='red' (this implies some rewriting on other modules
self.centers.append([x,y])
if len(p)<=2:
self.drawnpoints.append(self.paper.create_oval(x,y,x+self.t,y+self.t,fill="black"))
else:
if p[2]==0:
self.drawnpoints.append(self.paper.create_oval(x,y,x+self.t,y+self.t,
outline="red",fill="red"))
if p[2]==1:
self.drawnpoints.append(self.paper.create_oval(x,y,x+self.t,y+self.t,
outline="blue",fill="blue"))
if p[2]==2:
self.drawnpoints.append(self.paper.create_oval(x,y,x+self.t,y+self.t,
outline="green",fill="green"))
self.root.update()
def moveCenter(self,d):
self.center[0]=self.center[0]+(d[0]/self.zoom)
self.center[1]=self.center[1]+(d[1]/self.zoom)
#self.drawPoints()
for p in self.drawnpoints:
self.paper.move(p,-d[0],d[1])
def setzoom(self,zoom):
self.zoom=zoom
self.drawPoints()
def destroypoints(self):
for p in self.drawnpoints:
self.paper.delete(p)
def leftclick(self, event):
global start
start=[int(self.paper.canvasx(event.x)),int(self.paper.canvasy(event.y))]
def doublezoomin(self):
self.setzoom(2*self.zoom)
def doublezoomout(self):
self.setzoom(self.zoom/2)
def rightclick(self,event):
self.doublezoomout()
def doubleclickleft(self,event):
start=[int(self.paper.canvasx(event.x)),int(self.paper.canvasy(event.y))]
end=[self.paper.winfo_width()/2,self.paper.winfo_height()/2]
v=[-(end[0]-start[0]),(end[1]-start[1])]
self.moveCenter([v[0],v[1]])
self.doublezoomin()
def release(self,event):
global end
end=[int(self.paper.canvasx(event.x)),int(self.paper.canvasy(event.y))]
v=[-(end[0]-start[0]),(end[1]-start[1])]
self.moveCenter([v[0],v[1]])
def zoomout(self,event):
if self.zoom >= 1.0:
self.setzoom(self.zoom-self.deltazoom)
else:
zoominv=1/self.zoom
newzoominv=zoominv+self.deltazoom*zoominv
self.setzoom(1/(newzoominv))
def zoomin(self,event):
if self.zoom >= 1.0:
self.setzoom(self.zoom+self.deltazoom*self.zoom)
else:
zoominv=1/self.zoom
newzoominv=zoominv-self.deltazoom*zoominv
self.setzoom(1/(newzoominv))
def take_picture(self):
"""Saves the current view as an ps file"""
filename=asksaveasfilename(filetypes=[("postscript","*.ps *.eps")],defaultextension=".ps")
self.paper.postscript(file=filename)