quaternion rotations
This commit is contained in:
parent
a965f3ed89
commit
8e6e7a0aa1
15
quaternions/Makefile
Normal file
15
quaternions/Makefile
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
all:
|
||||||
|
python qq.py
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# Make runs a shell for each line so below is more a reminder. Run this from the terminal! not Make!
|
||||||
|
p_env:
|
||||||
|
python3 -m venv venv && source ./venv/bin/activate && python3 -m pip install numpy-quaternion
|
||||||
|
|
||||||
|
|
||||||
|
|
42
quaternions/qq.py
Normal file
42
quaternions/qq.py
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
import numpy as np
|
||||||
|
import quaternion
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# Create a quaternion: 1 + 2i + 3j + 4k
|
||||||
|
q1 = np.quaternion(1, 2, 0, 0)
|
||||||
|
q2 = np.quaternion(2, 1, 0, 0)
|
||||||
|
print(f"Quaternion q1:{q1} q2:{q2}")
|
||||||
|
mq = q1 * q2
|
||||||
|
print("Quaternion mq:", mq)
|
||||||
|
|
||||||
|
q1 = np.quaternion(1, 1, 0, 0)
|
||||||
|
q2 = np.quaternion(2, 0, 1, 0)
|
||||||
|
print(f"Quaternion q1:{q1} q2:{q2}")
|
||||||
|
mq = q1 * q2
|
||||||
|
print("Quaternion mq:", mq)
|
||||||
|
|
||||||
|
q3 = np.quaternion(1,1,0,0)
|
||||||
|
q4 = 1/q3
|
||||||
|
|
||||||
|
print(f"Quaternion q3:{q3} q4:{q4}")
|
||||||
|
|
||||||
|
q5 = np.quaternion(0,1,1,0)
|
||||||
|
q6 = 1/q5
|
||||||
|
|
||||||
|
print(f"Quaternion q5:{q5} q6:{q6}")
|
||||||
|
|
||||||
|
|
||||||
|
# Rotation quaternion (example: 90-degree rotation around the z-axis)
|
||||||
|
angle = np.pi / 2 # 90 degrees in radians
|
||||||
|
rotation_q = np.quaternion(np.cos(angle / 2), 0, 0, np.sin(angle / 2))
|
||||||
|
|
||||||
|
q7 = np.quaternion(0,1,2,3)
|
||||||
|
q8 = rotation_q * q7
|
||||||
|
q9 = q8 * rotation_q.conj()
|
||||||
|
print(f"q7:{q7}\nq8:{q8}\nq9:{q9}")
|
||||||
|
|
||||||
|
|
||||||
|
q10 = rotation_q * q7 * rotation_q.conj() # rotate in one line
|
||||||
|
print(f" q10 {q10} ")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user