From ffdace3e07f09da003a0add75188f6b5ef70c3c0 Mon Sep 17 00:00:00 2001 From: Liam Teale Date: Fri, 27 Dec 2024 21:00:36 -0800 Subject: [PATCH] improve vector operator overloads --- include/units/Vector2D.hpp | 115 ++++++++++++++++++++----------------- include/units/Vector3D.hpp | 79 ++++++++++++++----------- 2 files changed, 105 insertions(+), 89 deletions(-) diff --git a/include/units/Vector2D.hpp b/include/units/Vector2D.hpp index 0c51d97..58fe019 100644 --- a/include/units/Vector2D.hpp +++ b/include/units/Vector2D.hpp @@ -57,9 +57,8 @@ template class Vector2D { constexpr static Vector2D unitVector(Angle t) { return fromPolar(t, (T)1.0); } /** - * @brief + operator overload + * @brief + operator overload. Adds the x and y components of two vectors * - * This operator adds the x and y components of two vectors * {a, b} + {c, d} = {a + c, b + d} * * @param other vector to add @@ -70,9 +69,8 @@ template class Vector2D { } /** - * @brief - operator overload + * @brief - operator overload. Substracts the x and y components of two vectors * - * This operator subtracts the x and y components of two vectors * {a, b} - {c, d} = {a - c, b - d} * * @param other vector to subtract @@ -83,31 +81,52 @@ template class Vector2D { } /** - * @brief * operator overload + * @brief * operator overload. Multiplies a vector by a quantity * - * This operator multiplies the x and y components of a vector by a scalar - * a * {b, c} = {a * b, a * c} + * {a, b} * c = {a * c, b * c} * - * @param factor scalar to multiply by - * @return Vector2D + * @tparam Q the type of quantity to multiply the vector with + * @tparam R the quantity type of the resulting vector + * + * @param factor the quantity to multiple the vector by + * @return Vector2D */ - constexpr Vector2D operator*(double factor) const { return Vector2D(this->x * factor, this->y * factor); } + template > constexpr Vector2D operator*(Q factor) const { + return Vector2D(this->x * factor, this->y * factor); + } /** - * @brief / operator overload + * @brief * operator overload. Finds the dot product of 2 Vector2D objects + * + * {a, b} * {c, d} = (a * c) + (b * d) + * + * @tparam Q the type of quantity to use for the other vector + * @tparam R the type of quantity to use for the result + * @param other the vector to calculate the dot product with + * @return R the dot product + */ + template > constexpr R operator*(const Vector2D& other) const { + return (this->x * other.x) + (this->y * other.y); + } + + /** + * @brief / operator overload. Divides a vector by a quantity * - * This operator divides the x and y components of a vector by a scalar * {a, b} / c = {a / c, b / c} * - * @param factor scalar to divide by + * @tparam Q the type of quantity to divide the vector with + * @tparam R the quantity type of the resulting vector + * + * @param factor the quantity to divide the vector by * @return Vector2D */ - constexpr Vector2D operator/(double factor) const { return Vector2D(this->x / factor, this->y / factor); } + template > Vector2D constexpr operator/(Q factor) const { + return Vector2D(this->x / factor, this->y / factor); + } /** - * @brief += operator overload + * @brief += operator overload. Adds the components of two vectors and stores the result * - * This operator adds the x and y components of two vectors and stores the result in the calling vector * {a, b} += {c, d} => {a + c, b + d} * * @param other vector to add @@ -120,9 +139,8 @@ template class Vector2D { } /** - * @brief -= operator overload + * @brief -= operator overload. Subtracts the components of two vectors and stores the result * - * This operator subtracts the x and y components of two vectors and stores the result in the calling vector * {a, b} -= {c, d} => {a - c, b - d} * * @param other vector to subtract @@ -135,11 +153,9 @@ template class Vector2D { } /** - * @brief *= operator overload + * @brief *= operator overload. Multiplies the components of a vector by a scalar and stores the result * - * This operator multiplies the x and y components of a vector by a scalar and stores the result in the - * calling vector - * a *= {b, c} => {a * b, a * c} + * {a, b} *= c => {a * c, b * c} * * @param factor scalar to multiply by * @return Vector2D& @@ -151,10 +167,8 @@ template class Vector2D { } /** - * @brief /= operator overload + * @brief /= operator overload. Divides the components of a vector by a scalar and stores the result * - * This operator divides the x and y components of a vector by a scalar and stores the result in the - * calling vector * {a, b} /= c => {a / c, b / c} * * @param factor scalar to divide by @@ -167,22 +181,7 @@ template class Vector2D { } /** - * @brief dot product of 2 Vector2D objects - * - * This function calculates the dot product of two vectors - * a.dot(b) = (a.x * b.x) + (a.y * b.y) - * - * @tparam Q the type of quantity to use for the other vector - * @tparam R the type of quantity to use for the result - * @param other the vector to calculate the dot product with - * @return R the dot product - */ - template > constexpr R dot(const Vector2D& other) const { - return (this->x * other.x) + (this->y * other.y); - } - - /** - * @brief angle of the vector + * @brief angle of the vector from the origin * * @return Angle */ @@ -233,10 +232,7 @@ template class Vector2D { * * @return Vector2D */ - constexpr Vector2D normalize() const { - const T m = magnitude(); - return Vector2D(this->x / m, this->y / m); - } + constexpr Vector2D normalize() const { return (*this) / magnitude(); } /** * @brief rotate the vector by an angle @@ -267,11 +263,7 @@ template class Vector2D { * @param angle * @return Vector2D */ - constexpr Vector2D rotatedBy(Angle angle) const { - const T m = magnitude(); - const Angle t = theta() + angle; - return fromPolar(t, m); - } + constexpr Vector2D rotatedBy(Angle angle) const { return fromPolar(theta() + angle, magnitude()); } /** * @brief get a copy of this vector rotated to an angle @@ -279,12 +271,27 @@ template class Vector2D { * @param angle * @return Vector2D */ - constexpr Vector2D rotatedTo(Angle angle) const { - const T m = magnitude(); - return fromPolar(angle, m); - } + constexpr Vector2D rotatedTo(Angle angle) const { return fromPolar(angle, magnitude()); } }; +/** + * @brief * operator overload. Multiplies a scalar and a vector + * + * a * {b, c} = {a * b, a * c} + * + * @tparam Q1 the quantity type of the scalar + * @tparam Q2 the quantity type of the vector + * @tparam Q3 the type of quantity to use for the result + * + * @param lhs the scalar on the left hand side + * @param rhs the vector on the right hand side + * @return Q3 the product + */ +template > +constexpr Vector2D operator*(Q1 lhs, const Vector2D& rhs) { + return rhs * lhs; +} + // define some common vector types typedef Vector2D V2Position; typedef Vector2D V2Velocity; diff --git a/include/units/Vector3D.hpp b/include/units/Vector3D.hpp index 4b3a4dd..6cf211e 100644 --- a/include/units/Vector3D.hpp +++ b/include/units/Vector3D.hpp @@ -56,9 +56,8 @@ template class Vector3D { constexpr static Vector3D unitVector(const Vector3D& t) { return fromPolar(t, (T)1.0); } /** - * @brief + operator overload + * @brief + operator overload. Adds the components of two vectors * - * This operator adds the x, y, and z components of two vectors * {a, b, c} + {d, e, f} = {a + d, b + e, c + f} * * @param other vector to add @@ -69,9 +68,8 @@ template class Vector3D { } /** - * @brief - operator overload + * @brief - operator overload. Subtracts the components of two vectors * - * This operator subtracts the x, y, and z components of two vectors * {a, b, c} - {d, e, f} = {a - d, b - e, c - f} * * @param other vector to subtract @@ -82,35 +80,38 @@ template class Vector3D { } /** - * @brief * operator overload + * @brief * operator overload. Multiplies a vector by a quantity * - * This operator multiplies the x, y, and z components of a vector by a scalar - * a * {b, c, d} = {a * b, a * c, a * d} + * {a, b, c} * d = {a * d, b * d, c * d} * - * @param factor scalar to multiply by - * @return Vector3D + * @tparam Q the type of quantity to multiply the vector with + * @tparam R the quantity type of the resulting vector + * + * @param factor the quantity to multiple the vector by + * @return Vector3D */ - constexpr Vector3D operator*(double factor) const { - return Vector3D(this->x * factor, this->y * factor, this->z * factor); + template > constexpr Vector3D operator*(Q factor) const { + return Vector3D(this->x * factor, this->y * factor, this->z * factor); } /** - * @brief / operator overload + * @brief / operator overload. Multiplies a vector by a quantity * - * This operator divides the x, y, and z components of a vector by a scalar * {a, b, c} / d = {a / d, b / d, c / d} * - * @param factor scalar to divide by - * @return Vector3D + * @tparam Q the type of quantity to multiply the vector with + * @tparam R the quantity type of the resulting vector + * + * @param factor the quantity to multiple the vector by + * @return Vector3D */ - constexpr Vector3D operator/(double factor) const { - return Vector3D(this->x / factor, this->y / factor, this->z / factor); + template > constexpr Vector3D operator/(Q factor) const { + return Vector3D(this->x / factor, this->y / factor, this->z / factor); } /** - * @brief += operator overload + * @brief += operator overload. Adds the components of two vectors and stores the result * - * This operator adds the x, y, and z components of two vectors and stores the result in the calling vector * {a, b, c} += {d, e, f} => {a + d, b + e, c + f} * * @param other vector to add @@ -124,9 +125,8 @@ template class Vector3D { } /** - * @brief -= operator overload + * @brief -= operator overload. Subtracts the components of two vectors and stores the result * - * This operator subtracts the x, y, and z components of two vectors and stores the result in the calling vector * {a, b, c} -= {d, e, f} => {a - d, b - e, c - f} * * @param other vector to subtract @@ -140,10 +140,8 @@ template class Vector3D { } /** - * @brief *= operator overload + * @brief *= operator overload. Multiplies the components of a vector by a scalar and stores the result. * - * This operator multiplies the x, y, and z components of a vector by a scalar and stores the result in the - * calling vector * a *= {b, c, d} => {a * b, a * c, a * d} * * @param factor scalar to multiply by @@ -157,10 +155,8 @@ template class Vector3D { } /** - * @brief /= operator overload + * @brief /= operator overload. Divides the components of a vector by a scalar and stores the result * - * This operator divides the x, y, and z components of a vector by a scalar and stores the result in the - * calling vector * {a, b, c} /= d => {a / d, b / d, c / d} * * @param factor scalar to divide by @@ -264,7 +260,7 @@ template class Vector3D { * * @return Vector3D */ - constexpr Vector3D normalize() { return *(this) / magnitude(); } + constexpr Vector3D normalize() { return (*this) / magnitude(); } /** * @brief rotate the vector by an angle @@ -298,9 +294,7 @@ template class Vector3D { * @return Vector3D */ constexpr Vector3D rotatedBy(const Vector3D& angle) const { - const T m = magnitude(); - const Angle t = theta() + angle; - return fromPolar(t, m); + return fromPolar(theta() + angle, magnitude()); } /** @@ -309,12 +303,27 @@ template class Vector3D { * @param angle * @return Vector3D */ - constexpr Vector3D rotatedTo(Angle angle) const { - const T m = magnitude(); - return fromPolar(angle, m); - } + constexpr Vector3D rotatedTo(Angle angle) const { return fromPolar(angle, magnitude()); } }; +/** + * @brief * operator overload. Multiplies a scalar and a vector + * + * a * {b, c, d} = {a * b, a * c, a * d} + * + * @tparam Q1 the quantity type of the scalar + * @tparam Q2 the quantity type of the vector + * @tparam Q3 the type of quantity to use for the result + * + * @param lhs the scalar on the left hand side + * @param rhs the vector on the right hand side + * @return Q3 the product + */ +template > +constexpr Vector3D operator*(Q1 lhs, const Vector3D& rhs) { + return rhs * lhs; +} + // define some common vector types typedef Vector3D V3Position; typedef Vector3D V3Velocity;