Skip to content

lionkor/Vector2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

59 Commits
 
 
 
 
 
 

Repository files navigation

Vector2

No longer in development. Use glm instead.

Contact: development@kortlepel.com

Overview

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.

Features

Development Progress

  • 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

Notes

  • Floating point equality comparisons have been implemented roughly following this article. Feel free to override the functions used by #define-ing VECTOR2_FLOAT_COMPARE, VECTOR2_DOUBLE_COMPARE and VECTOR2_LONG_DOUBLE_COMPARE for float, double and long double comparisons, respectively. They're used like FUNCTION(first_float, second_float).
  • Vector-rotation is implemented to rotate CLOCKWISE.
  • rotated_rad is possibly faster than rotated_deg, since the latter converts from degrees to radians first.
  • The typedefs which specify size (for example Vector2i8) use int_fast8_t and similar (fast variant). Feel free to open an issue if this causes problems

How to use

It should be intuitive, but here's how intuitive:

Construct a vector

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);

Vector Operations

rotate (degrees)

auto result_vec = vec.rotated_deg (45.0);

rotate (radians)

auto result_vec = vec.rotated_rad (PI / 3.0);

linearly interpolate with other_vec

auto result_vec = vec.lerp (other_vec, 0.75);

linearly interpolate with other_vec unclamped

unclamped means that the value is not restricted between 0.0 and 1.0.

auto result_vec = vec.lerp_unclamped (other_vec, 1.5);

reflect off surface with normal_vec

auto result_vec = vec.reflected (normal_vec);

distance to other_vec

auto result_vec = vec.distance (other_vec);

squared distance to other_vec

auto result_vec = vec.sqr_distance (other_vec);

magnitude

double mag = vec.magnitude ();

squared magnitude

double sqr_mag = vec.sqr_magnitude ();

dot product with other_vec

auto result_vec = vec.dot (other_vec);

square root

auto result_vec = vec.square_root ();

normalize

result_vec = vec.normalized ();