# Quaternion

``` public class Quaternion ```
``` extends Object ``` ``` ```

 java.lang.Object ↳ com.viro.core.Quaternion

Represents a 4-component floating-point quaternion.

## Summary

### Fields

``` public float``` `w`

The W coordinate.

``` public float``` `x`

The X coordinate.

``` public float``` `y`

The Y coordinate.

``` public float``` `z`

The Z coordinate.

### Public constructors

``` Quaternion() ```

Construct a new Quaternion.

``` Quaternion(float x, float y, float z, float w) ```

Construct a new Quaternion from the given coordinates.

``` Quaternion(float[] coordinates) ```

Construct a new Quaternion from the given coordinates in an array of length 4.

``` Quaternion(float x, float y, float z) ```

Construct a new Quaternion from the given Euler angles about each axis, in radians.

``` Quaternion(Vector vec) ```

Construct a new Quaternion from the given Euler angles in radians.

``` Quaternion(Quaternion quaternion) ```

Construct a new Quaternion by copying the given Quaternion.

``` Quaternion(Matrix matrix) ```

Construct a new Quaternion from the given rotation `Matrix`.

### Public methods

``` float``` ``` dot(Quaternion q2) ```

Compute the dot product of this Quaternion with the given Quaternion, and return the result.

``` boolean``` ``` equals(Object obj) ```
``` float``` ``` getAngle() ```

Get the angle, in radians, of the rotation this Quaternion represents.

``` Matrix``` ``` getMatrix() ```

Convert this Quaternion into a rotation `Matrix`.

``` float``` ``` getNorm() ```

Get the norm of this Quaternion.

``` int``` ``` hashCode() ```
``` Quaternion``` ``` invert() ```

Invert this Quaternion and return the result.

``` Quaternion``` ``` lerp(Quaternion q, float t) ```

Linearly interpolate between this Quaternion and the given Quaternion, and return the result.

``` static Quaternion``` ``` makeIdentity() ```

Construct and return the identity Quaternion.

``` Quaternion``` ``` makeRotation(float angle, Vector axis) ```

Make a `Quaternion` that rotates about the given axis by the given angle in radians.

``` static Quaternion``` ``` makeRotationFromTo(Vector from, Vector to) ```

Construct and return a Quaternion that rotates the ray from to the ray to.

``` Quaternion``` ``` multiply(float s) ```

Multiply this Quaternion by the given scalar value and return the result.

``` Quaternion``` ``` multiply(Quaternion other) ```

Multiply this Quaternion by the given Quaternion and return the result.

``` Vector``` ``` multiply(Vector v) ```

Multiply the given `Vector` by this Quaternion and return the result.

``` Quaternion``` ``` normalize() ```

Normalize this Quaternion and return the result.

``` Quaternion``` ``` slerp(Quaternion q, float t, float threshold) ```

Perform a spherical linear interpolation between this Quaternion and the given Quaternion, and return the result.

``` Quaternion``` ``` subtract(Quaternion b) ```

Subtract the given Quaternion from this Quaternion and return the result.

``` float``` ``` toAngleAxis(Vector axis) ```

Get the axis of rotation and the angle of rotation (in radians) that this Quaternion represents.

``` float[]``` ``` toArray() ```

Get the contents of the Quaternion in an array.

``` Vector``` ``` toEuler() ```

Convert this Quaternion into Euler rotation about each principal axis, and return the result in a `Vector`.

``` String``` ``` toString() ```

## Fields

### w

`float w`

The W coordinate.

### x

`float x`

The X coordinate.

### y

`float y`

The Y coordinate.

### z

`float z`

The Z coordinate.

## Public constructors

### Quaternion

`Quaternion ()`

Construct a new Quaternion.

### Quaternion

```Quaternion (float x,
float y,
float z,
float w)```

Construct a new Quaternion from the given coordinates.

Parameters
`x` `float`: The X coordinate.

`y` `float`: The Y coordinate.

`z` `float`: The Z coordinate.

`w` `float`: The W coordinate.

### Quaternion

`Quaternion (float[] coordinates)`

Construct a new Quaternion from the given coordinates in an array of length 4.

Parameters
`coordinates` `float`: The x, y, z, and w coordinates.

### Quaternion

```Quaternion (float x,
float y,
float z)```

Construct a new Quaternion from the given Euler angles about each axis, in radians.

Parameters
`x` `float`: The X-axis rotation in radians.

`y` `float`: The Y-axis rotation in radians.

`z` `float`: The Z-axis rotation in radians.

### Quaternion

`Quaternion (Vector vec)`

Construct a new Quaternion from the given Euler angles in radians. The angles are provided as components in a `Vector`.

Parameters
`vec` `Vector`: The vector containing the Euler angle rotation.

### Quaternion

`Quaternion (Quaternion quaternion)`

Construct a new Quaternion by copying the given Quaternion.

Parameters
`quaternion` `Quaternion`: The Quaternion to copy.

### Quaternion

`Quaternion (Matrix matrix)`

Construct a new Quaternion from the given rotation `Matrix`.

Parameters
`matrix` `Matrix`: The rotation Matrix to convert into a Quaternion.

## Public methods

### dot

`float dot (Quaternion q2)`

Compute the dot product of this Quaternion with the given Quaternion, and return the result. Neither Quaternion is mutated by this operation.

Parameters
`q2` `Quaternion`: The Quaternion with which to dot this Quaternion.

