So, I have been using the common mathematics formula till now: cos theta = a.b/|a|*|b|. Implemented on python with the following bit of code
vector=[point2[0]-point1[0],point2[1]-point1[1]]
y_axis = [0, 1]
unit_vector = vector / np.linalg.norm(vector)
unit_y = y_axis / np.linalg.norm(y_axis)
dot_product = np.dot(unit_vector, unit_y)
angle = numpy.arccos(dot_product)
I found that this does not consistently provide the proper angle.
Is there any alternative/better way to do achieve this using just numpy?
CodePudding user response:
I think the problem might be the fact that your angle is not always computed clockwise. That is, if the x-coordinate of "vector" is negative, then arccos() will give a result that effectively computes the angle in the counter-clockwise sense. In the code below, I also expressed the result in degrees.
import numpy as np
vector = [-0.25, -0.25]
y_axis = [0, 1]
unit_vector = vector / np.linalg.norm(vector)
unit_y = y_axis / np.linalg.norm(y_axis)
dot_product = np.dot(unit_vector, unit_y)
angle = np.arccos(dot_product)
AngleInDegrees = 360*angle/(2*np.pi);
if vector[0]<0:
AngleInDegrees = 360-AngleInDegrees
print(AngleInDegrees)