Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_euclidean_distance_dict.py
- import tkinter as tk
- dist_dict = {}
- dist_threshold = 50
- def create_dist_dict():
- def p2():
- # point B on canvas
- for y in range(-dist_threshold, dist_threshold + 1):
- for x in range(0, dist_threshold + 1):
- distance = ((x - i) ** 2 + (y - j) ** 2) ** 0.5
- if distance <= dist_threshold:
- d = round(distance, 6)
- dist_dict[(x - i), (y - j)] = d
- else:
- break
- # point A on canvas
- for i in range(0, dist_threshold+1):
- for j in range(0, dist_threshold+1):
- p2()
- def canvas_click(event):
- # Get the clicked coordinates and check if within the threshold
- x, y = event.x, event.y
- try:
- d = dist_dict[(125 - x),(125 - y)]
- print(f" X:{x}, Y:{y} \t is within the distance threshold at {d}")
- except:
- print(f"X:{x}, Y:{y}")
- def mouse_move(event):
- # Get the clicked coordinates and check if they are within the threshold
- x, y = event.x, event.y
- try:
- d = dist_dict[(125 - x),(125 - y)]
- canvas.itemconfig(circle, fill='lime')
- except:
- canvas.itemconfig(circle, fill='red')
- canvas.update()
- # Create the tkinter window and canvas
- root = tk.Tk()
- canvas = tk.Canvas(root, width=250, height=250)
- canvas.pack()
- # Draw the threshold circle
- circle = canvas.create_oval(125-dist_threshold, 125-dist_threshold, 125+dist_threshold, 125+dist_threshold, fill='red')
- # Generate the distance dictionary
- create_dist_dict()
- # Bind mouse clicks on the canvas to the canvas_click function
- canvas.bind("<Button-1>", canvas_click)
- canvas.bind("<Motion>", mouse_move)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement