45 lines
1.3 KiB
Python
45 lines
1.3 KiB
Python
|
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()
|
||
|
|