# Untitled

a guest
Mar 28th, 2020
131
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. from math import sqrt
2. from tkinter import *
3.
4. def click(click_event):
5.     global prev
6.     try:
7.         canvas.create_line(prev.x, prev.y, click_event.x, click_event.y, width=2, fill='green')
8.         S.append(click_event)
9.     except:
10.         prev = click_event
11.         S.append(click_event)
12.         return
13.     prev = click_event
14. def distance(a, b):
15.     return sqrt((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2)
16. def point_line_distance(point, start, end):
17.     if (start == end):
18.         return distance(point, start)
19.     else:
20.         n = abs(
21.             (end[0] - start[0]) * (start[1] - point[1]) -
22.             (start[0] - point[0]) * (end[1] - start[1])
23.         )
24.         d = sqrt(
25.             (end[0] - start[0]) ** 2 + (end[1] - start[1]) ** 2
26.         )
27.         return n / d
28. def rdp(points, epsilon):
29.     # &quot;&quot;&quot;Reduces a series of points to a simplified version that loses detail, but
30.     # maintains the general shape of the series.
31.     #  Сводит ряд точек к упрощенной версии, которая теряет детализацию, но
32.     # сохраняет общую форму серии.
33.     # &quot;&quot;&quot;
34.     dmax = 0.0
35.     index = 0
36.     for i in range(1, len(points) - 1):
37.         d = point_line_distance(points[i], points[0], points[-1])
38.         if d > dmax:
39.             index = i
40.             dmax = d
41.     if dmax>= epsilon:
42.         results = rdp(points[:index + 1], epsilon)[:-1] + rdp(points[index:], epsilon)
43.     else:
44.         results = [points[0], points[-1]]
45.     return results
46. def move(move_event):
47.     global prev
48.     canvas.create_line(prev.x, prev.y, move_event.x, move_event.y, width=2,  fill='green')
49.   #  canvas.create_line( rdp(points[:index + 1], epsilon=0.5), width=2)
50.     rdp(prev[prev.x], 0,5)[:-1] + rdp(prev[prev.y], 0,5)
51.     prev = move_event  # what does this do ?
52. def action():
53.     print(S)
54. master = Tk()
55. canvas = Canvas(master, width=600, height=300)