Vector class

From Solid Graphics Wiki

Revision as of 03:06, 30 April 2008 by Rgrof (Talk | contribs)
(diff) ←Older revision | Current revision (diff) | Newer revision→ (diff)
Jump to: navigation, search
Vector class
Namespace: SKL3D
Base type: Point class
Include: Skl3D.h
Lib (Release): SKL.lib
Lib (Debug): SKL_D.lib

The Vector class specifies (x, y, z) direction in 3D space using float precision. A vector can be thought as arrow drawn in space from point A to point B. Other main property of a vector is it's size (also commonly reffered as lenght or magnitude).


Default Vector class constructor does not initialize the vector coordinates to any value. This is on purpose for performance reasons. Other class constructors allow to initialize the Vector object from other vector, or set of three float values, or float values array, or double values array.


Field name Type Description
x float the X coordinate of vector direction
y float the Y coordinate of vector direction
z float the Z coordinate of vector direction
coordinates float[ 3 ] array of three (x, y, z) vector direction coordinates. Note: the x, y, z fields and the coordinate array are actually defined as union, so for example code vector.coordinate[ 1 ] = 3.0f also changes the vector.y coordinate to 3.0f


Method name Description
Normalize Changes the vector so it's size is one unit.
Dot Calculates dot product of two vectors. The result is single float value which is equal to multiplication of sizes of both vectors and cosinus of their angle.
Cross Calculates cross product of two vectors. The result is a vector which is perpendicular to both input vectors and it's size is multiplication of sizes of both vectors and sinus of their angle.
Size Returns the vector size (length).
SquaredSize Returns sqaured vector's size.
IsBetween The IsBetween function returns true if it lies in between angle created by by turning two given vectors in counterclockwise direction. Othervise the return value is false.
IsCollinear The function returns true if the vector point in the same direction as a vector given as parameter. Othervise the return value is false.
AngleTo Calculates angle needed to turn the vector counterclockwise so the vector point to the same direction as other given vector.
YXRotationAngles Calculates Y axis and X axis rotations needed to point the Z axis vector into the direction of the vector.


* Depending on the other operand type (which can be other vector or float) the operator return a vector which is either cross product of the two vectors or multiplication of the vector by the float value.
/ Returns a vector which is calculated by dividing the vector coordinates by given float value.
+ Returns a vector which is calculated by adding coordinates of two vector.
- Returns a vector which is calculated by subtracting two vectors.
= The assignment operator allows to assign a point coordinates to the vector.


The Vector class does not have any virtual functions on purpose, so it is safe (in MS Visual C++) to typecast pointer to the class object to float pointer and access the x, y, z coordinates by float array indexes 0, 1 and 2.


SKL3D::Vector   vector;
vector.x = 10.0f;
vector.y = 15.0f;
vector.z = 20.0f;

float* fPtr = ( float* ) & vector;
fPtr[ 2 ] = 22.0f;     // this line changes vector.z coordinate to 22.0f

Also it is safe to use functions such as memcpy to copy values of one vector object to other, or values of vector array objects to another vector array.

See Also


SolidKit Library Documentation

Personal tools