-
Notifications
You must be signed in to change notification settings - Fork 1
Documentation
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.
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 )
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 )
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;
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
Takes a scalar object of an arbitrary type and returnes the squared result of the same type.
auto result = sf::sqr( 5 ); // result: 25
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
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
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
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 )
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 ) ) );
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 );
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 ) ) );
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
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
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 );
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 ) ) );