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(const Vector2d &other)#
Vector copy constructor.
- Parameters:
other – Vector to copy.
-
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.
Note
This function is only available if T is a floating point value.
- Returns:
The inverse magnitude.
-
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#
Negate the vector.
- Returns:
The new vector which is the negative.
-
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 T angle(const Vector2d &other) const#
Angle between this vector and another vector.
Note
This function is only available if T is a floating point value.
- Parameters:
other – The second vector
- Returns:
The angle between the two vectors in radians. Returns 0 if either vector is a zero vector. Returns a value in the range [0, pi].
-
inline T signed_angle(const Vector2d &other) const#
Signed angle between this vector and another vector.
The angle is calculated using atan2 of the cross product and dot product of the two vectors. This method is more numerically stable than acos(dot(v1, v2) / (||v1|| * ||v2||)) and produces a result in the range of [-pi, pi].
Note
This function is only available if T is a floating point value.
- Parameters:
other – The second vector
- Returns:
The angle between the two vectors in radians. Returns 0 if either vector is a zero vector. Returns a value in the range [-pi, pi].
-
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)#