Hi i'm trying to figure out how to calculate the inverse of a quaternion. A code example would be awesome.
Cheers
Hi i'm trying to figure out how to calculate the inverse of a quaternion. A code example would be awesome.
Cheers
有关整个四元数数学,请参阅Wikipedia 文章。
不知道您想使用哪种语言,但我会尝试在 Haskell 中给出一些提示。
data Quaternion = Q Double Double Double Double deriving (Show, Eq)
首先,您需要实现四元数的乘法和加法。
instance Num Quaternion where
(+) = q_plus
(*) = q_mult
--....
q_plus (Q a b c d) (Q a' b' c' d') = Q (a + a') (b + b') (c + c') (d + d')
q_mult (Q a b c d) (Q a' b' c' d') = Q a'' b'' c'' d''
where
a'' = a * a' - b * b' - c * c' - d * d'
b'' = a * b' + b * a' + c * d' - d * c'
c'' = a * c' - b * d' + c * a' + d * b'
d'' = a * d' + b * c' - c * b' + d * a'
与标量的乘法应通过转换完成:
scalar_to_q a = Q a 0 0 0
定义
i = Q 0 1 0 0
j = Q 0 0 1 0
k = Q 0 0 0 1
然后实现共轭和模:
q_conjugate q = (scalar_to_q (negate .5)) * (q + i * q * i + j * q * j + k * q * k)
q_modulus q = sqrt $ q * (q_conjugate q)
现在,反过来:
q_inverse q = (q_conjugate q) * (scalar_to_q (m * m))
where
m = q_modulus q
希望它有用。
PS:如果成功完成,上面的实例定义将简化一些事情。我让你填补空白。
查看矩阵和四元数常见问题解答。还有一些代码示例。