From e57bd4099125aea27d179e2e006b99676c8f87f1 Mon Sep 17 00:00:00 2001 From: Eero Holmala Date: Fri, 28 Apr 2023 13:38:39 +0300 Subject: [PATCH] App: Add Vector2 and Vector3 implementation --- App/.vscode/settings.json | 5 +- App/AppLib/include/AppLib/Math.hh | 26 +++--- App/AppLib/src/Math.cpp | 136 ++++++++++++++++++++++++++++++ 3 files changed, 155 insertions(+), 12 deletions(-) diff --git a/App/.vscode/settings.json b/App/.vscode/settings.json index c085a8b..cbab875 100644 --- a/App/.vscode/settings.json +++ b/App/.vscode/settings.json @@ -42,7 +42,10 @@ "xstddef": "cpp", "xstring": "cpp", "xtr1common": "cpp", - "xutility": "cpp" + "xutility": "cpp", + "list": "cpp", + "vector": "cpp", + "xtree": "cpp" }, "cmake.configureOnOpen": true } \ No newline at end of file diff --git a/App/AppLib/include/AppLib/Math.hh b/App/AppLib/include/AppLib/Math.hh index 7c2179b..9b5ad7c 100644 --- a/App/AppLib/include/AppLib/Math.hh +++ b/App/AppLib/include/AppLib/Math.hh @@ -8,6 +8,9 @@ namespace Core class Vector2 { public: + float x; + float y; + Vector2(); Vector2(float x, float y); @@ -15,18 +18,15 @@ namespace Core Vector2 operator-(Vector2 const& other); Vector2 operator*(Vector2 const& other); Vector2 operator/(Vector2 const& other); - Vector2 operator+=(Vector2 const& other); - Vector2 operator-=(Vector2 const& other); - Vector2 operator*=(Vector2 const& other); - Vector2 operator/=(Vector2 const& other); + Vector2& operator+=(Vector2 const& other); + Vector2& operator-=(Vector2 const& other); + Vector2& operator*=(Vector2 const& other); + Vector2& operator/=(Vector2 const& other); bool operator==(Vector2 const& other); bool operator!=(Vector2 const& other); Vector2 operator*(float const& scalar); - - float x; - float y; }; class Vector3 @@ -37,14 +37,18 @@ namespace Core float z; float w; + Vector3(); + Vector3(float x, float y, float z); + Vector3(float x, float y, float z, float w); + Vector3 operator+(Vector3 const& other); Vector3 operator-(Vector3 const& other); Vector3 operator*(Vector3 const& other); Vector3 operator/(Vector3 const& other); - Vector3 operator+=(Vector3 const& other); - Vector3 operator-=(Vector3 const& other); - Vector3 operator*=(Vector3 const& other); - Vector3 operator/=(Vector3 const& other); + Vector3& operator+=(Vector3 const& other); + Vector3& operator-=(Vector3 const& other); + Vector3& operator*=(Vector3 const& other); + Vector3& operator/=(Vector3 const& other); bool operator==(Vector3 const& other); bool operator!=(Vector3 const& other); diff --git a/App/AppLib/src/Math.cpp b/App/AppLib/src/Math.cpp index 63280b9..9f6041f 100644 --- a/App/AppLib/src/Math.cpp +++ b/App/AppLib/src/Math.cpp @@ -27,7 +27,143 @@ namespace Core Vector2 Vector2::operator/(Vector2 const &other) { + // TODO(Eero): Add Zero checks return Vector2(this->x/other.x, this->y/other.y); } + Vector2& Vector2::operator+=(Vector2 const &other) + { + this->x += other.x; + this->y += other.y; + return *this; + } + + Vector2& Vector2::operator-=(Vector2 const &other) + { + this->x -= other.x; + this->y -= other.y; + return *this; + } + + Vector2& Vector2::operator*=(Vector2 const &other) + { + this->x *= other.x; + this->y *= other.y; + return *this; + } + + Vector2& Vector2::operator/=(Vector2 const &other) + { + // TODO(Eero): Add Zero checks + this->x /= other.x; + this->y /= other.y; + return *this; + } + + bool Vector2::operator==(Vector2 const &other) + { + return (this->x == other.x && this->y == other.y) ? true : false; + } + + bool Vector2::operator!=(Vector2 const &other) + { + return (this->x != other.x || this->y != other.y) ? true : false; + } + + Vector2 Vector2::operator*(float const &scalar) + { + return Vector2(scalar * this->x, scalar * this->y); + } + + Vector3::Vector3() : x(0), y(0), z(0), w(1) + { + } + + Vector3::Vector3(float x, float y, float z) : x(x), y(y), z(z), w(1) + { + } + + Vector3::Vector3(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) + { + } + + Vector3 Vector3::operator+(Vector3 const &other) + { + return Vector3(this->x+other.x, this->y+other.y, this->z+other.z); + } + + Vector3 Vector3::operator-(Vector3 const &other) + { + return Vector3(this->x-other.x, this->y-other.y, this->z-other.z); + } + + Vector3 Vector3::operator*(Vector3 const &other) + { + return Vector3(this->x*other.x, this->y*other.y, this->z*other.z); + } + + Vector3 Vector3::operator/(Vector3 const &other) + { + // TODO(Eero): Add Zero checks + return Vector3(this->x/other.x, this->y/other.y, this->z/other.z); + } + + Vector3& Vector3::operator+=(Vector3 const &other) + { + this->x += other.x; + this->y += other.y; + this->z += other.z; + return *this; + } + + Vector3& Vector3::operator-=(Vector3 const &other) + { + this->x -= other.x; + this->y -= other.y; + this->z -= other.z; + return *this; + } + + Vector3& Vector3::operator*=(Vector3 const &other) + { + this->x *= other.x; + this->y *= other.y; + this->z *= other.z; + return *this; + } + + Vector3& Vector3::operator/=(Vector3 const &other) + { + // TODO(Eero): Add Zero checks + this->x /= other.x; + this->y /= other.y; + this->z /= other.z; + return *this; + } + + bool Vector3::operator==(Vector3 const &other) + { + return ( + this->x == other.x + && this->y == other.y + && this->z == other.z + && this->w == other.w // TODO(Eero): Is w check required? + ) ? true : false; + } + + bool Vector3::operator!=(Vector3 const &other) + { + return ( + this->x != other.x + || this->y != other.y + || this->z != other.z + || this->w != other.w // TODO(Eero): Is w check required? + ) ? true : false; + } + + Vector3 Vector3::operator*(float const &scalar) + { + return Vector3(scalar * this->x, scalar * this->y, scalar * this->z); + } + } // namespace Core \ No newline at end of file