QUATERNION(2)QUATERNION(2)

## NAME

qtom, mtoq, qadd, qsub, qneg, qmul, qdiv, qunit, qinv, qlen, slerp, qmid, qsqrt – Quaternion arithmetic

## SYNOPSIS

`#include <draw.h>
`

`#include <geometry.h>
`

`Quaternion qadd(Quaternion q, Quaternion r)
`

`Quaternion qsub(Quaternion q, Quaternion r)
`

`Quaternion qneg(Quaternion q)
`

`Quaternion qmul(Quaternion q, Quaternion r)
`

`Quaternion qdiv(Quaternion q, Quaternion r)
`

`Quaternion qinv(Quaternion q)
`

`double qlen(Quaternion p)
`

`Quaternion qunit(Quaternion q)
`

`void qtom(Matrix m, Quaternion q)
`

`Quaternion mtoq(Matrix mat)
`

`Quaternion slerp(Quaternion q, Quaternion r, double a)
`

`Quaternion qmid(Quaternion q, Quaternion r)
`

`Quaternion qsqrt(Quaternion q)
`

## DESCRIPTION

The Quaternions are a non-commutative extension field of the Real numbers, designed
to do for rotations in 3-space what the complex numbers do for rotations in 2-space.
Quaternions have a real component
*r**v*=(*i*,*j*,*k*).
Quaternions add componentwise and multiply according to the rule
(*r*,*v*)(*s*,*w*)=(*rs*-*v*\v’-.3m’.\v’.3m’*w*, *rw*+*vs*+*v*×*w*),
where \v’-.3m’.\v’.3m’ and × are the ordinary vector dot and cross products.
The multiplicative inverse of a non-zero quaternion (*r*,*v*)
is (*r*,*-v*)/(*r*^{\s-22\s+2}-*v*\v’-.3m’.\v’.3m’*v*).

The following routines do arithmetic on quaternions, represented as

typedef struct Quaternion Quaternion; struct Quaternion{ double r, i, j, k; };

Description

`qadd`

Add two quaternions.

`qsub`

Subtract two quaternions.

`qneg`

Negate a quaternion.

`qmul`

Multiply two quaternions.

`qdiv`

Divide two quaternions.

`qinv`

Return the multiplicative inverse of a quaternion.

`qlen`

Return
`sqrt(q.r*q.r+q.i*q.i+q.j*q.j+q.k*q.k)`,

`qunit`

Return a unit quaternion
*length=1*)*q*’s.

A rotation by angle *θ* about axis
*A**A**q*=(cos *θ*/2, *A*sin *θ*/2).
The same rotation is represented by −*q*; a rotation by −*θ* about −*A* is the same as a rotation by *θ* about *A*.
The quaternion *q* transforms points by
(0,*x’,y’,z’*) = \%*q*^{\s-2-1\s+2}(0,*x,y,z*)*q*.
Quaternion multiplication composes rotations.
The orientation of an object in 3-space can be represented by a quaternion
giving its rotation relative to some ‘standard’ orientation.

The following routines operate on rotations or orientations represented as unit quaternions:

`mtoq`

Convert a rotation matrix (see
*matrix*(2))

`qtom`

Convert a unit quaternion to a rotation matrix.

`slerp`

Spherical lerp. Interpolate between two orientations.
The rotation that carries
*q**r**q*^{\s-2-1\s+2}*r*, so
`slerp(q, r, t)`*q*(*q*^{\s-2-1\s+2}*r*)^{\s-2t\s+2}.

`qmid`

`slerp(q, r, .5)`

`qsqrt`

The square root of
*q*.

## SOURCE

`/sys/src/libgeometry/quaternion.c`