simplevectors
0.3.9
Simple vector implementations in C++
|
This section covers basic usage of vectors with svector::Vector2D
and svector::Vector3D
objects. For more details, see the library API.
Everything is under the namespace svector
.
The examples below are combined in the example/basic_examples.cpp
file in the source code.
All of the examples below (except for initialization, printing, and those specifically noted) show the methods of the vector object. For each method, there is also a function under the svector
namespace that takes in a vector object and the arguments for the corresponding method. For example:
is equivalent to:
Below are examples of zero initialization and initializing with values.
You can also initialize a vector in a functional manner by using the svector::makeVector()
function. This function can be used to initialize a vector from an std::array
, std::vector
, or an initializer list. Note that if you are using svector::makeVector()
to initialize from a std::vector
or an initializer list, then you need to specify the number of dimensions as a template argument. If you are using an initializer list, you also need to specify the type of the vector elements.
Both svector::Vector2D
and svector::Vector3D
have toString()
methods for printing.
The properties are shown in the code snippet below.
Note that the functional equivalent for getting the angles of a 3D vector is slightly different:
You can also access the x, y, and z components using the []
operator. In this case, the 0th index would correspond to the x-value, the 1st index would correspond to the y-value, and the 2nd index would correspond to the z-value. This also works on higher-dimensional vectors. There is no functional equivalent to this operator.
Below are the operations that can be done on both 2D and 3D vectors. For simplicity, examples will be shown with only 2D vectors.
Cross products only work on 3D vectors.
Vectors can be added, subtracted, multiplied, or divided in place.
Works for both 2D and 3D vectors.
Below shows an example of vector normalization.
normalize()
will result in undefined behavior if the magnitude of the vector is zero.For 2D vectors, the rotate
method requires one argument, ang
, which is the angle to rotate in radians. A positive angle indicates counterclockwise rotation, and a negative angle indicates clockwise rotation. The method returns a new vector.
For 3D vectors, the rotate
method only works for one axis at a time. It requires a template argument indicating the axis to rotate around: ALPHA
for the x-axis, BETA
for the y-axis, and GAMMA
for the z-axis.
Rotations can also be chained:
Note that the functional equivalent of rotation around a certain axis is slightly different:
The Vector
class and the classes that extend it (namely Vector2D
and Vector3D
) are container-like in the sense that they have iterators and begin()
, end()
, rbegin()
, and rend()
methods. This means that they can be looped through like any other STL container.
This can be helpful for calculating sums.