Skip to content

Commit

Permalink
mark vector functions as constexpr
Browse files Browse the repository at this point in the history
  • Loading branch information
toxicdefender404 committed Dec 28, 2024
1 parent 2c93ea3 commit ec38df6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 48 deletions.
48 changes: 24 additions & 24 deletions include/units/Vector2D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ template <isQuantity T> class Vector2D {
* This constructor initializes x and y to 0
*
*/
Vector2D() : x(0.0), y(0.0) {}
constexpr Vector2D() : x(0.0), y(0.0) {}

/**
* @brief Construct a new Vector2D object
Expand All @@ -32,7 +32,7 @@ template <isQuantity T> class Vector2D {
* @param nx x component
* @param ny y component
*/
Vector2D(T nx, T ny) : x(nx), y(ny) {}
constexpr Vector2D(T nx, T ny) : x(nx), y(ny) {}

/**
* @brief Create a new Vector2D object from polar coordinates
Expand All @@ -42,7 +42,7 @@ template <isQuantity T> class Vector2D {
* @param t angle
* @param m magnitude
*/
static Vector2D fromPolar(Angle t, T m) {
constexpr static Vector2D fromPolar(Angle t, T m) {
m = abs(m);
t = constrainAngle360(t);
return Vector2D<T>(m * cos(t), m * sin(t));
Expand All @@ -54,7 +54,7 @@ template <isQuantity T> class Vector2D {
* @param t angle
* @return Vector2D
*/
static Vector2D unitVector(Angle t) { return fromPolar(t, (T)1.0); }
constexpr static Vector2D unitVector(Angle t) { return fromPolar(t, (T)1.0); }

/**
* @brief + operator overload
Expand All @@ -65,7 +65,7 @@ template <isQuantity T> class Vector2D {
* @param other vector to add
* @return Vector2D<T>
*/
Vector2D<T> operator+(const Vector2D<T>& other) const { return Vector2D<T>(x + other.x, y + other.y); }
constexpr Vector2D<T> operator+(const Vector2D<T>& other) const { return Vector2D<T>(x + other.x, y + other.y); }

/**
* @brief - operator overload
Expand All @@ -76,7 +76,7 @@ template <isQuantity T> class Vector2D {
* @param other vector to subtract
* @return Vector2D<T>
*/
Vector2D<T> operator-(const Vector2D<T>& other) const { return Vector2D<T>(x - other.x, y - other.y); }
constexpr Vector2D<T> operator-(const Vector2D<T>& other) const { return Vector2D<T>(x - other.x, y - other.y); }

/**
* @brief * operator overload
Expand All @@ -87,7 +87,7 @@ template <isQuantity T> class Vector2D {
* @param factor scalar to multiply by
* @return Vector2D<T>
*/
Vector2D<T> operator*(double factor) const { return Vector2D<T>(x * factor, y * factor); }
constexpr Vector2D<T> operator*(double factor) const { return Vector2D<T>(x * factor, y * factor); }

/**
* @brief / operator overload
Expand All @@ -98,7 +98,7 @@ template <isQuantity T> class Vector2D {
* @param factor scalar to divide by
* @return Vector2D<T>
*/
Vector2D<T> operator/(double factor) const { return Vector2D<T>(x / factor, y / factor); }
constexpr Vector2D<T> operator/(double factor) const { return Vector2D<T>(x / factor, y / factor); }

/**
* @brief += operator overload
Expand All @@ -109,7 +109,7 @@ template <isQuantity T> class Vector2D {
* @param other vector to add
* @return Vector2D<T>&
*/
Vector2D<T>& operator+=(const Vector2D<T>& other) {
constexpr Vector2D<T>& operator+=(const Vector2D<T>& other) {
x += other.x;
y += other.y;
return (*this);
Expand All @@ -124,7 +124,7 @@ template <isQuantity T> class Vector2D {
* @param other vector to subtract
* @return Vector2D<T>&
*/
Vector2D<T>& operator-=(const Vector2D<T>& other) {
constexpr Vector2D<T>& operator-=(const Vector2D<T>& other) {
x -= other.x;
y -= other.y;
return (*this);
Expand All @@ -140,7 +140,7 @@ template <isQuantity T> class Vector2D {
* @param factor scalar to multiply by
* @return Vector2D<T>&
*/
Vector2D<T>& operator*=(double factor) {
constexpr Vector2D<T>& operator*=(double factor) {
x *= factor;
y *= factor;
return (*this);
Expand All @@ -156,7 +156,7 @@ template <isQuantity T> class Vector2D {
* @param factor scalar to divide by
* @return Vector2D<T>&
*/
Vector2D<T>& operator/=(double factor) {
constexpr Vector2D<T>& operator/=(double factor) {
x /= factor;
y /= factor;
return (*this);
Expand All @@ -173,7 +173,7 @@ template <isQuantity T> class Vector2D {
* @param other the vector to calculate the dot product with
* @return R the dot product
*/
template <isQuantity Q, isQuantity R = Multiplied<T, Q>> R dot(const Vector2D<Q>& other) const {
template <isQuantity Q, isQuantity R = Multiplied<T, Q>> constexpr R dot(const Vector2D<Q>& other) const {
return (x * other.x) + (y * other.y);
}

Expand All @@ -188,7 +188,7 @@ template <isQuantity T> class Vector2D {
* @param other the vector to calculate the cross product with
* @return R the cross product
*/
template <isQuantity Q, isQuantity R = Multiplied<T, Q>> R cross(const Vector2D<Q>& other) const {
template <isQuantity Q, isQuantity R = Multiplied<T, Q>> constexpr R cross(const Vector2D<Q>& other) const {
return (x * other.y) - (y * other.x);
}

Expand All @@ -197,14 +197,14 @@ template <isQuantity T> class Vector2D {
*
* @return Angle
*/
Angle theta() const { return atan2(y, x); }
constexpr Angle theta() const { return atan2(y, x); }

/**
* @brief magnitude of the vector
*
* @return T
*/
T magnitude() const { return sqrt(square(x) + square(y)); }
constexpr T magnitude() const { return sqrt(square(x) + square(y)); }

/**
* @brief difference between two vectors
Expand All @@ -215,23 +215,23 @@ template <isQuantity T> class Vector2D {
* @param other the other vector
* @return Vector2D<T>
*/
Vector2D<T> vectorTo(const Vector2D<T>& other) const { return Vector2D<T>(other.x - x, other.y - y); }
constexpr Vector2D<T> vectorTo(const Vector2D<T>& other) const { return Vector2D<T>(other.x - x, other.y - y); }

/**
* @brief the angle between two vectors
*
* @param other the other vector
* @return Angle
*/
Angle angleTo(const Vector2D<T>& other) const { return atan2(other.y - y, other.x - x); }
constexpr Angle angleTo(const Vector2D<T>& other) const { return atan2(other.y - y, other.x - x); }

/**
* @brief get the distance between two vectors
*
* @param other the other vector
* @return T
*/
T distanceTo(const Vector2D<T>& other) const { return sqrt(square(x - other.x, 2) + square(y - other.y, 2)); }
constexpr T distanceTo(const Vector2D<T>& other) const { return sqrt(square(x - other.x, 2) + square(y - other.y, 2)); }

/**
* @brief normalize the vector
Expand All @@ -240,7 +240,7 @@ template <isQuantity T> class Vector2D {
*
* @return Vector2D<T>
*/
Vector2D<T> normalize() const {
constexpr Vector2D<T> normalize() const {
T m = magnitude();
return Vector2D<T>(x / m, y / m);
}
Expand All @@ -250,7 +250,7 @@ template <isQuantity T> class Vector2D {
*
* @param angle
*/
void rotateBy(Angle angle) {
constexpr void rotateBy(Angle angle) {
T m = magnitude();
Angle t = theta() + angle;
x = m * cos(t);
Expand All @@ -262,7 +262,7 @@ template <isQuantity T> class Vector2D {
*
* @param angle
*/
void rotateTo(Angle angle) {
constexpr void rotateTo(Angle angle) {
T m = magnitude();
x = m * cos(angle);
y = m * sin(angle);
Expand All @@ -274,7 +274,7 @@ template <isQuantity T> class Vector2D {
* @param angle
* @return Vector2D<T>
*/
Vector2D<T> rotatedBy(Angle angle) const {
constexpr Vector2D<T> rotatedBy(Angle angle) const {
T m = magnitude();
Angle t = theta() + angle;
return fromPolar(t, m);
Expand All @@ -286,7 +286,7 @@ template <isQuantity T> class Vector2D {
* @param angle
* @return Vector2D<T>
*/
Vector2D<T> rotatedTo(Angle angle) const {
constexpr Vector2D<T> rotatedTo(Angle angle) const {
T m = magnitude();
return fromPolar(angle, m);
}
Expand Down
Loading

0 comments on commit ec38df6

Please sign in to comment.