Reputation: 51
I have a triangle with three vertices (x,y,z)
triangle = [[ 0. 0. 0.], [ 1. 0. 0.], [ 0.5 0.866 0. ]]
I used the answer from link but somehow I don't get the right angles.
def angle(triangle):
def unit_vector(vector):
print(vector)
""" Returns the unit vector of the vector. """
return vector / np.linalg.norm(vector)
def angle_between(v1, v2):
""" Returns the angle in radians between vectors 'v1' and 'v2'::
>>> angle_between((1, 0, 0), (0, 1, 0))
1.5707963267948966
>>> angle_between((1, 0, 0), (1, 0, 0))
0.0
>>> angle_between((1, 0, 0), (-1, 0, 0))
3.141592653589793
"""
v1_u = unit_vector(v1)
v2_u = unit_vector(v2)
return np.arccos(np.clip(np.dot(v1_u, v2_u), -1.0, 1.0))
v1, v2, v3 = triangle[0], triangle[1], triangle[2]
vec1, vec2, vec3 = v2-v1, v3-v2, v3-v1
a1, a2, a3 = angle_between(vec1,vec2),angle_between(vec2,vec3),angle_between(vec3,vec1)
print(a1,a2,a3)
2.09440780623 1.04722295888 1.04718484736
The sum of the angles are above 3.1416rad what is not possible.
Where is the problem?
Upvotes: 0
Views: 248
Reputation: 46921
you need to get the direction of your vectors right:
angle_between(vec1,-vec2) # note the sign here
if the sign of the vector is wrong, you do not get the inner angle of your triangle at this corner (but 'pi - the angle'). drawing a sketch will surely clarify this.
Upvotes: 2