Advertisement
Guest User

Untitled

a guest
May 19th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.10 KB | None | 0 0
  1. from tkinter import *
  2. coord = [[], []]
  3. triangles = []
  4. SIZE = 3
  5. BSIZE = 5
  6. new_tri_cord = [[-1, -1], [[-1, -1]], [-1, -1]]
  7. resu = [[-1, -1], [-1, -1]]
  8.  
  9. def main():
  10. res_kol = -1
  11. for a in range(len(coord[0]) - 1):
  12. for b in range(a + 1, len(coord[0])):
  13. x1_1, y1_1 = coord[0][a], coord[1][a]
  14. x1_2, y1_2 = coord[0][b], coord[1][b]
  15. print('Введите координаты концов второго отрезка.')
  16. kol = 0
  17. for i in triangles:
  18. res = False
  19. x2_1, y2_1 = i[0][0], i[0][1]
  20. x2_2, y2_2 = i[1][0], i[1][1]
  21. A1 = y1_1 - y1_2
  22. B1 = x1_2 - x1_1
  23. C1 = x1_1*y1_2 - x1_2*y1_1
  24. A2 = y2_1 - y2_2
  25. B2 = x2_2 - x2_1
  26. C2 = x2_1*y2_2 - x2_2*y2_1
  27. if B1*A2 - B2*A1 != 0:
  28. y = (C2*A1 - C1*A2) / (B1*A2 - B2*A1)
  29. x = (-C1 - B1*y) / A1
  30. if min(x1_1, x1_2) <= x <= max(x1_1, x1_2) and min(y1_1, y1_2) <= y <= max(y1_1, y1_2):
  31. print('Точка пересечения отрезков есть, координаты: ({0:f}, {1:f}).'.
  32. format(x, y))
  33. if min(i[0][0], i[1][0]) <= x <= max(i[0][0], i[1][0]) and min(i[0][1], i[1][1]) <= y <= max(i[0][1], i[1][1]):
  34. res = True
  35. else:
  36. print('Точки пересечения отрезков нет.')
  37. if B1*A2 - B2*A1 == 0: print('Точки пересечения отрезков нет, отрезки ||.')
  38.  
  39. x2_1, y2_1 = i[1][0], i[1][1]
  40. x2_2, y2_2 = i[2][0], i[2][1]
  41. A1 = y1_1 - y1_2
  42. B1 = x1_2 - x1_1
  43. C1 = x1_1*y1_2 - x1_2*y1_1
  44. A2 = y2_1 - y2_2
  45. B2 = x2_2 - x2_1
  46. C2 = x2_1*y2_2 - x2_2*y2_1
  47. if B1*A2 - B2*A1 != 0:
  48. y = (C2*A1 - C1*A2) / (B1*A2 - B2*A1)
  49. x = (-C1 - B1*y) / A1
  50. if min(x1_1, x1_2) <= x <= max(x1_1, x1_2) and \
  51. min(y1_1, y1_2) <= y <= max(y1_1, y1_2):
  52. print('Точка пересечения отрезков есть, координаты: ({0:f}, {1:f}).'.
  53. format(x, y))
  54. if min(i[1][0], i[2][0]) <= x <= max(i[1][0], i[2][0]) and min(i[1][1], i[2][1]) <= y <= max(i[1][1], i[2][1]):
  55. res = True
  56. else:
  57. print('Точки пересечения отрезков нет.')
  58. if B1*A2 - B2*A1 == 0: print('Точки пересечения отрезков нет, отрезки ||.')
  59.  
  60. x2_1, y2_1 = i[0][0], i[0][1]
  61. x2_2, y2_2 = i[2][0], i[2][1]
  62. A1 = y1_1 - y1_2
  63. B1 = x1_2 - x1_1
  64. C1 = x1_1*y1_2 - x1_2*y1_1
  65. A2 = y2_1 - y2_2
  66. B2 = x2_2 - x2_1
  67. C2 = x2_1*y2_2 - x2_2*y2_1
  68. if B1*A2 - B2*A1 != 0:
  69. y = (C2*A1 - C1*A2) / (B1*A2 - B2*A1)
  70. x = (-C1 - B1*y) / A1
  71. if min(x1_1, x1_2) <= x <= max(x1_1, x1_2) and \
  72. min(y1_1, y1_2) <= y <= max(y1_1, y1_2):
  73. print('Точка пересечения отрезков есть, координаты: ({0:f}, {1:f}).'.
  74. format(x, y))
  75. if min(i[0][0], i[2][0]) <= x <= max(i[0][0], i[2][0]) and min(i[0][1], i[2][1]) <= y <= max(i[0][1], i[2][1]):
  76. res = True
  77. else:
  78. print('Точки пересечения отрезков нет.')
  79. # случай деления на ноль, то есть параллельность
  80. if B1*A2 - B2*A1 == 0: print('Точки пересечения отрезков нет, отрезки ||.')
  81.  
  82. if res == True:
  83. kol+=1
  84. if kol > res_kol:
  85. res_kol = kol
  86. global resu
  87. print(coord)
  88. resu[0][0], resu[0][1] = coord[0][a], coord[1][a]
  89. resu[1][0], resu[1][1] = coord[0][b], coord[1][b]
  90. print(resu)
  91. canvas.create_line(resu[0][0], resu[0][1], resu[1][0], resu[1][1], fill='green')
  92.  
  93.  
  94. def add_tri():
  95. global triangles
  96. triangles.append(new_tri_cord)
  97. canvas.create_line(new_tri_cord[0][0], new_tri_cord[0][1], new_tri_cord[1][0], new_tri_cord[1][1], fill='yellow')
  98. canvas.create_line(new_tri_cord[1][0], new_tri_cord[1][1], new_tri_cord[2][0], new_tri_cord[2][1], fill='yellow')
  99. canvas.create_line(new_tri_cord[0][0], new_tri_cord[0][1], new_tri_cord[2][0], new_tri_cord[2][1], fill='yellow')
  100.  
  101. def clear():
  102. canvas.delete("all")
  103. global coord
  104. coord = [[], []]
  105. global triangles
  106. triangles = []
  107. global new_tri_cord
  108. new_tri_cord = [[-1, -1], [[-1, -1]], [-1, -1]]
  109.  
  110. def add_from_but():
  111. x = arg_x.get()
  112. y = arg_y.get()
  113. add_point(x, y)
  114.  
  115.  
  116. def add_point(x, y):
  117. if(lang.get() == 1):
  118. coord[0].append(x)
  119. coord[1].append(y)
  120. canvas.create_oval(x - SIZE, y - SIZE, x + SIZE, y + SIZE, fill="red")
  121. else:
  122. mass = []
  123. mass.append(x)
  124. mass.append(y)
  125. global new_tri_cord
  126. new_tri_cord.append(mass)
  127. del new_tri_cord[0]
  128.  
  129.  
  130. def callback(event):
  131. print(event.x, event.y)
  132. add_point(event.x, event.y)
  133.  
  134. root = Tk()
  135. root.title('Solution search')
  136. root["bg"] = "#edeef0"
  137.  
  138. root.geometry("1200x1000")
  139. root.resizable(height=False, width=False)
  140.  
  141. Label(root, font=('Ubuntu', 13), text='Введите новую точку').grid(row=0, column=0, columnspan=2)
  142.  
  143.  
  144. arg_x = IntVar()
  145. arg_y = IntVar()
  146.  
  147. EntryX = Entry(root, textvariable=arg_x, width=5, font='Arial 16').grid(row=1, column=0)
  148. EntryY = Entry(root, textvariable=arg_y, width=5, font='Arial 16').grid(row=1, column=1)
  149.  
  150. add_button = Button(text="Добавить точку", command=add_from_but).grid(row=2, column=0, columnspan=2)
  151. search_button = Button(text="Произвести расчет", command=main).grid(row=6, column=0, columnspan=2)
  152. clean_button = Button(text="Очистить", command=clear).grid(row=16, column=0, columnspan=2)
  153.  
  154. lang = IntVar()
  155.  
  156. point_checkbutton = Radiobutton(text="point", value=1, variable=lang, padx=15, pady=10)
  157. point_checkbutton.grid(row=3, column=0, sticky=W)
  158.  
  159. tri_checkbutton = Radiobutton(text="triangle", value=2, variable=lang, padx=15, pady=10)
  160. tri_checkbutton.grid(row=4, column=0, sticky=W)
  161.  
  162. add_tri = Button(text="Добавить треугольник", command=add_tri).grid(row=5, column=0, columnspan=2)
  163.  
  164. canvas = Canvas(root, width=1000, height=1000, bg='grey')
  165. canvas.grid(row=0, column=2, rowspan=17)
  166.  
  167. canvas.bind("<Button-1>", callback)
  168.  
  169. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement