diff --git a/include/units/Vector2D.hpp b/include/units/Vector2D.hpp index e40eba3..0c51d97 100644 --- a/include/units/Vector2D.hpp +++ b/include/units/Vector2D.hpp @@ -66,7 +66,7 @@ template class Vector2D { * @return Vector2D */ constexpr Vector2D operator+(const Vector2D& other) const { - return Vector2D(x + other.x, y + other.y); + return Vector2D(this->x + other.x, this->y + other.y); } /** @@ -79,7 +79,7 @@ template class Vector2D { * @return Vector2D */ constexpr Vector2D operator-(const Vector2D& other) const { - return Vector2D(x - other.x, y - other.y); + return Vector2D(this->x - other.x, this->y - other.y); } /** @@ -91,7 +91,7 @@ template class Vector2D { * @param factor scalar to multiply by * @return Vector2D */ - constexpr Vector2D operator*(double factor) const { return Vector2D(x * factor, y * factor); } + constexpr Vector2D operator*(double factor) const { return Vector2D(this->x * factor, this->y * factor); } /** * @brief / operator overload @@ -102,7 +102,7 @@ template class Vector2D { * @param factor scalar to divide by * @return Vector2D */ - constexpr Vector2D operator/(double factor) const { return Vector2D(x / factor, y / factor); } + constexpr Vector2D operator/(double factor) const { return Vector2D(this->x / factor, this->y / factor); } /** * @brief += operator overload @@ -114,8 +114,8 @@ template class Vector2D { * @return Vector2D& */ constexpr Vector2D& operator+=(const Vector2D& other) { - x += other.x; - y += other.y; + this->x += other.x; + this->y += other.y; return (*this); } @@ -129,8 +129,8 @@ template class Vector2D { * @return Vector2D& */ constexpr Vector2D& operator-=(const Vector2D& other) { - x -= other.x; - y -= other.y; + this->x -= other.x; + this->y -= other.y; return (*this); } @@ -145,8 +145,8 @@ template class Vector2D { * @return Vector2D& */ constexpr Vector2D& operator*=(double factor) { - x *= factor; - y *= factor; + this->x *= factor; + this->y *= factor; return (*this); } @@ -161,8 +161,8 @@ template class Vector2D { * @return Vector2D& */ constexpr Vector2D& operator/=(double factor) { - x /= factor; - y /= factor; + this->x /= factor; + this->y /= factor; return (*this); } @@ -178,7 +178,7 @@ template class Vector2D { * @return R the dot product */ template > constexpr R dot(const Vector2D& other) const { - return (x * other.x) + (y * other.y); + return (this->x * other.x) + (this->y * other.y); } /** @@ -186,14 +186,14 @@ template class Vector2D { * * @return Angle */ - constexpr Angle theta() const { return atan2(y, x); } + constexpr Angle theta() const { return atan2(this->y, this->x); } /** * @brief magnitude of the vector * * @return T */ - constexpr T magnitude() const { return sqrt(square(x) + square(y)); } + constexpr T magnitude() const { return sqrt(square(this->x) + square(this->y)); } /** * @brief difference between two vectors @@ -204,7 +204,9 @@ template class Vector2D { * @param other the other vector * @return Vector2D */ - constexpr 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 - this->x, other.y - this->y); + } /** * @brief the angle between two vectors @@ -212,7 +214,7 @@ template class Vector2D { * @param other the other vector * @return Angle */ - constexpr 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 - this->y, other.x - this->x); } /** * @brief get the distance between two vectors @@ -221,7 +223,7 @@ template class Vector2D { * @return T */ constexpr T distanceTo(const Vector2D& other) const { - return sqrt(square(x - other.x, 2) + square(y - other.y, 2)); + return sqrt(square(this->x - other.x, 2) + square(this->y - other.y, 2)); } /** @@ -232,8 +234,8 @@ template class Vector2D { * @return Vector2D */ constexpr Vector2D normalize() const { - T m = magnitude(); - return Vector2D(x / m, y / m); + const T m = magnitude(); + return Vector2D(this->x / m, this->y / m); } /** @@ -242,10 +244,10 @@ template class Vector2D { * @param angle */ constexpr void rotateBy(Angle angle) { - T m = magnitude(); - Angle t = theta() + angle; - x = m * cos(t); - y = m * sin(t); + const T m = magnitude(); + const Angle t = theta() + angle; + this->x = m * cos(t); + this->y = m * sin(t); } /** @@ -254,9 +256,9 @@ template class Vector2D { * @param angle */ constexpr void rotateTo(Angle angle) { - T m = magnitude(); - x = m * cos(angle); - y = m * sin(angle); + const T m = magnitude(); + this->x = m * cos(angle); + this->y = m * sin(angle); } /** @@ -266,8 +268,8 @@ template class Vector2D { * @return Vector2D */ constexpr Vector2D rotatedBy(Angle angle) const { - T m = magnitude(); - Angle t = theta() + angle; + const T m = magnitude(); + const Angle t = theta() + angle; return fromPolar(t, m); } @@ -278,7 +280,7 @@ template class Vector2D { * @return Vector2D */ constexpr Vector2D rotatedTo(Angle angle) const { - T m = magnitude(); + const T m = magnitude(); return fromPolar(angle, m); } }; diff --git a/include/units/Vector3D.hpp b/include/units/Vector3D.hpp index 0e3f47d..4b3a4dd 100644 --- a/include/units/Vector3D.hpp +++ b/include/units/Vector3D.hpp @@ -65,7 +65,7 @@ template class Vector3D { * @return Vector3D */ constexpr Vector3D operator+(const Vector3D& other) const { - return Vector3D(x + other.x, y + other.y, z + other.z); + return Vector3D(this->x + other.x, this->y + other.y, this->z + other.z); } /** @@ -78,7 +78,7 @@ template class Vector3D { * @return Vector3D */ constexpr Vector3D operator-(const Vector3D& other) const { - return Vector3D(x - other.x, y - other.y, z - other.z); + return Vector3D(this->x - other.x, this->y - other.y, this->z - other.z); } /** @@ -90,7 +90,9 @@ template class Vector3D { * @param factor scalar to multiply by * @return Vector3D */ - constexpr Vector3D operator*(double factor) const { return Vector3D(x * factor, y * factor, z * factor); } + constexpr Vector3D operator*(double factor) const { + return Vector3D(this->x * factor, this->y * factor, this->z * factor); + } /** * @brief / operator overload @@ -101,7 +103,9 @@ template class Vector3D { * @param factor scalar to divide by * @return Vector3D */ - constexpr Vector3D operator/(double factor) const { return Vector3D(x / factor, y / factor, z / factor); } + constexpr Vector3D operator/(double factor) const { + return Vector3D(this->x / factor, this->y / factor, this->z / factor); + } /** * @brief += operator overload @@ -113,9 +117,9 @@ template class Vector3D { * @return Vector3D& */ constexpr Vector3D& operator+=(const Vector3D& other) { - x += other.x; - y += other.y; - z += other.z; + this->x += other.x; + this->y += other.y; + this->z += other.z; return (*this); } @@ -129,9 +133,9 @@ template class Vector3D { * @return Vector3D& */ constexpr Vector3D& operator-=(const Vector3D& other) { - x -= other.x; - y -= other.y; - z -= other.z; + this->x -= other.x; + this->y -= other.y; + this->z -= other.z; return (*this); } @@ -146,9 +150,9 @@ template class Vector3D { * @return Vector3D& */ constexpr Vector3D& operator*=(double factor) { - x *= factor; - y *= factor; - z *= factor; + this->x *= factor; + this->y *= factor; + this->z *= factor; return (*this); } @@ -163,9 +167,9 @@ template class Vector3D { * @return Vector3D& */ constexpr Vector3D& operator/=(double factor) { - x /= factor; - y /= factor; - z /= factor; + this->x /= factor; + this->y /= factor; + this->z /= factor; return (*this); } @@ -181,7 +185,7 @@ template class Vector3D { * @return R the dot product */ template > constexpr R dot(const Vector3D& other) const { - return (x * other.x) + (y * other.y) + (z * other.z); + return (this->x * other.x) + (this->y * other.y) + (this->z * other.z); } /** @@ -197,8 +201,10 @@ template class Vector3D { * @param other the vector to calculate the cross product with * @return Vector3D the cross product */ - 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); + template > + constexpr Vector3D cross(const Vector3D& other) const { + return Vector3D(this->y * other.z - this->z * other.y, this->z * other.x - this->x * other.z, + this->x * other.y - this->y * other.x); } /** @@ -208,7 +214,7 @@ template class Vector3D { */ constexpr Vector3D theta() const { const T mag = magnitude(); - return Vector3D(acos(x / mag), acos(y / mag), acos(z / mag)); + return Vector3D(acos(this->x / mag), acos(this->y / mag), acos(this->z / mag)); } /** @@ -216,7 +222,7 @@ template class Vector3D { * * @return T */ - constexpr T magnitude() const { return sqrt(square(x) + square(y) + square(z)); } + constexpr T magnitude() const { return sqrt(square(this->x) + square(this->y) + square(this->z)); } /** * @brief difference between two vectors @@ -230,7 +236,7 @@ template class Vector3D { * @return Vector3D */ constexpr Vector3D vectorTo(const Vector3D& other) const { - return Vector2D(other.x - x, other.y - y, other.z - z); + return Vector2D(other.x - this->x, other.y - this->y, other.z - this->z); } /** @@ -240,7 +246,7 @@ template class Vector3D { * @return Angle */ constexpr Angle angleTo(const Vector3D& other) const { - return units::acos(dot(other) / (magnitude() * other.magnitude())); + return acos(dot(other) / (magnitude() * other.magnitude())); } /** @@ -258,10 +264,7 @@ template class Vector3D { * * @return Vector3D */ - constexpr Vector3D normalize() { - T m = magnitude(); - return Vector2D(x / m, y / m, z / m); - } + constexpr Vector3D normalize() { return *(this) / magnitude(); } /** * @brief rotate the vector by an angle @@ -271,9 +274,9 @@ template class Vector3D { constexpr void rotateBy(const Vector3D& angle) { const T m = magnitude(); const Vector3D t = theta() + angle; - x = m * cos(t.x); - y = m * cos(t.y); - z = m * cos(t.z); + this->x = m * cos(t.x); + this->y = m * cos(t.y); + this->z = m * cos(t.z); } /** @@ -283,9 +286,9 @@ template class Vector3D { */ constexpr void rotateTo(const Vector3D& angle) { const T m = magnitude(); - x = m * cos(angle.x); - y = m * cos(angle.y); - z = m * cos(angle.z); + this->x = m * cos(angle.x); + this->y = m * cos(angle.y); + this->z = m * cos(angle.z); } /** @@ -295,8 +298,8 @@ template class Vector3D { * @return Vector3D */ constexpr Vector3D rotatedBy(const Vector3D& angle) const { - T m = magnitude(); - Angle t = theta() + angle; + const T m = magnitude(); + const Angle t = theta() + angle; return fromPolar(t, m); } @@ -307,7 +310,7 @@ template class Vector3D { * @return Vector3D */ constexpr Vector3D rotatedTo(Angle angle) const { - T m = magnitude(); + const T m = magnitude(); return fromPolar(angle, m); } };