-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
435 lines (348 loc) · 15.4 KB
/
main.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
import tkinter as tk
from enum import Enum
class State(Enum):
MOVE = 1
DRAW_LINE = 2
class App(tk.Tk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.frame = tk.Frame(self)
self.frame.pack()
self.state = State.MOVE
self.button1 = tk.Button(self.frame, text="move", state=["disabled"], command=self.set_moving)
self.button2 = tk.Button(self.frame, text="draw line", command=self.set_drawingline)
self.button1.pack()
self.button2.pack()
canva = CanvasSpline(self, width=400, height=400, bg='white')
canva.pack()
def set_moving(self):
self.state = State.MOVE
self.button1['state'] = 'disabled'
self.button2['state'] = 'normal'
def set_drawingline(self):
self.state = State.DRAW_LINE
self.button1['state'] = 'normal'
self.button2['state'] = 'disabled'
class CanvasSpline(tk.Canvas):
bezier_curve_n = 10 #Amount of base lines in single bezier curve
def __init__(self, master, *args, **kwargs):
super().__init__(master, *args, **kwargs)
self.bezier_curves_count = 0
self.master = master
self.Points = []
self.line_mapping = dict()
self.highlighted_point = None
self.dragging = False
self.start_point = None
self.bind('<Button-1>', self.line_start)
self.bind('<Button-1>', self.add_point, add="+")
self.bind('<ButtonRelease-1>', self.line_end)
def move_point(self, event):
if self.dragging and self.master.state == State.MOVE:
init_coords = self.ellipse_to_point(self.start_point)
if self.highlighted_point is not None and self.start_point == self.highlighted_point:
self.move(self.start_point, event.x - init_coords[0], event.y - init_coords[1])
point = self.get_point_by_id(self.start_point)
point.x, point.y = event.x, event.y
startstart_point = 0
endend_point = 0
for line in self.line_mapping:
line_tuple = self.line_mapping[line]
if self.start_point in line_tuple:
self.update_bezier_line(line_tuple[0], line_tuple[1], recalculate_anchors= True)
if line_tuple[0] == self.start_point:
endend_point = line_tuple[1]
else:
startstart_point = line_tuple[0]
for line in self.line_mapping:
line_tuple = self.line_mapping[line]
if endend_point in line_tuple and endend_point == line_tuple[0] or \
startstart_point in line_tuple and startstart_point == line_tuple[1]:
self.update_bezier_line(line_tuple[0], line_tuple[1])
def line_start(self, event):
overlapped = self.find_overlapping(event.x - 5, event.y - 5, event.x + 5, event.y + 5)
if len(overlapped) > 0:
for item in overlapped:
if "base_point" in self.gettags(item):
self.dragging = True
print("user holding button!")
self.start_point = item
def update_bezier_line(self, p0, p1, recalculate_anchors = False):
bezier_tag = 0
if isinstance(p0, int):
start_point = self.get_point_by_id(p0)
else:
start_point = p0
if isinstance(p1, int):
end_point = self.get_point_by_id(p1)
else:
end_point = p1
if start_point is not None and end_point is not None:
for key, value in self.line_mapping.items():
if (start_point.id, end_point.id) == value:
#self.line_mapping.pop(key)
bezier_tag = key
self.delete(key)
if recalculate_anchors:
start_point.inbound_bezier_anchor, start_point.outbound_bezier_anchor = calculate_bezier_anchors(
self.get_point_by_id(start_point.inbound_line_point),
start_point,
end_point
)
end_point.inbound_bezier_anchor, end_point.outbound_bezier_anchor = calculate_bezier_anchors(
start_point,
end_point,
self.get_point_by_id(end_point.outbound_line_point)
)
curve = BezierCurve(self.bezier_curve_n,
start_point,
start_point.outbound_bezier_anchor,
end_point.inbound_bezier_anchor,
end_point)
curve_points = curve.get_all_points()
for i in range(0, len(curve_points) - 1):
self.create_base_line(curve_points[i], curve_points[i+1], [bezier_tag])
def create_bezier_line(self, start_point_id, end_point_id):
start_point = None
end_point = None
for point in self.Points:
if point == start_point_id:
start_point = point
point.outbound_line_point = end_point_id
if point == end_point_id:
end_point = point
point.inbound_line_point = start_point_id
startstart_point = self.get_point_by_id(start_point.inbound_line_point)
endend_point = self.get_point_by_id(end_point.outbound_line_point)
start_point.inbound_bezier_anchor, start_point.outbound_bezier_anchor = calculate_bezier_anchors(
startstart_point,
start_point,
end_point
)
end_point.inbound_bezier_anchor, end_point.outbound_bezier_anchor = calculate_bezier_anchors(
start_point,
end_point,
endend_point
)
curve = BezierCurve(self.bezier_curve_n,
start_point,
start_point.outbound_bezier_anchor,
end_point.inbound_bezier_anchor,
end_point)
curve_points = curve.get_all_points()
bezier_tag = f"bezier{self.bezier_curves_count}"
self.bezier_curves_count += 1
for i in range(0, len(curve_points) - 1):
self.create_base_line(curve_points[i], curve_points[i+1], [bezier_tag])
self.update_bezier_line(startstart_point, start_point)
self.update_bezier_line(end_point, endend_point)
return bezier_tag
def create_base_line(self, p0, p1, tags):
if isinstance(p0, int):
start_coords = self.ellipse_to_point(p0)
else:
start_coords = p0
if isinstance(p1, int):
end_coords = self.ellipse_to_point(p1)
else:
end_coords = p1
line_id = self.create_line(start_coords[0], start_coords[1], end_coords[0], end_coords[1], tags=["base_line", *tags])
print(f"created line {line_id}!")
return line_id
def update_base_line(self, start_point_id, end_point_id, line_id):
start_coords = self.ellipse_to_point(start_point_id)
end_coords = self.ellipse_to_point(end_point_id)
self.coords(line_id, (start_coords[0], start_coords[1], end_coords[0], end_coords[1]))
def line_end(self, event):
if self.dragging:
print("user stopped holding a button!")
self.dragging = False
if self.master.state == State.DRAW_LINE:
overlapped = self.find_overlapping(event.x - 5, event.y - 5, event.x + 5, event.y + 5)
if len(overlapped) > 0:
for item in overlapped:
if "base_point" in self.gettags(item):
self.highlight_point(event)
if self.points_not_in_use(self.start_point, item) and self.start_point != item:
line_id = self.create_bezier_line(self.start_point, item)
print("spawned line!")
self.line_mapping[line_id] = (self.start_point, item)
def get_point_by_id(self, id):
try:
return self.Points[self.Points.index(id)]
except ValueError:
return None
def points_not_in_use(self, start_point_id, end_point_id):
start_point = self.get_point_by_id(start_point_id)
end_point = self.get_point_by_id(end_point_id)
return \
(start_point_id, end_point_id) not in self.line_mapping \
and (end_point_id, start_point_id) not in self.line_mapping \
and start_point.outbound_line_point is None \
and end_point.inbound_line_point is None
def add_point(self, event):
overlapped = self.find_overlapping(event.x - 5, event.y - 5, event.x + 5, event.y + 5)
if len(overlapped) <= 0 and self.dragging == False:
id = self.create_oval(event.x - 5, event.y - 5, event.x + 5, event.y + 5, fill='black', tags=["base_point"])
self.tag_bind(id, '<Button-2>', self.del_point)
self.tag_bind(id, '<Button-3>', self.del_point)
self.tag_bind(id, '<Motion>', self.move_point)
self.tag_bind(id, '<Button-1>', self.highlight_point)
self.Points.append(Point(event.x, event.y, id))
def del_point(self, event):
id = self.find_closest(event.x, event.y)
self.delete(id[0])
if id[0] == self.highlighted_point:
self.highlighted_point = None
try:
i = self.Points.index(id[0])
point = self.Points[i]
if point.inbound_line_point is not None:
for key, value in self.line_mapping.items():
if (point.inbound_line_point, id[0]) == value:
self.line_mapping.pop(key)
self.delete(key)
print(f"deleted line {key}!")
inbound_point = self.get_point_by_id(point.inbound_line_point)
inbound_point.outbound_line_point = None
point.inbound_line_point = None
break
if point.outbound_line_point is not None:
for key, value in self.line_mapping.items():
if (id[0], point.outbound_line_point) == value:
self.line_mapping.pop(key)
self.delete(key)
print(f"deleted line {key}!")
outbound_point = self.get_point_by_id(point.outbound_line_point)
outbound_point.inbound_line_point = None
point.outbound_line_point = None
break
print(f"removed point {id[0]}!")
self.Points.remove(id[0])
except ValueError:
pass
def highlight_point(self, event):
id = self.find_closest(event.x, event.y)
if "base_point" in self.gettags(id):
if self.highlighted_point is not None:
self.itemconfig(self.highlighted_point, outline='black', width=0)
self.highlighted_point = id[0]
self.itemconfig(self.highlighted_point, outline='green', width=2)
def ellipse_to_point(self, ellipse_id):
coords = self.coords(ellipse_id)
return (coords[2] + coords[0]) / 2, (coords[3] + coords[1]) / 2
class Point:
def __init__(self, x, y, id, points = None):
self.x, self.y = x, y
self.id = id
self.points = points
self.inbound_line_point = None
self.outbound_line_point = None
self.inbound_bezier_anchor = None
self.outbound_bezier_anchor = None
def __getitem__(self, item):
if item == 0:
return self.x
elif item == 1:
return self.y
else:
raise IndexError()
def __setattr__(self, name, value):
if name == "inbound_line_point":
pass
#calculate_bezier_anchors(self.p0, self, None)
self.__dict__[name] = value
return self.__dict__[name]
def __iter__(self):
yield self.x
yield self.y
def __add__(self, other):
if isinstance(other, Point) or isinstance(other, tuple):
return Point(self.x + other[0], self.y + other[1], self.id)
raise TypeError(f"unsupported operand type(s) for +: {type(self)} and {type(other)}")
def __sub__(self, other):
if isinstance(other, Point) or isinstance(other, tuple):
return Point(self.x - other[0], self.y - other[1], self.id)
raise TypeError(f"unsupported operand type(s) for +: {type(self)} and {type(other)}")
def __truediv__(self, other):
return Point(self.x / other, self.y / other, self.id)
def __mul__(self, other):
return Point(self.x * other, self.y * other, self.id)
def __str__(self):
return f"({self.x}, {self.y})"
def __eq__(self, other):
if isinstance(other, Point):
return self.x == other.x and self.y == other.y
if isinstance(other, int):
return self.id == other
@staticmethod
def to_screen(point, width, height):
"""
:rtype: tuple
"""
x_screen = point[0] + width/2
y_screen = height/2 - point[1]
return x_screen, y_screen
@staticmethod
def to_cartesian(point, width, height):
"""
:rtype: tuple
"""
x_cart = width/2 - point[0]
y_cart = height/2 - point[1]
return x_cart, y_cart
@staticmethod
def get_length_between_points(p0, p1):
return ((p0.x - p1.x) ** 2 + (p0.y - p1.y) ** 2) ** .5
class BezierCurve:
def __init__(self, n, p0 = None, p1 = None, p2 = None, p3 = None):
self.p0, self.p1, self.p2, self.p3 = p0, p1, p2, p3
self.curr_t = 0.0
self.step = 1.0/n
p0x3 = self.p0 * 3
p1x3 = self.p1 * 3
p2x3 = self.p2 * 3
self.pow3arg = self.p3 - p2x3 + p1x3 - self.p0
self.pow2arg = p0x3 - p1x3 * 2 + p2x3
self.pow1arg = p1x3 - p0x3
def get_next_point(self):
point = None
if self.curr_t < 1.0:
point = self.pow3arg * self.curr_t ** 3 + self.pow2arg * self.curr_t ** 2 + self.pow1arg * self.curr_t + self.p0
self.curr_t += self.step
return point
def get_all_points(self):
points = []
t = 0.0
while t <= 1.0:
point = self.pow3arg * t ** 3 + self.pow2arg * t ** 2 + self.pow1arg * t + self.p0
points.append(point)
t += self.step
return points
def calculate_bezier_anchors(p0: Point, p1: Point, p2: Point):
if p0 is None:
p0 = p1
elif p2 is None:
p2 = p1
a0 = (p0 + p1) / 2
a1 = (p1 + p2) / 2
l1 = Point.get_length_between_points(p0, p1)
l2 = Point.get_length_between_points(p1, p2)
b = a0 * (l1 / (l1 + l2)) + a1 * (l2 / (l1 + l2))
diff = p1 - b
return a0 + diff, a1 + diff
def main():
points = []
a = Point(1, 1, 0, points)
points.append(a)
b = Point(3, 3, 1, points)
points.append(b)
c = Point(5, 5, 2, points)
points.append(c)
p0, p1 = calculate_bezier_anchors(None, b, c)
print(str(p0) + " " + str(p1))
print(tuple(a))
root = App()
root.mainloop()
if __name__ == '__main__':
main()