calculate angle between two vectors

/ Published in: Python
Save to your folder(s)

Copy this code and paste it in your HTML
  1. def dotproduct(a,b):
  2. return sum([a[i]*b[i] for i in range(len(a))])
  4. from math import acos
  6. #Calculates the size of a vector
  7. def veclength(a):
  8. return sum([a[i] for i in range(len(a))])**.5
  10. #Calculates the angle between two vector
  11. def ange(a,b):
  12. dp=dotproduct(a,b)
  13. la=veclength(a)
  14. lb=veclength(b)
  15. costheta=dp/(la*lb)
  16. return acos(costheta)

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.