No longer in development. Use glm instead.
Contact: development@kortlepel.com
This is a templated header-only class for a 2D Vector, written in C++17, with all common vector operations and some useful additional methods.
-
Header-only, MIT licensed, templated.
-
All equality- and relational operators
-
Supported operations:
- + - * / with another vector
- + - * / with any scalar (templated)
- rotation (deg & rad)
- reflection (using surface normal)
- distance & squared distance
- magnitude (length) & squared magnitude
- normalization
- dot product
- linear interpolation (clamped & unclamped)
- square root
-
Typedefs for all primitive types (
Vector2f, Vector2i, Vector2d, etc.)
- Common operators
- Templated operators
- License (MIT)
- Full in-source documentation
- Common operations like normalizing, dot/cross/scalar product, distance, magnitude, etc.
- Extra fancy operations like linear interpolation, abs, rotation, reflection
- Floating point equality comparisons have been implemented roughly following this article. Feel free to override the functions used by
#define-ingVECTOR2_FLOAT_COMPARE,VECTOR2_DOUBLE_COMPAREandVECTOR2_LONG_DOUBLE_COMPAREfor float, double and long double comparisons, respectively. They're used likeFUNCTION(first_float, second_float). - Vector-rotation is implemented to rotate CLOCKWISE.
rotated_radis possibly faster thanrotated_deg, since the latter converts from degrees to radians first.- The typedefs which specify size (for example
Vector2i8) useint_fast8_tand similar (fastvariant). Feel free to open an issue if this causes problems
It should be intuitive, but here's how intuitive:
With typedefs...
Vector2f vec {}; // (0, 0)
Vector2u vec (6); // (6, 6)
Vector2d vec { 2.0, 5.0 }; // (2.0, 5.0)
Vector2i vec (6, -15); // (6, -15)...or the typey way:
Vector2<long double> vec (2.5L, -18.5L);auto result_vec = vec.rotated_deg (45.0);auto result_vec = vec.rotated_rad (PI / 3.0);auto result_vec = vec.lerp (other_vec, 0.75);unclamped means that the value is not restricted between 0.0 and 1.0.
auto result_vec = vec.lerp_unclamped (other_vec, 1.5);auto result_vec = vec.reflected (normal_vec);auto result_vec = vec.distance (other_vec);auto result_vec = vec.sqr_distance (other_vec);double mag = vec.magnitude ();double sqr_mag = vec.sqr_magnitude ();auto result_vec = vec.dot (other_vec);auto result_vec = vec.square_root ();result_vec = vec.normalized ();