plotting as vector

This commit is contained in:
robin 2024-08-26 16:13:31 +01:00
parent 8e6e7a0aa1
commit 46a4d56a8b

44
quaternions/qq2.py Normal file
View File

@ -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()