From 8620f57f7101b2090add89330e74854b77161d3f Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Mon, 6 Jan 2025 13:18:40 +0100 Subject: place zapper method --- src/crepe/api/Vector2.h | 39 +++++++++++++++++++++------------------ src/crepe/api/Vector2.hpp | 10 ++++++++++ src/test/Vector2Test.cpp | 10 ++++++++++ 3 files changed, 41 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/crepe/api/Vector2.h b/src/crepe/api/Vector2.h index 52e1bb6..0f46964 100644 --- a/src/crepe/api/Vector2.h +++ b/src/crepe/api/Vector2.h @@ -11,55 +11,55 @@ struct Vector2 { T y = 0; //! Subtracts another vector from this vector and returns the result. - Vector2 operator-(const Vector2 & other) const; + Vector2 operator-(const Vector2 & other) const; //! Subtracts a scalar value from both components of this vector and returns the result. - Vector2 operator-(T scalar) const; + Vector2 operator-(T scalar) const; //! Adds another vector to this vector and returns the result. - Vector2 operator+(const Vector2 & other) const; + Vector2 operator+(const Vector2 & other) const; //! Adds a scalar value to both components of this vector and returns the result. - Vector2 operator+(T scalar) const; + Vector2 operator+(T scalar) const; //! Multiplies this vector by another vector element-wise and returns the result. - Vector2 operator*(const Vector2 & other) const; + Vector2 operator*(const Vector2 & other) const; //! Multiplies this vector by a scalar and returns the result. - Vector2 operator*(T scalar) const; + Vector2 operator*(T scalar) const; //! Divides this vector by another vector element-wise and returns the result. - Vector2 operator/(const Vector2 & other) const; + Vector2 operator/(const Vector2 & other) const; //! Divides this vector by a scalar and returns the result. - Vector2 operator/(T scalar) const; + Vector2 operator/(T scalar) const; //! Adds another vector to this vector and updates this vector. - Vector2 & operator+=(const Vector2 & other); + Vector2 & operator+=(const Vector2 & other); //! Adds a scalar value to both components of this vector and updates this vector. - Vector2 & operator+=(T other); + Vector2 & operator+=(T other); //! Subtracts another vector from this vector and updates this vector. - Vector2 & operator-=(const Vector2 & other); + Vector2 & operator-=(const Vector2 & other); //! Subtracts a scalar value from both components of this vector and updates this vector. - Vector2 & operator-=(T other); + Vector2 & operator-=(T other); //! Multiplies this vector by another vector element-wise and updates this vector. - Vector2 & operator*=(const Vector2 & other); + Vector2 & operator*=(const Vector2 & other); //! Multiplies this vector by a scalar and updates this vector. - Vector2 & operator*=(T other); + Vector2 & operator*=(T other); //! Divides this vector by another vector element-wise and updates this vector. - Vector2 & operator/=(const Vector2 & other); + Vector2 & operator/=(const Vector2 & other); //! Divides this vector by a scalar and updates this vector. - Vector2 & operator/=(T other); + Vector2 & operator/=(T other); //! Returns the negation of this vector. - Vector2 operator-() const; + Vector2 operator-() const; //! Checks if this vector is equal to another vector. bool operator==(const Vector2 & other) const; @@ -89,10 +89,13 @@ struct Vector2 { T distance_squared(const Vector2 & other) const; //! Returns the perpendicular vector to this vector. - Vector2 perpendicular() const; + Vector2 perpendicular() const; //! Checks if both components of the vector are NaN. bool is_nan() const; + + //! Rotate this vector clockwise by \c deg degrees + Vector2 rotate(float deg) const; }; } // namespace crepe diff --git a/src/crepe/api/Vector2.hpp b/src/crepe/api/Vector2.hpp index e195760..a4ce641 100644 --- a/src/crepe/api/Vector2.hpp +++ b/src/crepe/api/Vector2.hpp @@ -168,4 +168,14 @@ bool Vector2::is_nan() const { return std::isnan(x) && std::isnan(y); } +template +Vector2 Vector2::rotate(float deg) const { + float rad = -deg / 180 * M_PI; + return { + x * std::cos(rad) - y * std::sin(rad), + x * std::sin(rad) + y * std::cos(rad), + }; +} + + } // namespace crepe diff --git a/src/test/Vector2Test.cpp b/src/test/Vector2Test.cpp index 1e21af9..b17f95a 100644 --- a/src/test/Vector2Test.cpp +++ b/src/test/Vector2Test.cpp @@ -1,6 +1,7 @@ #include #include +#include using namespace crepe; @@ -530,3 +531,12 @@ TEST_F(Vector2Test, Perpendicular) { EXPECT_FLOAT_EQ(result4.x, -4.0f); EXPECT_FLOAT_EQ(result4.y, 3.0f); } + +TEST_F(Vector2Test, Rotate) { + vec2 foo {0, 1}; + + foo = foo.rotate(90); + const float GOOD_ENOUGH = 0.001; + EXPECT_NEAR(foo.x, 1, GOOD_ENOUGH); + EXPECT_NEAR(foo.y, 0, GOOD_ENOUGH); +} -- cgit v1.2.3