var Quaternion = ;var q = "99.3+8i";c;
In order to create a HTML element, which always rotates in 3D with your mobile device, all you need is the following snippet. Look at the examples folder for a complete version.
var rad = MathPI / 180;window;
Any function (see below) as well as the constructor of the Quaternion class parses its input like this.
You can pass either Objects, Doubles or Strings.
Calling the constructor will create a quaternion 1-element.
// 1 + 0i + 0j + 0k
The typical use case contains all quaternion parameters
w x y z
Quaternion as angle and vector. Note: This is not equivalent to Quaternion.fromAxisAngle()!
w x y z
Quaternion as an object (it's ok to leave components out)
w: w x: x y: y z: z
Quaternion out of a complex number, e.g. Complex.js.
re: real im: imaginary
Quaternion out of a 4 elements vector
w x y z
Quaternion out of a 3 elements vector
x y z
'1 - 2i - 3j - 4k'"123.45";"15+3i";"i";
Every stated parameter n in the following list of functions behaves in the same way as the constructor examples above
Note: Calling a method like add() without parameters results in a quaternion with all elements zero, not one!
Adds two quaternions Q1 and Q2
Subtracts a quaternions Q2 from Q1
Calculates the additive inverse, or simply it negates the quaternion
Calculates the length/modulus/magnitude or the norm of a quaternion
Calculates the squared length/modulus/magnitude or the norm of a quaternion
Normalizes the quaternion to have |Q| = 1 as long as the norm is not zero. Alternative names are the signum, unit or versor
Calculates the Hamilton product of two quaternions. Leaving out the imaginary part results in just scaling the quat.
Note: This function is not commutative, i.e. order matters!
Scales a quaternion by a scalar, faster than using multiplication
Calculates the dot product of two quaternions
Calculates the inverse of a quat for non-normalized quats such that Q^-1 * Q = 1 and Q * Q^-1 = 1
Multiplies a quaternion with the inverse of a second quaternion
Calculates the conjugate of a quaternion. If the quaternion is normalized, the conjugate is the inverse of the quaternion - but faster.
Calculates the natural exponentiation of the quaternion
Calculates the natural logarithm of the quaternion
Returns the real part of the quaternion
Returns the imaginary part of the quaternion as a 3D vector / array
Checks if two quats are the same
Checks if all parts of a quaternion are finite
Checks if any of the parts of the quaternion is not a number
Gets the Quaternion as a well formatted string
Gets the actual quaternion as a 4D vector / array
Calculates the 3x3 rotation matrix for the current quat as a 9 element array or alternatively as a 2d array
Calculates the homogeneous 4x4 rotation matrix for the current quat as a 16 element array or alternatively as a 2d array
Clones the actual object
Rotates a 3 component vector, represented as an array by the current quaternion
Sets the quaternion by a rotation given as axis and angle
Creates a quaternion by a rotation given by Euler angles. Optional the order of the axis can be provided.
Calculates the quaternion to rotate one vector onto the other
A quaternion zero instance (additive identity)
A quaternion one instance (multiplicative identity)
An imaginary number i instance
An imaginary number j instance
An imaginary number k instance
A small epsilon value used for
equals() comparison in order to circumvent double inprecision.
Installing Quaternion.js is as easy as cloning this repo or use one of the following commands:
bower install quaternion
npm install --save quaternion
As every library I publish, Quaternion.js is also built to be as small as possible after compressing it with Google Closure Compiler in advanced mode. Thus the coding style orientates a little on maxing-out the compression rate. Please make sure you keep this style if you plan to extend the library.
If you plan to enhance the library, make sure you add test cases and all the previous tests are passing. You can test the library with
Copyright (c) 2017, Robert Eisele Dual licensed under the MIT or GPL Version 2 licenses.