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