LibGfx: Add AffineTransform::inverse

This commit is contained in:
Simon Wanner 2022-03-18 01:17:32 +01:00 committed by Andreas Kling
parent a3c80f05ba
commit 9c97bd0de4
Notes: sideshowbarker 2024-07-17 17:10:54 +09:00
2 changed files with 17 additions and 0 deletions

View file

@ -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];

View file

@ -62,6 +62,8 @@ public:
AffineTransform& rotate_radians(float);
AffineTransform& multiply(const AffineTransform&);
Optional<AffineTransform> inverse() const;
private:
float m_values[6] { 0 };
};