-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKoch_graph.py
170 lines (124 loc) · 4.46 KB
/
Koch_graph.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
from dataclasses import dataclass
from subprocess import check_output
from PIL import Image, ImageTk
import tkinter as tk
WIDTH = 500
HEIGHT = 500
@dataclass
class Coords2D:
x: float
y: float
class Graph:
root: tk
graph: tk.Canvas
start: Coords2D
goal: Coords2D
count: int
option: str
points = []
def __init__(self, root, start, goal, count, option):
self.root = root
self.start = start
self.goal = goal
self.count = count
self.option = option
self.set_points()
def set_points(self):
self.points = eval(check_output(
[str(self.option),
str(self.start.x), str(self.start.y),
str(self.goal.x), str(self.goal.y),
str(self.count)],
shell=True).decode('utf-8')
)
def inc_count(self, event):
max_count = 6 if self.option == "bin\\Koch_snowflake.exe" else 5
self.count += 1 if event.keysym == "Up" and self.count < max_count else \
-1 if event.keysym == "Down" and self.count > 0 \
else 0
self.clear()
self.set_points()
self.draw()
def zoom(self, event):
scale = 1.1 if event.delta > 0 else 0.9
self.graph.scale('all', event.x, event.y, scale, scale)
def draw(self):
self.graph = tk.Canvas(self.root, width=WIDTH, height=HEIGHT)
for i in range(len(self.points) - 1):
self.graph.create_line(self.points[i].x, self.points[i].y, self.points[i + 1].x, self.points[i + 1].y)
self.graph.pack()
def clear(self):
self.graph.destroy()
def init_window(root):
root.title("Koch graph")
root.withdraw()
root.wm_iconphoto(False, ImageTk.PhotoImage(Image.open('icon.ico')))
root.minsize(240, 200)
root.resizable(False, False)
def get_values_frame(root):
frame = tk.Frame(root)
frame.pack()
start_x = tk.Label(frame, text=f"Start x: ")
start_x.grid(row=0, column=0, padx=5, pady=5, sticky=tk.E)
start_y = tk.Label(frame, text=f"Start y: ")
start_y.grid(row=1, column=0, padx=5, pady=5, sticky=tk.E)
goal_x = tk.Label(frame, text=f"Goal x: ")
goal_x.grid(row=2, column=0, padx=5, pady=5, sticky=tk.E)
goal_y = tk.Label(frame, text=f"Goal y: ")
goal_y.grid(row=3, column=0, padx=5, pady=5, sticky=tk.E)
count_label = tk.Label(frame, text=f"Count: ")
count_label.grid(row=4, column=0, padx=5, pady=5, sticky=tk.E)
entries = []
for i in range(5):
entry = tk.Entry(frame)
entry.grid(row=i, column=1, padx=5, pady=5, sticky=tk.W)
entries.append(entry)
entries[0].insert(0, "50")
entries[1].insert(0, "350")
entries[2].insert(0, "450")
entries[3].insert(0, "350")
entries[4].insert(0, "5")
selected_option = tk.StringVar()
options_list = ["Koch Curve", "Koch Snowflake"]
strings_list = ["bin\\Koch_curve.exe", "bin\\Koch_snowflake.exe"]
option_value_dict = {option: value for option, value in zip(options_list, strings_list)}
selected_option.set(options_list[0])
frame_submit = tk.Frame(root)
frame_submit.pack()
dropdown = tk.OptionMenu(frame_submit, selected_option, *options_list)
dropdown.grid(row=0, column=0, padx=5, pady=5, sticky=tk.W)
button = tk.Button(frame_submit, text="Submit", command=lambda: create_graph())
button.grid(row=0, column=1, padx=5, pady=5, sticky=tk.E)
def create_graph():
def get_user_input():
values = []
for entry in entries:
try:
value = int(entry.get())
values.append(value)
except ValueError:
entry.delete(0, tk.END)
return False
return values
if not get_user_input():
return
root.withdraw()
user_input = get_user_input()
frame.destroy()
frame_submit.destroy()
start = Coords2D(user_input[0], user_input[1])
goal = Coords2D(user_input[2], user_input[3])
count = user_input[4] - 1
graph = Graph(root, start, goal, count, option_value_dict[selected_option.get()])
graph.draw()
root.bind("<MouseWheel>", graph.zoom)
root.bind("<Key>", graph.inc_count)
root.deiconify()
root.deiconify()
def main():
root = tk.Tk()
init_window(root)
get_values_frame(root)
root.mainloop()
if __name__ == "__main__":
main()