Posted By

adkatrit on 02/11/11


Tagged

product dot angle Vector


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

adkatrit


calculate angle between two vectors


 / Published in: Python
 

  1. def dotproduct(a,b):
  2. return sum([a[i]*b[i] for i in range(len(a))])
  3.  
  4. from math import acos
  5.  
  6. #Calculates the size of a vector
  7. def veclength(a):
  8. return sum([a[i] for i in range(len(a))])**.5
  9.  
  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  

You need to login to post a comment.