math/vector: Added transform method to Vector2D class (#64)

This commit is contained in:
FrancisTheCat 2025-07-05 23:18:58 +02:00 committed by GitHub
parent 4737241eaf
commit e89269578a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 43 additions and 1 deletions

View file

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <hyprutils/math/Misc.hpp>
#include <format> #include <format>
#include <string> #include <string>
@ -97,6 +99,8 @@ namespace Hyprutils {
Vector2D round() const; Vector2D round() const;
Vector2D getComponentMax(const Vector2D& other) const; Vector2D getComponentMax(const Vector2D& other) const;
Vector2D transform(eTransform transform, const Vector2D& monitorSize) const;
}; };
} }
} }

View file

@ -1,4 +1,5 @@
#include <hyprutils/math/Vector2D.hpp> #include <hyprutils/math/Vector2D.hpp>
#include <hyprutils/math/Misc.hpp>
#include <algorithm> #include <algorithm>
#include <cmath> #include <cmath>
@ -55,3 +56,26 @@ double Hyprutils::Math::Vector2D::size() const {
Vector2D Hyprutils::Math::Vector2D::getComponentMax(const Vector2D& other) const { Vector2D Hyprutils::Math::Vector2D::getComponentMax(const Vector2D& other) const {
return Vector2D(std::max(this->x, other.x), std::max(this->y, other.y)); 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;
}
}

View file

@ -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); 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; return ret;
} }