Как сослаться на конкретную точку отсчета при нахождении угла между тремя точками?
Ниже приведенный код исправно показывает угол между красной и оранжевой линиями, т.к. получает координаты двух крайних точек и ссылается на координаты 0, 0 как на "среднюю" точку. Нужно заставить показывать угол между синей и зеленой линиями, для этого необходимо вместо 0, 0 ссылаться на "среднюю" точку 200, 200, и вот тут непонятно, как это сделать.
В поисковике много инфы со сложными объяснениями и километровыми формулами нахождения угла между тремя точками, но в моём случае угол уже считается, проблема только в принудительном назначении точки отсчета, решение которой найти не удалось
from tkinter import *
import math
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
root = Tk()
root.geometry(f"{400}x{400}+{600}+{250}")
canvas = Canvas(root, height=400, width=400)
canvas.pack()
xm = 0
ym = 0
def reaction():
root.bind('<Motion>', move)
def move(event):
global xm, ym
xm = event.x
ym = event.y
crt_ln()
def crt_ln():
x1 = 200
y1 = 200
x2 = xm
y2 = ym
canvas.delete("ld1")
canvas.delete("ld2")
canvas.create_line(x1, y1, x2, y2, fill='green', tags='ld1')
canvas.create_line(0, 0, x2, y2, fill='orange', tags='ld2')
v1 = Vector(400, 200)
v2 = Vector(x2, y2)
v1_theta = math.atan2(v1.y, v1.x)
v2_theta = math.atan2(v2.y, v2.x)
r = (v2_theta - v1_theta) * (180.0 / math.pi)
if r < 0:
r % 360
print(int(r))
canvas.create_line(200, 200, 400, 200, fill='blue')
canvas.create_line(0, 0, 400, 200, fill='red')
reaction()
root.mainloop()
Ответы (1 шт):
Вам необходимо нормализовать вектор. Вектор АВ состоящий из двух точек, например А(3, 7) и В(4, 2) находиться по формуле AB(Xb-Xa, Yb-Ya) и будет равен AB(4-3, 2-7) = AB(1, -5)
from tkinter import *
import math
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
root = Tk()
root.geometry(f"{400}x{400}+{600}+{250}")
canvas = Canvas(root, height=400, width=400)
canvas.pack()
def reaction():
root.bind('<Motion>', move)
def move(event):
crt_ln(event.x, event.y)
def crt_ln(xm, ym):
x1 = y1 = 200
red_vector = Vector(400-0, 200-0)
blue_vector = Vector(400-200, 200-200)
green_vector = Vector(xm-x1, ym-y1)
orange_vector = Vector(xm-0, ym-0)
canvas.delete("ld1")
canvas.delete("ld2")
canvas.create_line(x1, y1, xm, ym, fill='green', tags='ld1')
canvas.create_line(0, 0, xm, ym, fill='orange', tags='ld2')
red_theta = math.atan2(red_vector.y, red_vector.x)
blue_theta = math.atan2(blue_vector.y, blue_vector.x)
green_theta = math.atan2(green_vector.y, green_vector.x)
orange_theta = math.atan2(orange_vector.y, orange_vector.x)
grad_red_and_orange_vector = math.degrees(orange_theta - red_theta)
grad_blue_and_green_vector = math.degrees(blue_theta - green_theta)
print(f"alfa = {int(grad_red_and_orange_vector)}, beta = {int(grad_blue_and_green_vector)}")
canvas.create_line(200, 200, 400, 200, fill='blue')
canvas.create_line(0, 0, 400, 200, fill='red')
reaction()
root.mainloop()
