Skip to content

Instantly share code, notes, and snippets.

@uhho
Created August 29, 2018 00:10
Show Gist options
  • Save uhho/cbe5f6e7a1ba036093fe6d968c08033b to your computer and use it in GitHub Desktop.
Save uhho/cbe5f6e7a1ba036093fe6d968c08033b to your computer and use it in GitHub Desktop.
Calculate angle between two vectors
import numpy as np
def unit_vector(vector):
return vector / np.linalg.norm(vector)
def angle_between(v1, v2):
v1_u = unit_vector(v1)
v2_u = unit_vector(v2)
return np.arccos(np.clip(np.dot(v1_u, v2_u), -1.0, 1.0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment