7.4

## Quaternion

 (require quaternion) package: Quaternion

### 1Intro

Quaternions are used for rotation of koordinates and vectors.

### 2Structures

struct

 (struct qvector (x y z)
#:mutable
#:transparent)
x : flonum?
y : flonum?
z : flonum?

struct

 (struct quaternion (w v)
#:mutable
#:transparent)
w : flonum?
v : qvector?

### 3Basic procedures

 procedure(q-add q1 q2) → quaternion? q1 : (quaternion?) q2 : (quaternion?)
 procedure(q-sub q1 q2) → quaternion? q1 : (quaternion?) q2 : (quaternion?)
 procedure(q-norm q) → quaternion? q : (quaternion?)
 procedure(q-normalize q) → quaternion? q : (quaternion?)
 procedure(q-multiply-qq q1 q2) → quaternion? q1 : (quaternion?) q2 : (quaternion?)
 procedure(q-divide-qs q s) → quaternion? q : (quaternion?) s : (flonum?)
 procedure(q-negate q) → quaternion? q : (quaternion?)
 procedure(q-conjugate q) → quaternion? q : (quaternion?)
 procedure(q-inverse q) → quaternion? q : (quaternion?)

### 4Rotation

 procedure(q-rotation angl qv) → quaternion? angl : (flonum?) qv : (qvector?)
Rotation in radians about an arbitrary axis. Positive rotation is anticlockwise!

 procedure(q-rotate rotation object) → quaternion? rotation : (quaternion?) object : (quaternion?)
Applyes the rotation on an object. Object quaternion’s w has to be 0!

### 5Examples

 ; Rotation about one axis: (let* ([A (q-rotation (fl/ pi 2.0) (qvector 0.0 1.0 0.0))]; Rotation 90degrees about Y-axis. [B (q-rotate A (quaternion 0.0 (qvector 0.0 0.0 1.0)))]); Apply rotation on quaternion with A. (begin (printf "X: ~a\n" (~r(qvector-x (quaternion-v B)))); Print results. (printf "Y: ~a\n" (~r(qvector-y (quaternion-v B)))) (printf "Z: ~a\n" (~r(qvector-z (quaternion-v B))))))
 ; Rotation about two axis: (let* ([A1 (q-rotation (fl/ pi 4.0) (qvector 0.0 1.0 0.0))]; Rotation 90degrees about Y-axis. [A2 (q-rotation (fl/ pi 4.0) (qvector 1.0 0.0 0.0))]; Rotation 90degrees about X-axis. [AS (q-multiply-qq A1 A2)]; Combine rotations. [B (q-rotate AS (quaternion 0.0 (qvector 0.0 0.0 1.0)))]); Apply rotation on quaternion with A. (begin (printf "X: ~a\n" (~r(qvector-x (quaternion-v B)))); Print results. (printf "Y: ~a\n" (~r(qvector-y (quaternion-v B)))) (printf "Z: ~a\n" (~r(qvector-z (quaternion-v B))))))