From 46a4d56a8b0db9c29a1c8f3563f9079078c091e1 Mon Sep 17 00:00:00 2001 From: robin Date: Mon, 26 Aug 2024 16:13:31 +0100 Subject: [PATCH] plotting as vector --- quaternions/qq2.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 quaternions/qq2.py diff --git a/quaternions/qq2.py b/quaternions/qq2.py new file mode 100644 index 0000000..4161337 --- /dev/null +++ b/quaternions/qq2.py @@ -0,0 +1,44 @@ +import numpy as np +import quaternion +import matplotlib.pyplot as plt +from mpl_toolkits.mplot3d import Axes3D + +# Define the original vector (as a pure quaternion) +v = np.quaternion(0, 1, 2, 3) # This is the vector (1, 2, 3) + +# Define a rotation quaternion (e.g., 90-degree rotation around the z-axis) +angle = np.pi / 2 # 90 degrees +rotation_q = np.quaternion(np.cos(angle / 2), 0, 0, np.sin(angle / 2)) + +# Apply the quaternion rotation +v_rotated = rotation_q * v * rotation_q.conjugate() +impure = rotation_q * v + +# Extract the vector components (x, y, z) from the quaternion +v_vector = [v.x, v.y, v.z] +v_impure = [impure.x, impure.y, impure.z] +v_rotated_vector = [v_rotated.x, v_rotated.y, v_rotated.z] + +# Plotting +fig = plt.figure() +ax = fig.add_subplot(111, projection='3d') + +# Plot the original vector +ax.quiver(0, 0, 0, v_vector[0], v_vector[1], v_vector[2], color='b', label='Original Vector') +ax.quiver(0, 0, 0, v_impure[0], v_impure[1], v_impure[2], color='b', label='Impure Vector') + +# Plot the rotated vector +ax.quiver(0, 0, 0, v_rotated_vector[0], v_rotated_vector[1], v_rotated_vector[2], color='r', label='Rotated Vector') + +# Setting the axes properties +ax.set_xlim([-3.5, 3.5]) +ax.set_ylim([-3.5, 3.5]) +ax.set_zlim([-3.5, 3.5]) + +ax.set_xlabel('X') +ax.set_ylabel('Y') +ax.set_zlabel('Z') + +plt.legend() +plt.show() +