From ec38df6750498b1081889bd9741944a605368b51 Mon Sep 17 00:00:00 2001 From: toxicdefender404 Date: Sat, 28 Dec 2024 00:05:32 +0000 Subject: [PATCH] mark vector functions as constexpr --- include/units/Vector2D.hpp | 48 +++++++++++++++++++------------------- include/units/Vector3D.hpp | 48 +++++++++++++++++++------------------- 2 files changed, 48 insertions(+), 48 deletions(-) diff --git a/include/units/Vector2D.hpp b/include/units/Vector2D.hpp index 3e0fa55..40aa850 100644 --- a/include/units/Vector2D.hpp +++ b/include/units/Vector2D.hpp @@ -22,7 +22,7 @@ template 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 @@ -32,7 +32,7 @@ template 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 @@ -42,7 +42,7 @@ template 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(m * cos(t), m * sin(t)); @@ -54,7 +54,7 @@ template 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 @@ -65,7 +65,7 @@ template class Vector2D { * @param other vector to add * @return Vector2D */ - Vector2D operator+(const Vector2D& other) const { return Vector2D(x + other.x, y + other.y); } + constexpr Vector2D operator+(const Vector2D& other) const { return Vector2D(x + other.x, y + other.y); } /** * @brief - operator overload @@ -76,7 +76,7 @@ template class Vector2D { * @param other vector to subtract * @return Vector2D */ - Vector2D operator-(const Vector2D& other) const { return Vector2D(x - other.x, y - other.y); } + constexpr Vector2D operator-(const Vector2D& other) const { return Vector2D(x - other.x, y - other.y); } /** * @brief * operator overload @@ -87,7 +87,7 @@ template class Vector2D { * @param factor scalar to multiply by * @return Vector2D */ - Vector2D operator*(double factor) const { return Vector2D(x * factor, y * factor); } + constexpr Vector2D operator*(double factor) const { return Vector2D(x * factor, y * factor); } /** * @brief / operator overload @@ -98,7 +98,7 @@ template class Vector2D { * @param factor scalar to divide by * @return Vector2D */ - Vector2D operator/(double factor) const { return Vector2D(x / factor, y / factor); } + constexpr Vector2D operator/(double factor) const { return Vector2D(x / factor, y / factor); } /** * @brief += operator overload @@ -109,7 +109,7 @@ template class Vector2D { * @param other vector to add * @return Vector2D& */ - Vector2D& operator+=(const Vector2D& other) { + constexpr Vector2D& operator+=(const Vector2D& other) { x += other.x; y += other.y; return (*this); @@ -124,7 +124,7 @@ template class Vector2D { * @param other vector to subtract * @return Vector2D& */ - Vector2D& operator-=(const Vector2D& other) { + constexpr Vector2D& operator-=(const Vector2D& other) { x -= other.x; y -= other.y; return (*this); @@ -140,7 +140,7 @@ template class Vector2D { * @param factor scalar to multiply by * @return Vector2D& */ - Vector2D& operator*=(double factor) { + constexpr Vector2D& operator*=(double factor) { x *= factor; y *= factor; return (*this); @@ -156,7 +156,7 @@ template class Vector2D { * @param factor scalar to divide by * @return Vector2D& */ - Vector2D& operator/=(double factor) { + constexpr Vector2D& operator/=(double factor) { x /= factor; y /= factor; return (*this); @@ -173,7 +173,7 @@ template class Vector2D { * @param other the vector to calculate the dot product with * @return R the dot product */ - template > R dot(const Vector2D& other) const { + template > constexpr R dot(const Vector2D& other) const { return (x * other.x) + (y * other.y); } @@ -188,7 +188,7 @@ template class Vector2D { * @param other the vector to calculate the cross product with * @return R the cross product */ - template > R cross(const Vector2D& other) const { + template > constexpr R cross(const Vector2D& other) const { return (x * other.y) - (y * other.x); } @@ -197,14 +197,14 @@ template 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 @@ -215,7 +215,7 @@ template class Vector2D { * @param other the other vector * @return Vector2D */ - Vector2D vectorTo(const Vector2D& other) const { return Vector2D(other.x - x, other.y - y); } + constexpr Vector2D vectorTo(const Vector2D& other) const { return Vector2D(other.x - x, other.y - y); } /** * @brief the angle between two vectors @@ -223,7 +223,7 @@ template class Vector2D { * @param other the other vector * @return Angle */ - Angle angleTo(const Vector2D& other) const { return atan2(other.y - y, other.x - x); } + constexpr Angle angleTo(const Vector2D& other) const { return atan2(other.y - y, other.x - x); } /** * @brief get the distance between two vectors @@ -231,7 +231,7 @@ template class Vector2D { * @param other the other vector * @return T */ - T distanceTo(const Vector2D& other) const { return sqrt(square(x - other.x, 2) + square(y - other.y, 2)); } + constexpr T distanceTo(const Vector2D& other) const { return sqrt(square(x - other.x, 2) + square(y - other.y, 2)); } /** * @brief normalize the vector @@ -240,7 +240,7 @@ template class Vector2D { * * @return Vector2D */ - Vector2D normalize() const { + constexpr Vector2D normalize() const { T m = magnitude(); return Vector2D(x / m, y / m); } @@ -250,7 +250,7 @@ template class Vector2D { * * @param angle */ - void rotateBy(Angle angle) { + constexpr void rotateBy(Angle angle) { T m = magnitude(); Angle t = theta() + angle; x = m * cos(t); @@ -262,7 +262,7 @@ template class Vector2D { * * @param angle */ - void rotateTo(Angle angle) { + constexpr void rotateTo(Angle angle) { T m = magnitude(); x = m * cos(angle); y = m * sin(angle); @@ -274,7 +274,7 @@ template class Vector2D { * @param angle * @return Vector2D */ - Vector2D rotatedBy(Angle angle) const { + constexpr Vector2D rotatedBy(Angle angle) const { T m = magnitude(); Angle t = theta() + angle; return fromPolar(t, m); @@ -286,7 +286,7 @@ template class Vector2D { * @param angle * @return Vector2D */ - Vector2D rotatedTo(Angle angle) const { + constexpr Vector2D rotatedTo(Angle angle) const { T m = magnitude(); return fromPolar(angle, m); } diff --git a/include/units/Vector3D.hpp b/include/units/Vector3D.hpp index 7fb1b4a..0e3f47d 100644 --- a/include/units/Vector3D.hpp +++ b/include/units/Vector3D.hpp @@ -21,7 +21,7 @@ template class Vector3D { * * This constructor initializes x, y, and z to 0 */ - Vector3D() : x(0.0), y(0.0), z(0.0) {} + constexpr Vector3D() : x(0.0), y(0.0), z(0.0) {} /** * @brief Construct a new Vector2D object @@ -32,7 +32,7 @@ template class Vector3D { * @param ny y component * @param nz z component */ - Vector3D(T nx, T ny, T nz) : x(nx), y(ny), z(nz) {} + constexpr Vector3D(T nx, T ny, T nz) : x(nx), y(ny), z(nz) {} /** * @brief Create a new Vector3D object from spherical coordinates @@ -42,7 +42,7 @@ template class Vector3D { * @param t angle * @param m magnitude */ - static Vector3D fromPolar(const Vector3D& t, T m) { + constexpr static Vector3D fromPolar(const Vector3D& t, T m) { m = m.abs(); return Vector3D(m * cos(t.x), m * cos(t.y), m * cos(t.z)); } @@ -53,7 +53,7 @@ template class Vector3D { * @param t angle * @return Vector3D */ - static Vector3D unitVector(const Vector3D& t) { return fromPolar(t, (T)1.0); } + constexpr static Vector3D unitVector(const Vector3D& t) { return fromPolar(t, (T)1.0); } /** * @brief + operator overload @@ -64,7 +64,7 @@ template class Vector3D { * @param other vector to add * @return Vector3D */ - Vector3D operator+(const Vector3D& other) const { + constexpr Vector3D operator+(const Vector3D& other) const { return Vector3D(x + other.x, y + other.y, z + other.z); } @@ -77,7 +77,7 @@ template class Vector3D { * @param other vector to subtract * @return Vector3D */ - Vector3D operator-(const Vector3D& other) const { + constexpr Vector3D operator-(const Vector3D& other) const { return Vector3D(x - other.x, y - other.y, z - other.z); } @@ -90,7 +90,7 @@ template class Vector3D { * @param factor scalar to multiply by * @return Vector3D */ - Vector3D operator*(double factor) const { return Vector3D(x * factor, y * factor, z * factor); } + constexpr Vector3D operator*(double factor) const { return Vector3D(x * factor, y * factor, z * factor); } /** * @brief / operator overload @@ -101,7 +101,7 @@ template class Vector3D { * @param factor scalar to divide by * @return Vector3D */ - Vector3D operator/(double factor) const { return Vector3D(x / factor, y / factor, z / factor); } + constexpr Vector3D operator/(double factor) const { return Vector3D(x / factor, y / factor, z / factor); } /** * @brief += operator overload @@ -112,7 +112,7 @@ template class Vector3D { * @param other vector to add * @return Vector3D& */ - Vector3D& operator+=(const Vector3D& other) { + constexpr Vector3D& operator+=(const Vector3D& other) { x += other.x; y += other.y; z += other.z; @@ -128,7 +128,7 @@ template class Vector3D { * @param other vector to subtract * @return Vector3D& */ - Vector3D& operator-=(const Vector3D& other) { + constexpr Vector3D& operator-=(const Vector3D& other) { x -= other.x; y -= other.y; z -= other.z; @@ -145,7 +145,7 @@ template class Vector3D { * @param factor scalar to multiply by * @return Vector3D& */ - Vector3D& operator*=(double factor) { + constexpr Vector3D& operator*=(double factor) { x *= factor; y *= factor; z *= factor; @@ -162,7 +162,7 @@ template class Vector3D { * @param factor scalar to divide by * @return Vector3D& */ - Vector3D& operator/=(double factor) { + constexpr Vector3D& operator/=(double factor) { x /= factor; y /= factor; z /= factor; @@ -180,7 +180,7 @@ template class Vector3D { * @param other the vector to calculate the dot product with * @return R the dot product */ - template > R dot(const Vector3D& other) const { + template > constexpr R dot(const Vector3D& other) const { return (x * other.x) + (y * other.y) + (z * other.z); } @@ -197,7 +197,7 @@ template class Vector3D { * @param other the vector to calculate the cross product with * @return Vector3D the cross product */ - template > Vector3D cross(const Vector3D& other) const { + template > constexpr Vector3D cross(const Vector3D& other) const { return Vector3D(y * other.z - z * other.y, z * other.x - x * other.z, x * other.y - y * other.x); } @@ -206,7 +206,7 @@ template class Vector3D { * * @return Angle */ - Vector3D theta() const { + constexpr Vector3D theta() const { const T mag = magnitude(); return Vector3D(acos(x / mag), acos(y / mag), acos(z / mag)); } @@ -216,7 +216,7 @@ template class Vector3D { * * @return T */ - T magnitude() const { return sqrt(square(x) + square(y) + square(z)); } + constexpr T magnitude() const { return sqrt(square(x) + square(y) + square(z)); } /** * @brief difference between two vectors @@ -229,7 +229,7 @@ template class Vector3D { * @param other the other vector * @return Vector3D */ - Vector3D vectorTo(const Vector3D& other) const { + constexpr Vector3D vectorTo(const Vector3D& other) const { return Vector2D(other.x - x, other.y - y, other.z - z); } @@ -239,7 +239,7 @@ template class Vector3D { * @param other the other vector * @return Angle */ - Angle angleTo(const Vector3D& other) const { + constexpr Angle angleTo(const Vector3D& other) const { return units::acos(dot(other) / (magnitude() * other.magnitude())); } @@ -249,7 +249,7 @@ template class Vector3D { * @param other the other vector * @return T */ - T distanceTo(const Vector3D& other) const { return vectorTo(other).magnitude(); } + constexpr T distanceTo(const Vector3D& other) const { return vectorTo(other).magnitude(); } /** * @brief normalize the vector @@ -258,7 +258,7 @@ template class Vector3D { * * @return Vector3D */ - Vector3D normalize() { + constexpr Vector3D normalize() { T m = magnitude(); return Vector2D(x / m, y / m, z / m); } @@ -268,7 +268,7 @@ template class Vector3D { * * @param angle */ - void rotateBy(const Vector3D& angle) { + constexpr void rotateBy(const Vector3D& angle) { const T m = magnitude(); const Vector3D t = theta() + angle; x = m * cos(t.x); @@ -281,7 +281,7 @@ template class Vector3D { * * @param angle */ - void rotateTo(const Vector3D& angle) { + constexpr void rotateTo(const Vector3D& angle) { const T m = magnitude(); x = m * cos(angle.x); y = m * cos(angle.y); @@ -294,7 +294,7 @@ template class Vector3D { * @param angle * @return Vector3D */ - Vector3D rotatedBy(const Vector3D& angle) const { + constexpr Vector3D rotatedBy(const Vector3D& angle) const { T m = magnitude(); Angle t = theta() + angle; return fromPolar(t, m); @@ -306,7 +306,7 @@ template class Vector3D { * @param angle * @return Vector3D */ - Vector3D rotatedTo(Angle angle) const { + constexpr Vector3D rotatedTo(Angle angle) const { T m = magnitude(); return fromPolar(angle, m); }