Skip to content

Documentation

Denis Wagner edited this page Jan 21, 2019 · 8 revisions

General Notes

This library is currently for two-dimensional vectors only. Feel free to add specializations for three-dimensional vectors as well, or file an issue or a pull request.

You can use this library with C++98 but it is highly recommended to use it with a C++17 compiler to exploit the mandatory copy elision to eliminate the need of copying while returning a newly generated object, which makes your code a lot more efficient.

Vector addition

template <typename T> T operator+( const T& vec1, const T& vec2 )

Takes two two-dimensional sfml vector objects (e.g. sf::Vector2f) and performs a vector addition and returns a new vector object of the same vector type as the result.

sf::Vector2f vec1( 1, 2 );
sf::Vecotr2f vec2( 2, 3 );
auto result = vec1 + vec2; // result: ( 3, 5 )

Vector subtraction

template <typename T> T operator-( const T& vec1, const T& vec2 )

Takes two two-dimensional sfml vector objects (e.g. sf::Vector2f) and performs a vector subtraction and returns a new vector object of the same vector type as the result.

sf::Vector2f vec1( 3, 4 );
sf::Vecotr2f vec2( 2, 2 );
auto result = vec1 - vec2; // result: ( 1, 2 )

Scalar multiplication

template <typename T, typename U> T operator*( const T& vec, U scalar )

template <typename U, typename T> T operator*( U scalar, const T& vec )

Takes a two-dimensional sfml vector object (e.g. sf::Vector2f) and an arbitrary scalar object and performs a scalar multiplication and returns a new vector object of the same vector type as the result.

sf::Vector2f vec( 3, 2 );
auto result1 = vec * 3; // result: ( 9, 6 )
auto result2 = 3 * vec;

Dot product

template <typename T> double operator*( const T& vec1, const T& vec2 )

Takes two two-dimensional sfml vector objects (e.g. sf::Vector2f) and returns the dot product. The return type is double to avoid narrowing conversions.

sf::Vector2f vec1( 4, 2 );
sf::Vector2f vec2( 1, 3 );
auto result = vec1 * vec2; // result: 10 

sqr

template <typename T> inline T sqr( T value )

Takes a scalar object of an arbitrary type and returnes the squared result of the same type.

auto result = sf::sqr( 5 ); // result: 25

radToDeg

template <typename T> inline double radToDeg( T radians )

Takes a scalar object of an arbitrary type and converts the radians input value to degrees. The return type is double to avoid narrowing conversions.

auto degValue = sf::radToDeg( 1.57 ); // degValue: 90 degrees

degToRad

template <typename T> inline double degToRad( T degree )

Takes a scalar object of an arbitrary type and converts the degrees input value to radians. The return type is double to avoid narrowing conversions.

auto radValue = sf::degToRad( 90 ); // radValue: 1.57 degrees

getLength

template <typename T> inline double getLength( const T& vec )

Takes a two-dimensional sfml vector object (e.g. sf::Vector2f) and returns the length of the given vector. The return type is double to avoide narrowing conversions.

sf::Vector2f vec( 2, 2 );
auto length = sf::getLength(vec); // length: 2.828427125

getInverted

template <typename T> inline T getInverted( const T& vec )

Takes a two-dimensional sfml vector object (e.g. sf::Vector2f) and returns an object of the same vector type. The result is the inverted version of the input vector, while keeping the original input vector as it is (because it is const). It inverts the values in both axises.

sf::Vector2f vec( 2, -1 ); // won't be altered
auto invertedVec = sf::getInverted( vec ); // invertedVec: ( -2, 1 )

invert

template <typename T> inline T& invert( T& vec )

Takes a two-dimensional sfml vector object (e.g. sf::Vector2f) and inverts the input vector in-place and returns a reference to the altered input vector. It inverts the values in both axises.

sf::Vector2f vec( 2, -1 );
sf::invert( vec ); // vec: ( -2, 1 )

Because it returns the reference to the input vector those types of functions can be chained:

sf::rotate( sf::normalize( sf::invert( vec ) ) );

getNormalized

template <typename T> inline T getNormalized( const T& vec )

Takes a two-dimensional sfml vector object (e.g. sf::Vector2f) and returns an object of the same vector type. The result is the normalized version of the input vector, while keeping the original input vector as it is (because it is const). It normalizes the vector so it gets the length 1.

sf::Vector2f vec( 2, 1 ); // won't be altered
auto normalizedVec = sf::getNormalized( vec );

normalize

template <typename T> inline T& normalize( T& vec )

Takes a two-dimensional sfml vector object (e.g. sf::Vector2f) and normalizes the input vector in-place and returns a reference to the altered input vector. It normalizes the vector so it gets the length 1.

sf::Vector2f vec( 2, -1 );
sf::normalize( vec ); // changes vec

Because it returns the reference to the input vector those types of functions can be chained:

sf::rotate( sf::normalize( sf::invert( vec ) ) );

getRotationAngle

template <typename T> inline double getRotationAngle( const T& vec )

Takes a two-dimensional sfml vector object (e.g. sf::Vector2f) and returns an angle in degrees which describs the direction of the vector depending on the unit circle as double.

sf::Vector2f vecUp( 0, 1 ); // vecUp is pointing upwards, e.g. 90 degrees in the unit circle
auto directionAngle = sf::getRotationAngle( vecUp ); // directionAngle: 90

getAngleBetween

template <typename T> inline double getAngleBetween( const T& vec1, const T& vec2 )

Takes two two-dimensional sfml vector objects (e.g. sf::Vector2f) and returns the angle in degrees between the given vectors as double

sf::Vector2f vec1( 1, 0 );
sf::Vector2f vec2( 0, 1 );
auto angle = sf::getAngleBetween( vec1, vec2 ); // result: 90 degrees

getRotated

template <typename T> inline void getRotated( const T& vec, double angle )

Takes a two-dimensional sfml vector object (e.g. sf::Vector2f) and a double scalar as an angle in degrees and returns an object of the same vector type. The result is the rotated version of the input vector with the given angle, while keeping the original input vector as it is (because it is const). It rotates the vector counter clockwise.

sf::Vector2f vec( 2, 1 ); // won't be altered
auto normalizedVec = sf::getNormalized( vec );

rotate

template <typename T> inline T& rotate( T& vec, double angle )

Takes a two-dimensional sfml vector object (e.g. sf::Vector2f) and a double scalar as angle in degrees. It rotates the input vector in-place with the given angle and returns a reference to the altered input vector. It rotates the vector counter clockwise.

sf::Vector2f vec( 2, 1 );
sf::rotate( vec, 90 ); // rotates vec with 90 degrees

The implementation of this function converts degrees to radians. Feel free to add your own specialization for radians angles, so there will be no need for angle conversions.

Because it returns the reference to the input vector those types of functions can be chained:

sf::rotate( sf::normalize( sf::invert( vec ) ) );