geometry.py

changeset 31
02e7e1d73ebb
parent 30
0d9ca37901ed
child 38
66c9591b733d
equal deleted inserted replaced
30:0d9ca37901ed 31:02e7e1d73ebb
391 cosine = dot_product(vec_1, vec_2) 391 cosine = dot_product(vec_1, vec_2)
392 try: 392 try:
393 cosine /= vec_1.length() * vec_2.length() 393 cosine /= vec_1.length() * vec_2.length()
394 except ZeroDivisionError: 394 except ZeroDivisionError:
395 return 0 395 return 0
396 # Fix the cosine, it can go outside bounds due to rounding errors,
397 # e.g. 1.0000000000000002, which then causes a math domain error.
398 cosine = min(max(-1, cosine), 1)
396 angle = acos(cosine) 399 angle = acos(cosine)
397 if normalized and angle > π / 2: 400 if normalized and angle > π / 2:
398 angle = π - angle 401 angle = π - angle
399 return angle 402 return angle
400 403

mercurial