Vector2d
The Vector2d provides a container for a 2-dimensional vector with associated math operations implemented.
API Reference
Header File
Classes
-
template<typename T>
class Vector2d Container representing a 2 dimensional vector.
Provides getters/setters, index operator, and vector / scalar math utilities.
Example
fmt::print("--- uint8_t vector ---\n"); espp::Vector2u8 v8(2, 3); fmt::print("original: {}\n", v8); auto v8_2 = v8; v8 = uint8_t(2) * v8; // NOTE: need explicit cast to avoid ambiguity with auto type v8 /= 2; v8 += espp::Vector2u8(0, 1); v8 -= espp::Vector2u8(0, 1); fmt::print("should be same: {}, {}\n", v8, v8_2); // for good measure, print all the comparisons fmt::print(" v == v2: {}\n", v8 == v8_2); fmt::print(" v != v2: {}\n", v8 != v8_2); fmt::print(" v < v2: {}\n", v8 < v8_2); fmt::print(" v <= v2: {}\n", v8 <= v8_2); fmt::print(" v > v2: {}\n", v8 > v8_2); fmt::print(" v >= v2: {}\n", v8 >= v8_2); fmt::print("magnitude: {}\n", v8.magnitude()); fmt::print("normalized: {}\n", v8.normalized()); fmt::print("--- float vector ---\n"); espp::Vector2f v(1, 1); fmt::print("original: {}\n", v); auto v2 = v; v = 2.0f * v; v /= 2.0f; v += espp::Vector2f(0, 1); v -= espp::Vector2f(0, 1); fmt::print("should be same: {}, {}\n", v, v2); // for good measure, print all the comparisons fmt::print(" v == v2: {}\n", v == v2); fmt::print(" v != v2: {}\n", v != v2); fmt::print(" v < v2: {}\n", v < v2); fmt::print(" v <= v2: {}\n", v <= v2); fmt::print(" v > v2: {}\n", v > v2); fmt::print(" v >= v2: {}\n", v >= v2); fmt::print("magnitude: {}\n", v.magnitude()); fmt::print("normalized: {}\n", v.normalized()); fmt::print("norm mag: {}\n", v.normalized().magnitude()); fmt::print("rotated -pi/2: {}\n", v.rotated(-M_PI_2)); // now compute the angle and signed angle between two vectors auto rotated = v.rotated(-M_PI_2); fmt::print("angle: {}\n", v.angle(rotated)); fmt::print("signed angle: {}\n", v.signed_angle(rotated)); fmt::print("actual angle: {}\n", -M_PI_2); // now test inv_magnitude on it fmt::print("inv_magnitude: {}\n", rotated.inv_magnitude()); // and show that it produces the correct normalized vector fmt::print("normalized (fast): {}\n", rotated * rotated.inv_magnitude()); // and that that's (close enough to) the same as the normalized vector fmt::print("normalized (slow): {}\n", rotated.normalized());
Public Functions
-
inline explicit Vector2d(T x = 0, T y = 0)
Constructor for the vector, defaults to 0,0.
- Parameters
x – The starting X value.
y – The starting Y value.
-
inline Vector2d &operator=(const Vector2d &other)
Assignment operator.
- Parameters
other – Vector to assign to this vector.
- Returns
This vector, updated to be a copy of
other
.
-
inline T inv_magnitude() const
Returns the inverse magnitude of the vector.
This function is more efficient than 1.0 / magnitude() as it uses a fast inverse square root.
Returns vector magnitude: ||v||.
Returns vector magnitude squared: ||v||^2.
Getter for the x value.
Setter for the x value.
Note
This function is only available if T is a floating point value.
- Returns
The inverse magnitude.
- Parameters
v – New value for
x
.- Returns
The magnitude.
- Returns
The magnitude squared.
- Returns
The current x value.
-
inline int operator<=>(const Vector2d &other) const
Spaceship operator for comparing two vectors.
- Parameters
other – The vector to compare against.
- Returns
-1 if this vector is less than
other
, 0 if they are equal, 1 if this vector is greater thanother
.
-
inline bool operator==(const Vector2d &other) const
Equality operator for comparing two vectors.
- Parameters
other – The vector to compare against.
- Returns
True if the vectors are equal, false otherwise.
-
inline T &operator[](int index)
Index operator for vector elements.
Note
Returns a mutable reference to the element.
- Parameters
index – The index to return.
- Returns
Mutable reference to the element at
index
.
-
inline Vector2d operator-(const Vector2d &rhs) const
Return a new vector which is the provided vector subtracted from this vector.
- Parameters
rhs – The vector to subtract from this vector.
- Returns
Resultant vector subtraction.
-
inline Vector2d &operator-=(const Vector2d &rhs)
Return the provided vector subtracted from this vector.
- Parameters
rhs – The vector to subtract from this vector.
- Returns
Resultant vector subtraction.
-
inline Vector2d operator+(const Vector2d &rhs) const
Return a new vector, which is the addition of this vector and the provided vector.
- Parameters
rhs – The vector to add to this vector.
- Returns
Resultant vector addition.
-
inline Vector2d &operator+=(const Vector2d &rhs)
Return the vector added with the provided vector.
- Parameters
rhs – The vector to add to this vector.
- Returns
Resultant vector addition.
-
inline Vector2d operator*(const T &v) const
Return a scaled version of the vector, multiplied by the provided value.
- Parameters
v – Value the vector should be multiplied by.
- Returns
Resultant scaled vector.
-
inline Vector2d &operator*=(const T &v)
Return the vector multiplied by the provided value.
- Parameters
v – Value the vector should be scaled by.
- Returns
Resultant scaled vector.
-
inline Vector2d operator/(const T &v) const
Return a scaled version of the vector, divided by the provided value.
- Parameters
v – Value the vector should be divided by.
- Returns
Resultant scaled vector.
-
inline Vector2d &operator/=(const T &v)
Return the vector divided by the provided value.
- Parameters
v – Value the vector should be divided by.
- Returns
Resultant scaled vector.
-
inline Vector2d operator/(const Vector2d &v) const
Return a scaled version of the vector, divided by the provided vector value. Scales x and y independently.
- Parameters
v – Vector values the vector should be divided by.
- Returns
Resultant scaled vector.
-
inline explicit Vector2d(T x = 0, T y = 0)