summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorSimon Wanner <skyrising@pvpctutorials.de>2022-03-18 01:17:32 +0100
committerAndreas Kling <kling@serenityos.org>2022-03-18 18:51:42 +0100
commit9c97bd0de4973093d267421b9b928cab3cab5643 (patch)
treeaa0943aa6f2b694f56aadc25207735a43d5a6836 /Userland
parenta3c80f05ba5591fd30250f3cf107b5bb13ac1982 (diff)
downloadserenity-9c97bd0de4973093d267421b9b928cab3cab5643.zip
LibGfx: Add AffineTransform::inverse
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibGfx/AffineTransform.cpp15
-rw-r--r--Userland/Libraries/LibGfx/AffineTransform.h2
2 files changed, 17 insertions, 0 deletions
diff --git a/Userland/Libraries/LibGfx/AffineTransform.cpp b/Userland/Libraries/LibGfx/AffineTransform.cpp
index f0c9df2382..2bd8f15f0c 100644
--- a/Userland/Libraries/LibGfx/AffineTransform.cpp
+++ b/Userland/Libraries/LibGfx/AffineTransform.cpp
@@ -125,6 +125,21 @@ AffineTransform& AffineTransform::rotate_radians(float radians)
return *this;
}
+Optional<AffineTransform> AffineTransform::inverse() const
+{
+ auto determinant = a() * d() - b() * c();
+ if (determinant == 0)
+ return {};
+ return AffineTransform {
+ d() / determinant,
+ -b() / determinant,
+ -c() / determinant,
+ a() / determinant,
+ (c() * f() - d() * e()) / determinant,
+ (b() * e() - a() * f()) / determinant,
+ };
+}
+
void AffineTransform::map(float unmapped_x, float unmapped_y, float& mapped_x, float& mapped_y) const
{
mapped_x = a() * unmapped_x + b() * unmapped_y + m_values[4];
diff --git a/Userland/Libraries/LibGfx/AffineTransform.h b/Userland/Libraries/LibGfx/AffineTransform.h
index 1ec3ace816..c2fc79820e 100644
--- a/Userland/Libraries/LibGfx/AffineTransform.h
+++ b/Userland/Libraries/LibGfx/AffineTransform.h
@@ -62,6 +62,8 @@ public:
AffineTransform& rotate_radians(float);
AffineTransform& multiply(const AffineTransform&);
+ Optional<AffineTransform> inverse() const;
+
private:
float m_values[6] { 0 };
};