From e89269578ae39525f6ec36d22b29eba55d2f09d8 Mon Sep 17 00:00:00 2001 From: FrancisTheCat <90558133+FrancisTheCat@users.noreply.github.com> Date: Sat, 5 Jul 2025 23:18:58 +0200 Subject: [PATCH] math/vector: Added transform method to Vector2D class (#64) --- include/hyprutils/math/Vector2D.hpp | 4 ++++ src/math/Vector2D.cpp | 24 ++++++++++++++++++++++++ tests/math.cpp | 16 +++++++++++++++- 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/include/hyprutils/math/Vector2D.hpp b/include/hyprutils/math/Vector2D.hpp index f59e1e6..ccb874a 100644 --- a/include/hyprutils/math/Vector2D.hpp +++ b/include/hyprutils/math/Vector2D.hpp @@ -1,5 +1,7 @@ #pragma once +#include + #include #include @@ -97,6 +99,8 @@ namespace Hyprutils { Vector2D round() const; Vector2D getComponentMax(const Vector2D& other) const; + + Vector2D transform(eTransform transform, const Vector2D& monitorSize) const; }; } } diff --git a/src/math/Vector2D.cpp b/src/math/Vector2D.cpp index 6f0da5f..6957154 100644 --- a/src/math/Vector2D.cpp +++ b/src/math/Vector2D.cpp @@ -1,4 +1,5 @@ #include +#include #include #include @@ -55,3 +56,26 @@ double Hyprutils::Math::Vector2D::size() const { Vector2D Hyprutils::Math::Vector2D::getComponentMax(const Vector2D& other) const { return Vector2D(std::max(this->x, other.x), std::max(this->y, other.y)); } + +Vector2D Hyprutils::Math::Vector2D::transform(eTransform transform, const Vector2D& monitorSize) const { + switch (transform) { + case HYPRUTILS_TRANSFORM_NORMAL: + return *this; + case HYPRUTILS_TRANSFORM_90: + return Vector2D(y, monitorSize.y - x); + case HYPRUTILS_TRANSFORM_180: + return Vector2D(monitorSize.x - x, monitorSize.y - y); + case HYPRUTILS_TRANSFORM_270: + return Vector2D(monitorSize.x - y, x); + case HYPRUTILS_TRANSFORM_FLIPPED: + return Vector2D(monitorSize.x - x, y); + case HYPRUTILS_TRANSFORM_FLIPPED_90: + return Vector2D(y, x); + case HYPRUTILS_TRANSFORM_FLIPPED_180: + return Vector2D(x, monitorSize.y - y); + case HYPRUTILS_TRANSFORM_FLIPPED_270: + return Vector2D(monitorSize.x - y, monitorSize.y - x); + default: + return *this; + } +} diff --git a/tests/math.cpp b/tests/math.cpp index 316a6fc..8df3af3 100644 --- a/tests/math.cpp +++ b/tests/math.cpp @@ -104,5 +104,19 @@ int main(int argc, char** argv, char** envp) { EXPECT(std::abs(expected.getMatrix().at(8) - matrixBox.getMatrix().at(8)) < 0.1, true); } + { + Vector2D original(30, 40); + Vector2D monitorSize(100, 200); + + EXPECT_VECTOR2D(original.transform(HYPRUTILS_TRANSFORM_NORMAL, monitorSize), Vector2D(30, 40 )); + EXPECT_VECTOR2D(original.transform(HYPRUTILS_TRANSFORM_90, monitorSize), Vector2D(40, 200 - 30)); + EXPECT_VECTOR2D(original.transform(HYPRUTILS_TRANSFORM_180, monitorSize), Vector2D(100 - 30, 200 - 40)); + EXPECT_VECTOR2D(original.transform(HYPRUTILS_TRANSFORM_270, monitorSize), Vector2D(100 - 40, 30 )); + EXPECT_VECTOR2D(original.transform(HYPRUTILS_TRANSFORM_FLIPPED, monitorSize), Vector2D(100 - 30, 40 )); + EXPECT_VECTOR2D(original.transform(HYPRUTILS_TRANSFORM_FLIPPED_90, monitorSize), Vector2D(40, 30 )); + EXPECT_VECTOR2D(original.transform(HYPRUTILS_TRANSFORM_FLIPPED_180, monitorSize), Vector2D(30, 200 - 40)); + EXPECT_VECTOR2D(original.transform(HYPRUTILS_TRANSFORM_FLIPPED_270, monitorSize), Vector2D(100 - 40, 200 - 30)); + } + return ret; -} \ No newline at end of file +}