# Maths - Quaternion Code

## complete sfrotation class

This class can represent a 3D rotation. The class has 4 double numbers which represent the rotation as either quaternion, axis-angle or euler number depending on the cde int/enum

The class has methods to combine with other rotations. Also many other methods, including the ability to load and save to from VRML and x3d

There are 3 versions available depending on language:

The full source code is available on Sourceforge here:

## Conjugate

`public final void conjugate(Quat4d q1) {    x = -q1.x;    y = -q1.y;    z = -q1.z;    w = q1.w;  }`

## Normalising Quaternions

```
public final void normalise() {    double n = Math.sqrt(x*x + y*y + z*z + w*w);    x /= n;    y /= n;    z /= n;    w /= n;}```

## Quaternion Scalar Multiplication

`public final void scale(double s){    x *= s;    y *= s;    z *= s;    w *= s;}  `

## Quaternion Multiplication

```public final void mul(Quat4d q1,Quat4d q2) {    x =  q1.x * q2.w + q1.y * q2.z - q1.z * q2.y + q1.w * q2.x;    y = -q1.x * q2.z + q1.y * q2.w + q1.z * q2.x + q1.w * q2.y;    z =  q1.x * q2.y - q1.y * q2.x + q1.z * q2.w + q1.w * q2.z;    w = -q1.x * q2.x - q1.y * q2.y - q1.z * q2.z + q1.w * q2.w;}
```
Information about the equivalence of quaternion multiplication and orthogonal matrix multiplication.

`public final void add(Quat4d q1,Quat4d q2) {    x = q1.x + q2.x;    y = q1.y + q2.y;    z = q1.z + q2.z;    w = q1.w + q2.w;}`