mirror of
https://github.com/3b1b/manim.git
synced 2025-11-14 15:37:45 +00:00
Fix angle_between_vectors, add rotation_between_vectors
This commit is contained in:
parent
d065e1973d
commit
82bd02d21f
1 changed files with 16 additions and 10 deletions
|
|
@ -12,6 +12,14 @@ from manimlib.constants import TAU
|
||||||
from manimlib.utils.iterables import adjacent_pairs
|
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):
|
def get_norm(vect):
|
||||||
return sum((x**2 for x in vect))**0.5
|
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)
|
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):
|
def angle_of_vector(vector):
|
||||||
"""
|
"""
|
||||||
Returns polar coordinate theta when vector is project on xy plane
|
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.
|
Returns the angle between two 3D vectors.
|
||||||
This angle will always be btw 0 and pi
|
This angle will always be btw 0 and pi
|
||||||
"""
|
"""
|
||||||
diff = (angle_of_vector(v2) - angle_of_vector(v1)) % TAU
|
return math.acos(np.dot(normalize(v1), normalize(v2)))
|
||||||
return min(diff, TAU - diff)
|
|
||||||
|
|
||||||
|
|
||||||
def project_along_vector(point, vector):
|
def project_along_vector(point, vector):
|
||||||
|
|
@ -186,14 +200,6 @@ def normalize_along_axis(array, axis, fall_back=None):
|
||||||
return array
|
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):
|
def get_unit_normal(v1, v2, tol=1e-6):
|
||||||
v1 = normalize(v1)
|
v1 = normalize(v1)
|
||||||
v2 = normalize(v2)
|
v2 = normalize(v2)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue