Quaternions Presentation

1662 days ago by peter.story

## What are we trying to do? We are rotating a vector, "v", 90 degrees ## about another vector, "u". v = vector([1,1,1]) # Shown in GREEN u = vector([0,0,1]) # Shown in RED v_rot = vector([-1,1,1]) # Shown in BLUE show(plot(v, color='green', thickness=5) + plot(u, color='red', thickness=5) + plot(v_rot, color='blue', thickness=5)) ## Now, we were able to do this in our head, but that would be impossible if a more ## challenging "u" vector and angle were chosen. 
       
## We can perform any rotation around any 3D vector using quaternions. ## These operations are greatly simplified when quaternions are represented at matrices. ## Quaternion: a + bi + cj + dk ## Quaternion as a vector: <a, b, c, d> ## Quaternion as a Matrix: matrix([[a,b,c,d],[-b,a,-d,c],[-c,d,a,-b],[-d,-c,b,a]]) ### START # Change these for different rotations theta = pi/2 v = vector([1,1,1]) u = vector([0,0,1]) ### END # Represent a rotation as a quaternion def rotToQuat(myVec, myAngle): return vector([cos(myAngle/2), myVec[0]*sin(myAngle/2), myVec[1]*sin(myAngle/2), myVec[2]*sin(myAngle/2)]) # return vector([cos(myAngle), myVec[0]*sin(myAngle), myVec[1]*sin(myAngle), myVec[2]*sin(myAngle)]) # Defining quaternion vector to matrix conversion def qVecToMat(myVec): a = myVec[0]; b = myVec[1]; c = myVec[2]; d = myVec[3]; return matrix([[a,b,c,d],[-b,a,-d,c],[-c,d,a,-b],[-d,-c,b,a]]) # Creating quaternion matrix q_vector = rotToQuat(u, theta) #q_vector = vector([cos(theta),0,0,sin(theta)]) #; show(q_vector) q_matrix = qVecToMat(q_vector) #; show(q_matrix) # Creating quaternion representation of vector v v_quat = vector([0, v[0], v[1], v[2]]) v_matrix = qVecToMat(v_quat) #; show(v_matrix) # Creating conjugate quaternion q_star_matrix = q_matrix.transpose() #; show(q_star_matrix) # Multiplying them, we get prod_matrix = q_matrix*v_matrix*q_star_matrix # But not showing, because it is enormous ## Now, extracting our rotated vector from the matrix v_rot = vector([prod_matrix[0,1], prod_matrix[0,2], prod_matrix[0,3]]) #; show(p_prime) # Plotting show(plot(v, color='green', thickness=5) + plot(u, color='red', thickness=5) + plot(v_rot, color='blue', thickness=5)) 
       
v = vector([1,1,1]) w = vector([0,1,1]) show(plot(v, color='red') + plot(w, color='blue'))