From 662598fcef1c3219e81deef530905dc43f5bc6a7 Mon Sep 17 00:00:00 2001 From: Grant Sanderson Date: Fri, 4 Jan 2019 12:48:05 -0800 Subject: [PATCH] Make sure rotate_vector works on 2d vectors --- manimlib/utils/space_ops.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/manimlib/utils/space_ops.py b/manimlib/utils/space_ops.py index 9b5af5bb..50efb097 100644 --- a/manimlib/utils/space_ops.py +++ b/manimlib/utils/space_ops.py @@ -53,13 +53,21 @@ def quaternion_conjugate(quaternion): def rotate_vector(vector, angle, axis=OUT): - quat = quaternion_from_angle_axis(angle, axis) - quat_inv = quaternion_conjugate(quat) - product = reduce( - quaternion_mult, - [quat, np.append(0, vector), quat_inv] - ) - return product[1:] + if len(vector) == 2: + # Use complex numbers...because why not + z = complex(*vector) * np.exp(complex(0, angle)) + return np.array([z.real, z.imag]) + elif len(vector) == 3: + # Use quaternions...because why not + quat = quaternion_from_angle_axis(angle, axis) + quat_inv = quaternion_conjugate(quat) + product = reduce( + quaternion_mult, + [quat, np.append(0, vector), quat_inv] + ) + return product[1:] + else: + raise Exception("vector must be of dimension 2 or 3") def thick_diagonal(dim, thickness=2):