Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- coord = [[], []]
- triangles = []
- SIZE = 3
- BSIZE = 5
- new_tri_cord = [[-1, -1], [[-1, -1]], [-1, -1]]
- resu = [[-1, -1], [-1, -1]]
- def main():
- res_kol = -1
- for a in range(len(coord[0]) - 1):
- for b in range(a + 1, len(coord[0])):
- x1_1, y1_1 = coord[0][a], coord[1][a]
- x1_2, y1_2 = coord[0][b], coord[1][b]
- print('Введите координаты концов второго отрезка.')
- kol = 0
- for i in triangles:
- res = False
- x2_1, y2_1 = i[0][0], i[0][1]
- x2_2, y2_2 = i[1][0], i[1][1]
- A1 = y1_1 - y1_2
- B1 = x1_2 - x1_1
- C1 = x1_1*y1_2 - x1_2*y1_1
- A2 = y2_1 - y2_2
- B2 = x2_2 - x2_1
- C2 = x2_1*y2_2 - x2_2*y2_1
- if B1*A2 - B2*A1 != 0:
- y = (C2*A1 - C1*A2) / (B1*A2 - B2*A1)
- x = (-C1 - B1*y) / A1
- if min(x1_1, x1_2) <= x <= max(x1_1, x1_2) and min(y1_1, y1_2) <= y <= max(y1_1, y1_2):
- print('Точка пересечения отрезков есть, координаты: ({0:f}, {1:f}).'.
- format(x, y))
- 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]):
- res = True
- else:
- print('Точки пересечения отрезков нет.')
- if B1*A2 - B2*A1 == 0: print('Точки пересечения отрезков нет, отрезки ||.')
- x2_1, y2_1 = i[1][0], i[1][1]
- x2_2, y2_2 = i[2][0], i[2][1]
- A1 = y1_1 - y1_2
- B1 = x1_2 - x1_1
- C1 = x1_1*y1_2 - x1_2*y1_1
- A2 = y2_1 - y2_2
- B2 = x2_2 - x2_1
- C2 = x2_1*y2_2 - x2_2*y2_1
- if B1*A2 - B2*A1 != 0:
- y = (C2*A1 - C1*A2) / (B1*A2 - B2*A1)
- x = (-C1 - B1*y) / A1
- if min(x1_1, x1_2) <= x <= max(x1_1, x1_2) and \
- min(y1_1, y1_2) <= y <= max(y1_1, y1_2):
- print('Точка пересечения отрезков есть, координаты: ({0:f}, {1:f}).'.
- format(x, y))
- 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]):
- res = True
- else:
- print('Точки пересечения отрезков нет.')
- if B1*A2 - B2*A1 == 0: print('Точки пересечения отрезков нет, отрезки ||.')
- x2_1, y2_1 = i[0][0], i[0][1]
- x2_2, y2_2 = i[2][0], i[2][1]
- A1 = y1_1 - y1_2
- B1 = x1_2 - x1_1
- C1 = x1_1*y1_2 - x1_2*y1_1
- A2 = y2_1 - y2_2
- B2 = x2_2 - x2_1
- C2 = x2_1*y2_2 - x2_2*y2_1
- if B1*A2 - B2*A1 != 0:
- y = (C2*A1 - C1*A2) / (B1*A2 - B2*A1)
- x = (-C1 - B1*y) / A1
- if min(x1_1, x1_2) <= x <= max(x1_1, x1_2) and \
- min(y1_1, y1_2) <= y <= max(y1_1, y1_2):
- print('Точка пересечения отрезков есть, координаты: ({0:f}, {1:f}).'.
- format(x, y))
- 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]):
- res = True
- else:
- print('Точки пересечения отрезков нет.')
- # случай деления на ноль, то есть параллельность
- if B1*A2 - B2*A1 == 0: print('Точки пересечения отрезков нет, отрезки ||.')
- if res == True:
- kol+=1
- if kol > res_kol:
- res_kol = kol
- global resu
- print(coord)
- resu[0][0], resu[0][1] = coord[0][a], coord[1][a]
- resu[1][0], resu[1][1] = coord[0][b], coord[1][b]
- print(resu)
- canvas.create_line(resu[0][0], resu[0][1], resu[1][0], resu[1][1], fill='green')
- def add_tri():
- global triangles
- triangles.append(new_tri_cord)
- 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')
- 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')
- 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')
- def clear():
- canvas.delete("all")
- global coord
- coord = [[], []]
- global triangles
- triangles = []
- global new_tri_cord
- new_tri_cord = [[-1, -1], [[-1, -1]], [-1, -1]]
- def add_from_but():
- x = arg_x.get()
- y = arg_y.get()
- add_point(x, y)
- def add_point(x, y):
- if(lang.get() == 1):
- coord[0].append(x)
- coord[1].append(y)
- canvas.create_oval(x - SIZE, y - SIZE, x + SIZE, y + SIZE, fill="red")
- else:
- mass = []
- mass.append(x)
- mass.append(y)
- global new_tri_cord
- new_tri_cord.append(mass)
- del new_tri_cord[0]
- def callback(event):
- print(event.x, event.y)
- add_point(event.x, event.y)
- root = Tk()
- root.title('Solution search')
- root["bg"] = "#edeef0"
- root.geometry("1200x1000")
- root.resizable(height=False, width=False)
- Label(root, font=('Ubuntu', 13), text='Введите новую точку').grid(row=0, column=0, columnspan=2)
- arg_x = IntVar()
- arg_y = IntVar()
- EntryX = Entry(root, textvariable=arg_x, width=5, font='Arial 16').grid(row=1, column=0)
- EntryY = Entry(root, textvariable=arg_y, width=5, font='Arial 16').grid(row=1, column=1)
- add_button = Button(text="Добавить точку", command=add_from_but).grid(row=2, column=0, columnspan=2)
- search_button = Button(text="Произвести расчет", command=main).grid(row=6, column=0, columnspan=2)
- clean_button = Button(text="Очистить", command=clear).grid(row=16, column=0, columnspan=2)
- lang = IntVar()
- point_checkbutton = Radiobutton(text="point", value=1, variable=lang, padx=15, pady=10)
- point_checkbutton.grid(row=3, column=0, sticky=W)
- tri_checkbutton = Radiobutton(text="triangle", value=2, variable=lang, padx=15, pady=10)
- tri_checkbutton.grid(row=4, column=0, sticky=W)
- add_tri = Button(text="Добавить треугольник", command=add_tri).grid(row=5, column=0, columnspan=2)
- canvas = Canvas(root, width=1000, height=1000, bg='grey')
- canvas.grid(row=0, column=2, rowspan=17)
- canvas.bind("<Button-1>", callback)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement