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));
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 magnitude_squared() const
Returns vector magnitude squared: ||v||^2.
- Returns
The magnitude squared.
-
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 Vector2d &operator/=(const Vector2d &v)
Return the vector divided by the provided vector values.
- Parameters
v – Vector of values the vector should be divided by.
- Returns
Resultant scaled vector.
-
inline T dot(const Vector2d &other) const
Dot product of this vector with another vector.
- Parameters
other – The second vector
- Returns
The dot product (x1*x2 + y1*y2)
-
inline Vector2d normalized() const
Return normalized (unit length) version of the vector.
- Returns
The normalized vector.
-
template<class U = T, typename std::enable_if<std::is_floating_point<U>::value>::type* = nullptr>
inline Vector2d rotated(T radians) const Rotate the vector by
radians
.Note
This function is only available if T is a floating point value.
- Parameters
radians – Amount of rotation (in radians) to rotate the vector by.
- Returns
Rotated vector.
-
inline explicit Vector2d(T x = 0, T y = 0)