def angle(vector1, vector2):
"""Return the angle between vector1 and vector2 in radians [0, pi]."""
u1 = vector1.normalize()
u2 = vector2.normalize()
cosine = u1 * u2
if cosine == 1.0:
return 0
elif cosine == -1.0:
return math.pi
elif cosine == 0.0:
return math.pi/2
# Unit vectors: sine == sqrt(1.0 ** 2 - cosine ** 2) if angle within [0, pi]
tangent = math.sqrt(1.0 - cosine * cosine) / cosine
return math.atan(tangent)
评论列表
文章目录