Returns
`float` The dot product of the two Quaternions.

### equals

`boolean equals (Object obj)`

Parameters
`obj` `Object`

Returns
`boolean`

### getAngle

`float getAngle ()`

Get the angle, in radians, of the rotation this Quaternion represents. Note this method does not specify about which axis the rotation will occur. To retrieve the axis use `toAngleAxis(Vector)`.

Returns
`float` The angle of rotation in radians.

### getMatrix

`Matrix getMatrix ()`

Convert this Quaternion into a rotation `Matrix`.

Returns
`Matrix` The rotation Matrix generated from this Quaternion.

### getNorm

`float getNorm ()`

Get the norm of this Quaternion.

Returns
`float` The norm of the Quaternion.

### hashCode

`int hashCode ()`

Returns
`int`

### invert

`Quaternion invert ()`

Invert this Quaternion and return the result. This Quaternion is not mutated by this operation.

Returns
`Quaternion` The inverted Quaternion.

### lerp

```Quaternion lerp (Quaternion q,
float t)```

Linearly interpolate between this Quaternion and the given Quaternion, and return the result. Neither Quaternion is mutated by this operation.

Parameters
`q` `Quaternion`: The Quaternion to interpolate toward.

`t` `float`: The interpolation factor between 0 and 1, where 0 corresponds to this Quaternion, 1 corresponds to q, and values in-between result in an interpolated Quaternion between the two.

Returns
`Quaternion` The interpolated Quaternion.

### makeIdentity

`Quaternion makeIdentity ()`

Construct and return the identity Quaternion.

Returns
`Quaternion` The identity Quaternion.

### makeRotation

```Quaternion makeRotation (float angle,
Vector axis)```

Make a `Quaternion` that rotates about the given axis by the given angle in radians.

Parameters
`angle` `float`: The magnitude of the rotation in radians.

`axis` `Vector`: The axis about which rotation will occur. Must be unit length.

Returns
`Quaternion` The `Quaternion` representing rotation of angle radians about axis.

### makeRotationFromTo

```Quaternion makeRotationFromTo (Vector from,
Vector to)```

Construct and return a Quaternion that rotates the ray from to the ray to. In other words, when multiplied by this Quaternion, from will become to.

Parameters
`from` `Vector`: The Quaternion will move this `Vector` to the to Vector.

`to` `Vector`: The destination `Vector`.

Returns
`Quaternion` The Quaternion to rotate from to to.

### multiply

`Quaternion multiply (float s)`

Multiply this Quaternion by the given scalar value and return the result. This Quaternion is not mutated by this operation.

Parameters
`s` `float`: The scalar value to multiply by.

Returns
`Quaternion` The scaled Quaternion.

### multiply

`Quaternion multiply (Quaternion other)`

Multiply this Quaternion by the given Quaternion and return the result. Neither Quaternion is mutated by this operation.

Parameters
`other` `Quaternion`: The Quaternion to multiply by.

Returns
`Quaternion` The product of the two Quaternions.

### multiply

`Vector multiply (Vector v)`

Multiply the given `Vector` by this Quaternion and return the result. Neither the Quaternion nor the Vector are mutated by this operation.

Parameters
`v` `Vector`: The `Vector` to be multiplied.

Returns
`Vector` The result of the multiplication.

### normalize

`Quaternion normalize ()`

Normalize this Quaternion and return the result. This Quaternion is not mutated by this operation.

Returns
`Quaternion` The normalized Quaternion.

### slerp

```Quaternion slerp (Quaternion q,
float t,
float threshold)```

Perform a spherical linear interpolation between this Quaternion and the given Quaternion, and return the result. Slerp results in constant-speed motion along a unit-radius great circle arc between the two Quaternions.

Parameters
`q` `Quaternion`: The Quaternion to interpolate toward.

`t` `float`: The interpolation factor between 0 and 1, where 0 corresponds to this Quaternion, 1 corresponds to q, and values in-between result in an interpolated Quaternion between the two.

`threshold` `float`: To avoid inaccuracies near the end (t = 1) the interpolation switches to linear interpolation at some point. This value defines how much of the remaining interpolation will be calculated with lerp. Everything from 1-threshold up will be linear interpolation. A reasonable default threshold is 0.05f.

Returns
`Quaternion` The interpolated Quaternion.

### subtract

`Quaternion subtract (Quaternion b)`

Subtract the given Quaternion from this Quaternion and return the result. Neither Quaternion is mutated by this operation.

Parameters
`b` `Quaternion`: The Quaternion to subtract.

Returns
`Quaternion` The difference after subtracting the given Quaternion from this Quaternion.

### toAngleAxis

`float toAngleAxis (Vector axis)`

Get the axis of rotation and the angle of rotation (in radians) that this Quaternion represents. Store the axis in the given output `Vector`.

Parameters
`axis` `Vector`: The axis computed will be stored here.

Returns
`float` The angle of rotation in radians.

### toArray

`float[] toArray ()`

Get the contents of the Quaternion in an array.

Returns
`float[]` Float array of length four with components [x, y, z, w].

### toEuler

`Vector toEuler ()`

Convert this Quaternion into Euler rotation about each principal axis, and return the result in a `Vector`. This Quaternion is not mutated by this operation.

Returns
`Vector` The Euler rotation about each axis (X, Y, and Z) in radians.

### toString

`String toString ()`

Returns
`String`