Guest User

Untitled

a guest
Mar 29th, 2020
107
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from math import sqrt
  2. from tkinter import *
  3. def mdp(E):
  4.     D=[]
  5.     x1=E[0][0]
  6.     x2=E[len(E)-1][0]
  7.     y1=E[0][1]
  8.     y2=E[len(E)-1][1]
  9.     for i in range(1, len(E)-1,1):
  10.         dist=abs((y2-y1)*E[i][0]-(x2-x1)*E[i][1]+x2*y1-y2*x1)/\
  11.              (sqrt((y2-y1)**2+(x2-x1)**2))
  12.         #print(dist)
  13.         D.append((dist,E[i][0],E[i][1],i))
  14.     D.sort(reverse=1)
  15.     return D[0]
  16.  
  17. def new_line(N):
  18.     for i in range (len(N)-1):
  19.         canvas.create_line(N[i][0], N[i][1], N[i+1][0], N[i+1][1], width=5, fill='red')
  20. def click(click_event):
  21.     global prev
  22.     try:
  23.         canvas.create_line(prev.x, prev.y, click_event.x, click_event.y, width=2, fill='green')
  24.         S.append((click_event.x,click_event.y))
  25.     except:
  26.         prev = click_event
  27.         S.append((click_event.x,click_event.y))
  28.         return
  29.     prev = click_event
  30. def action(K):
  31.     print('iter')
  32.     epsilon = int(eps.get())
  33.     print(N)
  34.     #print(mdp(K)[0])
  35.     if len(K)<=2:
  36.         for i in K:
  37.             if i not in N:
  38.                 N.append(i)
  39.                 print('add 2 points')
  40.         new_line(N)
  41.     elif mdp(K)[0]<epsilon:
  42.         pass
  43.     else:
  44.         N.append(mdp(K)[1:3])
  45.         action(K[:mdp(K)[3]])
  46.         action(K[mdp(K)[3]:])
  47.  
  48. master = Tk()
  49. canvas = Canvas(master, width=600, height=300)
  50. canvas.pack(padx=20, pady=20)
  51. canvas.bind('<Button-1>', click)
  52. but1=Button(master,text='show',command=lambda K=S: action(S));but1.pack()
  53. eps=Entry(master,width=5);eps.pack()
  54.  
  55. S=[]
  56. N=[]
  57.  
  58. mainloop()
RAW Paste Data