Fix angle_between_vectors, add rotation_between_vectors

This commit is contained in:
Grant Sanderson
2021-11-16 17:08:35 -08:00
parent d065e1973d
commit 82bd02d21f

View File

@ -12,6 +12,14 @@ from manimlib.constants import TAU
from manimlib.utils.iterables import adjacent_pairs
def cross(v1, v2):
return [
v1[1] * v2[2] - v1[2] * v2[1],
v1[2] * v2[0] - v1[0] * v2[2],
v1[0] * v2[1] - v1[1] * v2[0]
]
def get_norm(vect):
return sum((x**2 for x in vect))**0.5
@ -147,6 +155,13 @@ def z_to_vector(vector):
return rotation_matrix(angle, axis=axis)
def rotation_between_vectors(v1, v2):
return rotation_matrix(
angle=angle_between_vectors(v1, v2),
axis=normalize(np.cross(v1, v2))
)
def angle_of_vector(vector):
"""
Returns polar coordinate theta when vector is project on xy plane
@ -159,8 +174,7 @@ def angle_between_vectors(v1, v2):
Returns the angle between two 3D vectors.
This angle will always be btw 0 and pi
"""
diff = (angle_of_vector(v2) - angle_of_vector(v1)) % TAU
return min(diff, TAU - diff)
return math.acos(np.dot(normalize(v1), normalize(v2)))
def project_along_vector(point, vector):
@ -186,14 +200,6 @@ def normalize_along_axis(array, axis, fall_back=None):
return array
def cross(v1, v2):
return np.array([
v1[1] * v2[2] - v1[2] * v2[1],
v1[2] * v2[0] - v1[0] * v2[2],
v1[0] * v2[1] - v1[1] * v2[0]
])
def get_unit_normal(v1, v2, tol=1e-6):
v1 = normalize(v1)
v2 = normalize(v2)