I tried to calculate the angle ABC between points A, B and C. I know the math are pretty basic but I don't understand why my function give the wrong result. First, here is the code (a
contains a list [x, y, z]
)
def angle(a, b, c):
# Create vectors from points
ba = [a[0] - b[0], a[1] - b[1], a[2] - b[2]]
bc = [c[0] - b[0], c[1] - b[1], c[2] - b[2]]
# Normalize vector
nba = sqrt(ba[0]**2 + ba[1]**2 + ba[2]**2)
ba = [ba[0]/nba, ba[1]/nba, ba[2]/nba]
nbc = sqrt(bc[0]**2 + bc[1]**2 + bc[2]**2)
bc = [bc[0]/nbc, bc[1]/nbc, bc[2]/nbc]
# Calculate scalar from normalized vectors
scal = ba[0]*bc[0] + ba[1]*bc[1] + ba[2]*bc[2]
# calculate the angle in radian
angle = acos(scal)
This function gives wrong result. In fact, it gives the good result if I change the second vector from bc
to cb
:
cb = [b[0]-c[0], b[1]-c[1], b[2]-c[2]]
I don't understand why, as if I follow math, my first solution should work well and give the good result...