Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_dist_degrees.py
- import tkinter as tk
- from PIL import Image, ImageTk, ImageDraw, ImageFont
- import math
- WW, HH = 640, 640
- cx, cy = WW // 2, HH // 2
- root = tk.Tk()
- root.geometry('+0+0')
- canvas = tk.Canvas(root, width=WW, height=HH)
- canvas.pack()
- current_mouse_x, current_mouse_y = cx, cy
- def get_degrees(x, y):
- dx = x - cx
- dy = y - cy
- degrees = math.degrees(math.atan2(-dy, dx))
- if degrees < 0:
- degrees += 360
- return degrees
- def get_distance_from_center(x, y):
- dx = x - cx
- dy = y - cy
- distance = math.sqrt(dx * dx + dy * dy)
- return distance
- def on_drag(event):
- global current_mouse_x, current_mouse_y
- current_mouse_x, current_mouse_y = event.x, event.y
- def on_motion(event):
- global current_mouse_x, current_mouse_y
- current_mouse_x, current_mouse_y = event.x, event.y
- canvas.bind("<B1-Motion>", on_drag)
- canvas.bind("<Motion>", on_motion)
- while 1:
- canvas.delete('all')
- degrees = get_degrees(current_mouse_x, current_mouse_y)
- distance = get_distance_from_center(current_mouse_x, current_mouse_y)
- arm_angle = math.radians(degrees)
- arm_end_x = cx + distance * math.cos(arm_angle)
- arm_end_y = cy - distance * math.sin(arm_angle) # negative because canvas y is flipped
- canvas.create_line(cx, cy, arm_end_x, arm_end_y, fill='blue', width=3)
- canvas.create_oval(cx-5, cy-5, cx+5, cy+5, fill='red', outline='red') # center dot
- canvas.create_text((20, 20), text=f"{degrees:.0f}ยฐ", fill='red', anchor='nw')
- canvas.create_text((20, 40), text=f"Distance: {distance:.0f}", fill='green', anchor='nw')
- root.update()
Advertisement
Add Comment
Please, Sign In to add comment