Apr-15-2020, 10:22 AM
Hello everyone!
Tell me, please - how can I determine the angle between the bot and the click? I wrote this in the program, but if the click is on the left and higher relative to the bot, the angle is negative. How to calculate the angle in the range from 0 to 360 degrees?
Tell me, please - how can I determine the angle between the bot and the click? I wrote this in the program, but if the click is on the left and higher relative to the bot, the angle is negative. How to calculate the angle in the range from 0 to 360 degrees?
import tkinter as tk from tkinter import* import math root = tk.Tk() root.geometry('500x500+2+10') def target(event): bot_center_x = canvas1.coords(bot)[0] + 15 bot_center_y = canvas1.coords(bot)[1] + 15 clik_x = event.x clik_y = event.y #print('bot: '+str((int(bot_center_x), int(bot_center_y)))) #print('clik: '+str((clik_x, clik_y))) angle(bot_center_x, bot_center_y, clik_x, clik_y) def angle(bot_center_x, bot_center_y, clik_x, clik_y): angle_radian = math.atan2(clik_y - bot_center_y, clik_x - bot_center_x) angle_gradus = math.degrees(angle_radian) print('angle_radian: '+str(round(angle_radian, 2))) print('angle_gradus: '+str(round(angle_gradus, 2))) print() canvas1 = Canvas(root, width = 500, height = 500) canvas1.place(x = 0, y = 0) bot = canvas1.create_oval(200, 200, 230, 230) root.bind('<Button-1>', target) root.mainloop()