The vector class

Definition

Here are different methods to create a vectorn object:

        // Produces an empty vector
        tmath::vectorn<type, tupel> v1;
        
        // Initiates the vector with values
        tmath::vectorn<float, 3> v2(0.0f, 1.0f, 25.65f);

        // Initiates the vector by another vector
        tmath::vectorn<float, 3> v3(v2);

The identifier type can be float , double or an int. The identifier tupel is the number of components that should be supported by this vector object. 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 vector can be added or subtracted like this example:

        tmath::vectorn<float,3> v1(0.0f, 1.0f, 0.0f);
        tmath::vectorn<float,3> v2(1.0f, 1.0f, 0.0f);
        tmath::vectorn<float,3> v3;
        
        // Addition
        v3 = v1 + v2;
        v3 += v1;
        
        // Subtraction
        v3 = v1 - v2;
        v3 -= v1;

Multiplication and Division

Two vectors can be multiplicated by each other. The result is the scalar product. One can also multiplicate the vector with a scalar.

        tmath::vectorn<float,3> v1(0.0f, 1.0f, 0.0f);
        tmath::vectorn<float,3> v2(1.0f, 1.0f, 0.0f);
        tmath::vectorn<float,3> v3;
        
        // Scalar product
        float skalar = v1 * v2;
        
        // Multiplication with a scalar
        v3 = v1 * 1.0f;
        
        // Dito
        v3 = 1.0f * v1;

It is possible to divide a scalar. This is only an inverted multiplication.

        tmath::vectorn<float,3> v1(0.0f, 1.0f, 0.0f);
        tmath::vectorn<float,3> v2;
        
        // Division with a scalar
        v2 = v1 / 1.2f; // == v1 * (1.0f/1.2f)

Other functions

The length of a vector is calculated like this:

        tmath::vectorn<float,3> v1(3.3f, 1.0f, 0.0f);
        
        // Calculates the length of the vector
        float length =tmath::len(v1);

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

        tmath::vectorn<float,3> v1(3.3f, 1.0f, 0.0f);
        
        // Calculates the norm of a vector
        float n = tmath::norm(v1);

To normalize a vector use the normalize() function:

        tmath::vectorn<float,3> v1(3.3f, 1.0f, 0.0f);
        tmath::normalize(v1);

The cross product of two vectors could calculated with the cross() function:

        tmath::vectorn<float,3> v1(3.3f, 1.0f, 0.0f);
        tmath::vectorn<float,3> v2(3.3f, 1.0f, 0.0f);
        tmath::vectorn<float,3> v3;
        
        // Calclulates the cross product. (Only for vectorn<type, 3> )
        v3 = tmath::cross(v1,v2);

Access to the components

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

        tmath::vectorn<float,3> v1(3.3f, 1.0f, 0.0f);
        float x = v1.x;
        float y = v1.y;
        float z = v1.z;

Second, you can use the vector itself as a pointer of an array. You can access the components with the [] operator.

        tmath::vectorn<float,3> v1(3.3f, 1.0f, 0.0f);
        float* v = v1;
        float xcomp = v[0];
        float ycomp = v[1];
        float zcomp = v[2];

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::vectorn<float,3> v1(3.3f, 1.0f, 0.0f);
        std::cout << v << std::endl; 

Conversion

You can convert between the vectorn<T,2>, vectorn<T,3>, vectorn<T,4> classes. If you want to change a vectorn<T,3> into a vectorn<T,2> do as in the following code:

        tmath::vectorn<float,3> v1(4.321f, 23.0f, 0.0021f);
        tmath::vectorn<float,2> v2;
        
        // Converts v1 in v2
        tmath::conv(v1,v2);

Or the other way around. You can convert a vectorn<T,2> into a vectorn<T,3>:

        tmath::vectorn<float,2> v1(4.321f, 23.0f);
        tmath::vectorn<float,3> v2;
        
        // Converts v1 in v2.   v2=(4.321f, 23.0f, 0.0f)
        tmath::conv(v1,v2); 

The added new component is filled with a null.


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