The quaternion class

Definition

There are different types how a quaternion can be defined:
        tmath::quaternion<type> q1;                             // Produces an empty quaternion
        tmath::quaternion<type> q2(0.0, 1.0f, 25.65, 1.0f);     // Initiates the quaternion with values
        tmath::quaternion<type> q3(v2);                         // Initiates the quaternion by another quaternion

The identifier type can be float , double or an int. You should know that if you are using int types you will get rounding errors because you are not working with real values.

Addition, Subtraction

A quaternion can be added or subtracted like following example:

        tmath::quaternion<float> q1(0.0f, 1.0f, 0.0f, 0.2f);
        tmath::quaternion<float> q2(1.0f, 1.0f, 0.0f, 0.4f);
        tmath::quaternion<float> q3;
        
        // Addition
        q3 = q1 + q2;
        q3 += q1;
        
        // Subtraction
        q3 = q1 - q2;
        q3 -= q1;

Multiplication and Division

Two quaternions can be multiplicated by each other. The result is another quaternion.

        tmath::quaternion<float> q1(0.0f, 1.0f, 0.0f, 0.3f);
        tmath::quaternion<float> q2(1.0f, 1.0f, 0.0f, 7.54f);
        tmath::quaternion<float> q3;

        // Quaternion quaternion multiplication
        q3 = q1 * q2;
        
        // multiplication with a scalar
        q3 = q1 * 1.0f;
        
        // dito
        q3 = 1.0f * q1;

Or a division with a scalar:

        tmath::quaternion<float> q1(0.0f, 1.0f, 0.0f, 0.02f);
        tmath::quaternion<float> q2;

        // Division == q1 * (1.0f/1.2f)
        q2 = q1 / 1.2f; 

Other functions

The length of a quaternion is calculated like this:

        tmath::quaternion<float> q1(3.3f, 1.0f, 0.0f, 9.3f);
        
        // The length of a quaternion
        float length =tmath::len(q1);

To estimate the norm of a quaternion use the norm() function

        tmath::quaternion<float> q1(3.3f, 1.0f, 0.0f, 2.0f);
        
        // The norm of a quaternion
        float n = tmath::norm(q1);

To normalize a vector use the normalize() function:

        tmath::quaternion<float> q1(3.3f, 1.0f, 0.0f, 9.54f);
        
        // Normalizing a quaternion
        tmath::normalize(q1);

If you need the conjugated quaternion use the conj() function:

        tmath::quaternion<float> q1(3.3f, 1.0f, 0.0f, 9.54f), qres;
        
        // Conjugate a quaternion
        qres = tmath::conj(q1);

Or if you need the inverse of the quaternion use the inv() function:

        tmath::quaternion<float> q1(3.3f, 1.0f, 0.0f, 9.54f), qres;
        
        // Invert a quaternion
        qres = tmath::inv(q1);

Access to the components

There are two possible access methods. One is to use the .x .y .z .w variable names.

        tmath::quaternion<float> q1(3.3f, 1.0f, 0.0f, 74.2f);
        float x = q1.x;
        float y = q1.y;
        float z = q1.z;
        float w = q1.w;

Or you can use the quaternion itself as a pointer of an array. Of course the type of the array depends on the quaternion.

        tmath::quaternion<float> q1(3.3f, 1.0f, 0.0f, 0.2f);
        float* q = q1;
        float xcomp = q[0];
        float ycomp = q[1];
        float zcomp = q[2];
        float wcomp = q[3];

Please keep in mind that the element access via the [] operator are not designed for security checking. This means no border checks are done.

String Output

You can use the std::cout method to print out the components of the vector.
        tmath::quaternion<float> q1(3.3f, 1.0f, 0.0f, 34.4f);
        std::cout << q << std::endl; 

Generated on Mon Sep 10 17:42:12 2007 for TinyMath by  doxygen 1.5.2