From adbd81d3937a1363af312202c7e4b5ec0bf3458d Mon Sep 17 00:00:00 2001 From: max-001 Date: Thu, 21 Nov 2024 17:04:37 +0100 Subject: Made Vector2 templated --- src/crepe/api/Vector2.cpp | 33 --------------------------------- src/crepe/api/Vector2.h | 7 +++++-- src/crepe/api/Vector2.hpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 35 deletions(-) delete mode 100644 src/crepe/api/Vector2.cpp create mode 100644 src/crepe/api/Vector2.hpp (limited to 'src/crepe/api') diff --git a/src/crepe/api/Vector2.cpp b/src/crepe/api/Vector2.cpp deleted file mode 100644 index 30b968e..0000000 --- a/src/crepe/api/Vector2.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#include "Vector2.h" - -using namespace crepe; - -Vector2 Vector2::operator-(const Vector2 & other) const { return {x - other.x, y - other.y}; } - -Vector2 Vector2::operator+(const Vector2 & other) const { return {x + other.x, y + other.y}; } - -Vector2 Vector2::operator*(double scalar) const { return {x * scalar, y * scalar}; } - -Vector2 & Vector2::operator*=(const Vector2 & other) { - x *= other.x; - y *= other.y; - return *this; -} - -Vector2 & Vector2::operator+=(const Vector2 & other) { - x += other.x; - y += other.y; - return *this; -} - -Vector2 & Vector2::operator+=(double other) { - x += other; - y += other; - return *this; -} - -Vector2 Vector2::operator-() const { return {-x, -y}; } - -bool Vector2::operator==(const Vector2 & other) const { return x == other.x && y == other.y; } - -bool Vector2::operator!=(const Vector2 & other) const { return !(*this == other); } diff --git a/src/crepe/api/Vector2.h b/src/crepe/api/Vector2.h index 2fb6136..0e41936 100644 --- a/src/crepe/api/Vector2.h +++ b/src/crepe/api/Vector2.h @@ -3,11 +3,12 @@ namespace crepe { //! 2D vector +template struct Vector2 { //! X component of the vector - double x = 0; + T x = 0; //! Y component of the vector - double y = 0; + T y = 0; //! Subtracts another vector from this vector and returns the result. Vector2 operator-(const Vector2 & other) const; @@ -38,3 +39,5 @@ struct Vector2 { }; } // namespace crepe + +#include "Vector2.hpp" diff --git a/src/crepe/api/Vector2.hpp b/src/crepe/api/Vector2.hpp new file mode 100644 index 0000000..e0ac3d4 --- /dev/null +++ b/src/crepe/api/Vector2.hpp @@ -0,0 +1,44 @@ +#include "Vector2.h" + +namespace crepe { + +template +Vector2 Vector2::operator-(const Vector2 & other) const { return {x - other.x, y - other.y}; } + +template +Vector2 Vector2::operator+(const Vector2 & other) const { return {x + other.x, y + other.y}; } + +template +Vector2 Vector2::operator*(double scalar) const { return {x * scalar, y * scalar}; } + +template +Vector2 & Vector2::operator*=(const Vector2 & other) { + x *= other.x; + y *= other.y; + return *this; +} + +template +Vector2 & Vector2::operator+=(const Vector2 & other) { + x += other.x; + y += other.y; + return *this; +} + +template +Vector2 & Vector2::operator+=(double other) { + x += other; + y += other; + return *this; +} + +template +Vector2 Vector2::operator-() const { return {-x, -y}; } + +template +bool Vector2::operator==(const Vector2 & other) const { return x == other.x && y == other.y; } + +template +bool Vector2::operator!=(const Vector2 & other) const { return !(*this == other); } + +} // namespace crepe -- cgit v1.2.3