Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  1. from tkinter import *
  2. from tkinter import messagebox as mb
  3.  
  4. CANVAS_HEIGHT = 500
  5. CANVAS_WIDTH = 500
  6.  
  7. points = list()
  8. counter = 0
  9. scale_const_y = 1
  10. scale_const_x = 1
  11.  
  12. def fill_header():
  13.  
  14. Label(info_frame, text='#', relief=SUNKEN, width=5).grid(row=0, column=0)
  15. Label(info_frame, text='X', relief=SUNKEN, width=5).grid(row=0, column=1)
  16. Label(info_frame, text='Y', relief=SUNKEN, width=5).grid(row=0, column=2)
  17.  
  18. def add_point():
  19.  
  20. global counter
  21. changed = False
  22. x = entry_x.get()
  23. y = entry_y.get()
  24.  
  25. try:
  26. float(x), float(y)
  27.  
  28. except ValueError:
  29. mb.messagebox("Неверный тип координат точек.")
  30. return -1
  31.  
  32. for i in range(counter):
  33. if points[i][0] == float(x) and points[i][1] == float(y):
  34. mb.showerror("Предупреждение", "Эта точка уже была введена.")
  35. return -2
  36.  
  37. fill_table_row(counter, x, y)
  38. points.append([float(x), float(y)])
  39.  
  40. max_x = points[0][0]
  41. max_y = points[0][1]
  42.  
  43. for point in points[1:]:
  44.  
  45. max_x = max_x if max_x > abs(point[0]) else abs(point[0])
  46. max_y = max_y if max_y > abs(point[1]) else abs(point[1])
  47.  
  48. max_x = max(max_x, CANVAS_WIDTH)
  49. max_y = max(max_y, CANVAS_HEIGHT)
  50.  
  51. if float(x) > max_x:
  52. scale_coords(x=x / max_x)
  53. changed = True
  54.  
  55. if float(y) > max_y:
  56. scale_coords(y=y / max_y)
  57. changed = True
  58.  
  59. if changed:
  60. redraw_canvas()
  61.  
  62. new_coords = transform_coordinates(float(x), float(y))
  63. canvas.create_oval(new_coords, new_coords[0] + 2, new_coords[1] + 2, fill='green')
  64.  
  65. counter += 1
  66.  
  67. def redraw_canvas():
  68.  
  69. canvas.delete("all")
  70. for point in points:
  71. canvas.create_oval(point, point)
  72.  
  73. def find_solution():
  74.  
  75. if len(points) < 3:
  76. mb.showerror("Недостаточно точек для построения решения.")
  77. return -1
  78.  
  79. delta_min = len(points)
  80. line = None
  81.  
  82. for i in range(len(points)):
  83. for j in range(len(points)):
  84. delta = 0
  85. if i != j:
  86. a = points[i][1] - points[j][1]
  87. b = points[j][0] - points[i][0]
  88. c = points[i][0] * points[j][1] - points[j][0] * points[i][1]
  89. for k in range(len(points)):
  90. if k != j and k != i:
  91. if a * points[k][0] + b * points[k][1] + c > 0: delta += 1
  92. elif a * points[k][0] + b * points[k][1] + c < 0: delta -= 1
  93.  
  94.  
  95. if abs(delta) < delta_min:
  96. delta_min = abs(delta)
  97.  
  98. first_point = (- b * CANVAS_WIDTH / 2 - c) / a if a != 0 else points[i][0]
  99. second_point = ( b * CANVAS_WIDTH / 2 - c) / a if a != 0 else points[j][0]
  100.  
  101. line = [transform_coordinates(first_point, CANVAS_HEIGHT / 2),
  102. transform_coordinates(second_point, - CANVAS_HEIGHT / 2)]
  103.  
  104. canvas.delete('line')
  105. canvas.create_line(line, fill='blue', tag='line')
  106.  
  107. def transform_coordinates(x, y):
  108.  
  109. x = x + CANVAS_WIDTH // 2
  110. y = -y + CANVAS_HEIGHT // 2
  111.  
  112. return [x, y]
  113.  
  114. def scale_coords(scale_x=scale_const_x, scale_y=scale_const_y):
  115.  
  116. global points
  117. scale_const_x = scale_x
  118. scale_const_y = scale_y
  119.  
  120. for point in points:
  121. points[0] *= scale_x
  122. points[1] *= scale_y
  123.  
  124. def fill_table_row(counter, a, b):
  125.  
  126. cols = 3
  127. table_row = list()
  128. table_row.append(Label(info_frame,
  129. text=str(counter + 1),
  130. relief=SUNKEN,
  131. width=5))
  132.  
  133. table_row.append(Label(info_frame,
  134. text=a,
  135. relief=SUNKEN,
  136. width=5))
  137.  
  138. table_row.append(Label(info_frame,
  139. text=b,
  140. relief=SUNKEN,
  141. width=5))
  142.  
  143. for j in range(cols):
  144. table_row[j].grid(row=counter + 1, column=j)
  145.  
  146.  
  147. window = Tk()
  148. info_frame = Frame(window)
  149. interface_frame = Frame(window)
  150. canvas = Canvas(window, width=CANVAS_WIDTH, height=CANVAS_HEIGHT)
  151.  
  152. btn_solute = Button(interface_frame,
  153. text="Найти решение:",
  154. width=15,
  155. command=find_solution).pack()
  156.  
  157. btn_points = Button(interface_frame,
  158. text="Добавить точку:",
  159. width=15,
  160. command=add_point).pack()
  161.  
  162. upper_frame = Frame(interface_frame)
  163. bottom_frame = Frame(interface_frame)
  164.  
  165. label_x = Label(upper_frame, width=6, text='X')
  166. label_y = Label(upper_frame, width=6, text='Y')
  167. entry_x = Entry(bottom_frame, width=6)
  168. entry_y = Entry(bottom_frame, width=6)
  169.  
  170. label_x.pack(side=LEFT)
  171. label_y.pack(side=RIGHT)
  172. entry_x.pack(side=LEFT)
  173. entry_y.pack(side=RIGHT)
  174. upper_frame.pack()
  175. bottom_frame.pack()
  176.  
  177. interface_frame.pack(side=LEFT)
  178. canvas.pack(side = RIGHT)
  179. info_frame.pack(side=LEFT)
  180.  
  181. fill_header()
  182.  
  183. window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement