From d0e9c53ac19382e37cae70b2c4e014330acf6ac1 Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Sat, 6 Jul 2024 09:56:31 -0700 Subject: [PATCH 1/2] feat: :construction: Add initial impl of Vector class Adds an initial implementation of an N-dimensional Vector class --- include/units/Vector.hpp | 186 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 include/units/Vector.hpp diff --git a/include/units/Vector.hpp b/include/units/Vector.hpp new file mode 100644 index 0000000..4b16e2f --- /dev/null +++ b/include/units/Vector.hpp @@ -0,0 +1,186 @@ +#pragma once + +#include "units/Angle.hpp" +#include + +namespace units { +/** + * @class Vector2D + * + * @brief a 2D vector with x and y components of a given quantity type. + * + * @tparam T the type of quantity to use for the vector components + */ +template class Vector { + protected: + std::array vec {}; + public: + Vector() {} + + Vector(T x) + requires(N == 1) + : vec({std::move(x)}) {} + + Vector(T x, T y) + requires(N == 2) + : vec({std::move(x), std::move(y)}) {} + + Vector(T x, T y, T z) + requires(N == 3) + : vec({std::move(x), std::move(y), std::move(z)}) {} + + Vector(std::array _vec): vec(_vec) {} + + /** + * @brief get the x component + * + * @return T x component + */ + T getX() requires(N >= 1) { return vec[0]; } + + /** + * @brief get the y component + * + * @return T y component + */ + T getY() requires(N >= 2) { return vec[1]; } + + /** + * @brief get the z component + * + * @return T z component + */ + T getZ() requires(N >= 3) { return vec[2]; } + + /** + * @brief set the x component + * + * @param nx x component + */ + void setX(T nx) requires(N >= 2) { vec[0] = nx; } + + /** + * @brief set the y component + * + * @param ny y component + */ + void setY(T ny) requires(N >= 2) { vec[1] = ny; } + + /** + * @brief set the z component + * + * @param nz z component + */ + void setZ(T ny) requires(N >= 3) { vec[2] = ny; } + + /** + * @brief += operator overload + * + * 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 + * @return Vector& + */ + Vector& operator+=(Vector& other) { + for (size_t i = 0; i < vec.size(); i++) { + vec[i] += other.vec[i]; + } + return (*this); + } + + /** + * @brief -= operator overload + * + * 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 + * @return Vector& + */ + Vector& operator-=(Vector& other) { + for (size_t i = 0; i < vec.size(); i++) { + vec[i] -= other.vec[i]; + } + return (*this); + } + + /** + * @brief *= operator overload + * + * 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} + * + * @param factor scalar to multiply by + * @return Vector& + */ + Vector& operator*=(double factor) { + for (size_t i = 0; i < vec.size(); i++) { + vec[i] *= factor; + } + return (*this); + } + + /** + * @brief /= operator overload + * + * 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 + * @return Vector& + */ + Vector& operator/=(double factor) { + for (size_t i = 0; i < vec.size(); i++) { + vec[i] /= factor; + } + return (*this); + } + + /** + * @brief + operator overload + * + * This operator adds the x and y components of two vectors + * {a, b} + {c, d} = {a + c, b + d} + * + * @param other vector to add + * @return Vector + */ + Vector operator+(Vector& other) { return (Vector(other) += *this); } + + /** + * @brief - operator overload + * + * This operator subtracts the x and y components of two vectors + * {a, b} - {c, d} = {a - c, b - d} + * + * @param other vector to subtract + * @return Vector + */ + Vector operator-(Vector& other) { return (Vector(other) += *this); } + + /** + * @brief * operator overload + * + * This operator multiplies the x and y components of a vector by a scalar + * a * {b, c} = {a * b, a * c} + * + * @param factor scalar to multiply by + * @return Vector + */ + Vector operator*(double factor) { return (Vector(*this) *= factor); } + + /** + * @brief / operator overload + * + * 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 + * @return Vector + */ + Vector operator/(double factor) { return (Vector(*this) /= factor); } +}; +} // namespace units \ No newline at end of file From 35538e38bfd1dd949b50883fb2f8ecf8254c7329 Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Mon, 19 Aug 2024 22:20:01 +0000 Subject: [PATCH 2/2] aa --- include/units/Vector.hpp | 48 ++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/include/units/Vector.hpp b/include/units/Vector.hpp index 4b16e2f..0fc74e0 100644 --- a/include/units/Vector.hpp +++ b/include/units/Vector.hpp @@ -32,46 +32,33 @@ template class Vector { Vector(std::array _vec): vec(_vec) {} /** - * @brief get the x component + * @brief get a ref to the x component * - * @return T x component + * @return the x component */ - T getX() requires(N >= 1) { return vec[0]; } + T& getX() requires(N >= 1) { return vec[0]; } /** - * @brief get the y component + * @brief get a ref to the y component * - * @return T y component + * @return the y component */ - T getY() requires(N >= 2) { return vec[1]; } + T& getY() requires(N >= 2) { return vec[1]; } /** - * @brief get the z component + * @brief get a ref to the z component * - * @return T z component + * @return the z component */ - T getZ() requires(N >= 3) { return vec[2]; } + T& getZ() requires(N >= 3) { return vec[2]; } /** - * @brief set the x component + * @brief get a ref to any component * - * @param nx x component + * @return The component */ - void setX(T nx) requires(N >= 2) { vec[0] = nx; } - - /** - * @brief set the y component - * - * @param ny y component - */ - void setY(T ny) requires(N >= 2) { vec[1] = ny; } - - /** - * @brief set the z component - * - * @param nz z component - */ - void setZ(T ny) requires(N >= 3) { vec[2] = ny; } + template + T& get() requires(N >= I) { return vec[I]; } /** * @brief += operator overload @@ -182,5 +169,14 @@ template class Vector { * @return Vector */ Vector operator/(double factor) { return (Vector(*this) /= factor); } + + template + Multiplied dot(vector v) { + Multiplied sum{0}; + for(size_t i = 0; i < n; ++i) { + sum += this->vec[i] * v.vec[i]; + } + return sum; + } }; } // namespace units \ No newline at end of file