-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.py
More file actions
237 lines (179 loc) · 6.26 KB
/
Copy pathgui.py
File metadata and controls
237 lines (179 loc) · 6.26 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
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
import tkinter as tk
import threading
import time
from convexhull import ConvexHull2D
from display import Display, DisplayJarvis, DisplayGraham
class GUI(tk.Frame):
def __init__(self):
self.root = tk.Tk()
#super().__init__()
self.master = self.root
self.canvas = self.initializeUserInterface()
self.setupCanvasObjectContainer()
self.delay = 200 # in miliseconds
self.cvx = ConvexHull2D(n=50, delay=self.delay,
max_x=self.w-200, max_y=self.h-200)
# varible used to prematurely stop algorithms
self.killthread = False
#self.executeJarvis()
#self.executeGraham()
self.root.mainloop()
def setupCanvasObjectContainer(self):
'''Method to setup container for
canvas elements
'''
self.cvsobjects = {
'points': {},
'r_lines': {},
'circles': {},
'texts': {},
'lines': {},
}
def clearCanvas(self):
'''Method to clear canvas of any elements
'''
self.setupCanvasObjectContainer()
self.canvas.delete('all')
'''
for objtype in self.cvsobjects.keys():
for elementkey in self.cvsobjects[objtype].keys():
self.canvas.delete(
self.cvsobjects[objtype][elementkey]
)
'''
def generateNewPoints(self):
'''Method to generate new points
'''
# clear all canvas
self.clearCanvas()
try:
count = int(self.inputCount.get())
self.cvx = ConvexHull2D(n=count, delay=self.delay,
max_x=self.w-200, max_y=self.h-200)
except ValueError:
return
def initializeUserInterface(self):
'''
self.master.title("Convex Hull 2D")
self.pack(fill=tk.BOTH, expand=1)
canvas = tk.Canvas(self)
#canvas.grid(column=0, row=0,
# sticky=tk.W+tk.N)
canvas.pack(fill=tk.BOTH, expand=1)
'''
self.w, self.h = self.root.winfo_screenwidth(), \
self.root.winfo_screenheight()
self.root.geometry("%dx%d+0+0" % (self.w, self.h))
canvas = tk.Canvas(self.root,
width=self.w-200, height=self.h-200,
borderwidth=2, relief='sunken')
canvas.grid(column=0, row=0,
rowspan=10, sticky=tk.W)
label = tk.Label(self.root,
text="ENTER COUNT \nOF POINTS \nTO RANDOMLY \nGENERATE")
label.grid(column=1, row=0)
self.inputCount = tk.StringVar()
self.inputCount.set('50')
entryPointCount = tk.Entry(self.root,
textvariable=self.inputCount)
entryPointCount.grid(column=1, row=1,
sticky=tk.W+tk.E)
buttonSubmitInput = tk.Button(self.root,
text='Generate New Points',
command=self.generateNewPoints)
buttonSubmitInput.grid(column=1, row=2,
rowspan=1,
sticky=tk.W+tk.N+tk.E+tk.S)
buttonDispAll = tk.Button(self.root,
text='Display All Points',
command=self.displayPoints)
buttonDispAll.grid(column=1, row=3,
rowspan=1,
sticky=tk.W+tk.N+tk.E+tk.S)
buttonExJarvis = tk.Button(self.root,
text='Execute Jarvis',
command=self.executeJarvis)
buttonExJarvis.grid(column=1, row=4,
rowspan=1,
sticky=tk.W+tk.N+tk.E+tk.S)
buttonExGraham = tk.Button(self.root,
text='Execute Graham',
command=self.executeGraham)
buttonExGraham.grid(column=1, row=5,
rowspan=1,
sticky=tk.W+tk.N+tk.E+tk.S)
buttonStopAlgo = tk.Button(self.root,
text='Stop Algorithm',
command=self.interruptAlgorithm)
buttonStopAlgo.grid(column=1,row=6,
rowspan=1,
sticky=tk.W+tk.N+tk.E+tk.S)
return canvas
def displayPoints(self):
if isinstance(self.cvx, ConvexHull2D):
display = Display(self)
display.displayAllPoints()
del display
def interruptAlgorithm(self):
'''Method to kill thread running
algorithms prematurely
'''
self.killthread = True
def updateCanvas(self, display, algo='jarvis'):
start = time.time()
if self.killthread:
self.cvx.stopalgo = True
self.clearCanvas()
return
if algo == 'jarvis':
display.displayJarvis()
if not self.threadjarvis.isAlive():
# thread is no longer alive
display.postAlgorithmStep()
return
elif algo == 'graham':
display.displayGraham()
if not self.threadgraham.isAlive():
#self.postGrahamStep()
display.postAlgorithmStep()
return
delta = time.time() - start
if delta > self.delay/1000:
print(delta)
self.master.after(self.delay,
self.updateCanvas, display, algo
)
def executeJarvis(self):
self.clearCanvas()
display = DisplayJarvis(self)
# if previously set True, set False
self.killthread = False
# create a thread for jarvis
self.threadjarvis = threading.Thread(
target=self.cvx.runJarvis,
args=(True,)
)
self.threadjarvis.daemon = True
self.threadjarvis.start()
# allowing jarvis to be setup
time.sleep(0.005)
# call to update canvas
self.updateCanvas(display, algo='jarvis')
def executeGraham(self):
self.clearCanvas()
display = DisplayGraham(self)
# if previously set True, set False
self.killthread = False
# create thread for Graham
self.threadgraham = threading.Thread(
target=self.cvx.runGraham,
args=(True,)
)
self.threadgraham.daemon = True
self.threadgraham.start()
# allowing graham to be setup
time.sleep(0.005)
#call to update canvas
self.updateCanvas(display, algo='graham')
if __name__ == '__main__':
gui = GUI()