Advertisement
here2share

# tk_euclidean_distance_dict.py

Apr 11th, 2023
607
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.56 KB | None | 0 0
  1. # tk_euclidean_distance_dict.py
  2.  
  3. import tkinter as tk
  4.  
  5. dist_dict = {}
  6. dist_threshold = 50
  7. def create_dist_dict():    
  8.     def p2():
  9.         # point B on canvas
  10.         for y in range(-dist_threshold, dist_threshold + 1):
  11.             for x in range(0, dist_threshold + 1):
  12.                 distance = ((x - i) ** 2 + (y - j) ** 2) ** 0.5
  13.                 if distance <= dist_threshold:
  14.                     d = round(distance, 6)
  15.                     dist_dict[(x - i), (y - j)] = d
  16.                 else:
  17.                     break
  18.    
  19.     # point A on canvas
  20.     for i in range(0, dist_threshold+1):
  21.         for j in range(0, dist_threshold+1):
  22.             p2()
  23.  
  24. def canvas_click(event):
  25.     # Get the clicked coordinates and check if within the threshold
  26.     x, y = event.x, event.y
  27.     try:
  28.         d = dist_dict[(125 - x),(125 - y)]
  29.         print(f" X:{x}, Y:{y} \t is within the distance threshold at {d}")
  30.     except:
  31.         print(f"X:{x}, Y:{y}")
  32.  
  33. def mouse_move(event):
  34.     # Get the clicked coordinates and check if they are within the threshold
  35.     x, y = event.x, event.y
  36.     try:
  37.         d = dist_dict[(125 - x),(125 - y)]
  38.         canvas.itemconfig(circle, fill='lime')
  39.     except:
  40.         canvas.itemconfig(circle, fill='red')
  41.     canvas.update()
  42.  
  43. # Create the tkinter window and canvas
  44. root = tk.Tk()
  45. canvas = tk.Canvas(root, width=250, height=250)
  46. canvas.pack()
  47.  
  48. # Draw the threshold circle
  49. circle = canvas.create_oval(125-dist_threshold, 125-dist_threshold, 125+dist_threshold, 125+dist_threshold, fill='red')
  50.  
  51. # Generate the distance dictionary
  52. create_dist_dict()
  53.  
  54. # Bind mouse clicks on the canvas to the canvas_click function
  55. canvas.bind("<Button-1>", canvas_click)
  56. canvas.bind("<Motion>", mouse_move)
  57.  
  58. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement