From dc22e59a1a9c59a2add31af993fb53ac5fa84088 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 26 Jun 2020 18:23:38 +0200 Subject: [PATCH] LibGfx: Teach AffineTransform how to rotate and multiply --- Libraries/LibGfx/AffineTransform.cpp | 22 ++++++++++++++++++++++ Libraries/LibGfx/AffineTransform.h | 7 +++++++ 2 files changed, 29 insertions(+) diff --git a/Libraries/LibGfx/AffineTransform.cpp b/Libraries/LibGfx/AffineTransform.cpp index 02dad6c24e..77738c25ed 100644 --- a/Libraries/LibGfx/AffineTransform.cpp +++ b/Libraries/LibGfx/AffineTransform.cpp @@ -69,6 +69,28 @@ AffineTransform& AffineTransform::translate(float tx, float ty) return *this; } +AffineTransform& AffineTransform::multiply(const AffineTransform& other) +{ + AffineTransform result; + result.m_values[0] = other.a() * a() + other.b() * c(); + result.m_values[1] = other.a() * b() + other.b() * d(); + result.m_values[2] = other.c() * a() + other.d() * c(); + result.m_values[3] = other.c() * b() + other.d() * d(); + result.m_values[4] = other.e() * a() + other.f() * c() + e(); + result.m_values[5] = other.e() * b() + other.f() * d() + f(); + *this = result; + return *this; +} + +AffineTransform& AffineTransform::rotate_radians(float radians) +{ + float sin_angle = sinf(radians); + float cos_angle = cosf(radians); + AffineTransform rotation(cos_angle, sin_angle, -sin_angle, cos_angle, 0, 0); + multiply(rotation); + return *this; +} + void AffineTransform::map(float unmapped_x, float unmapped_y, float& mapped_x, float& mapped_y) const { mapped_x = (m_values[0] * unmapped_x + m_values[2] * unmapped_y + m_values[4]); diff --git a/Libraries/LibGfx/AffineTransform.h b/Libraries/LibGfx/AffineTransform.h index 9e6aa35113..0a3613171d 100644 --- a/Libraries/LibGfx/AffineTransform.h +++ b/Libraries/LibGfx/AffineTransform.h @@ -39,6 +39,11 @@ public: { } + AffineTransform(float a, float b, float c, float d, float e, float f) + : m_values { a, b, c, d, e, f } + { + } + bool is_identity() const; void map(float unmapped_x, float unmapped_y, float& mapped_x, float& mapped_y) const; @@ -64,6 +69,8 @@ public: AffineTransform& scale(float sx, float sy); AffineTransform& translate(float tx, float ty); + AffineTransform& rotate_radians(float); + AffineTransform& multiply(const AffineTransform&); private: float m_values[6] { 0 };