Getting the angle between two vectors is well known. But finding the ‘rotation angle’ from one vector to another needs a bit more consideration. The following functions can handle this.

import numpy as np

def calculate_rotation_angle_from_vector_to_vector(a,b):
""" return rotation angle from vector a to vector b, in degrees.

Args:
a : np.array vector. format (x,y)
b : np.array vector. format (x,y)

Returns:
angle [float]: degrees. 0~360
"""

unit_vector_1 = a / np.linalg.norm(a)
unit_vector_2 = b / np.linalg.norm(b)
dot_product = np.dot(unit_vector_1, unit_vector_2)
angle = np.arccos(dot_product)

angle = angle/ np.pi * 180

c = np.cross(b,a)

if c>0:
angle +=180

return angle

This function will use cross product between two vectors to identify if the rotation direction is CW or CCW. This function will return rotation angle in CCW direction in degrees.

Categories: python