summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGfx
diff options
context:
space:
mode:
authorMatthew Olsson <matthewcolsson@gmail.com>2021-04-12 11:20:55 -0700
committerAndreas Kling <kling@serenityos.org>2021-05-02 22:48:06 +0200
commitac238b3bd63e47a334c6859ddb6570b89f7be3dc (patch)
tree1b3aa2c5dc873219728f14f5c613887058b099df /Userland/Libraries/LibGfx
parentf5c676fd86a3ced3ede298d09f0b6d7b4ee1624e (diff)
downloadserenity-ac238b3bd63e47a334c6859ddb6570b89f7be3dc.zip
LibGfx: Add some helper methods to AffineTransform
Also makes some basic getters ALWAYS_INLINE and [[nodiscard]], as well as fixing as error with the map method.
Diffstat (limited to 'Userland/Libraries/LibGfx')
-rw-r--r--Userland/Libraries/LibGfx/AffineTransform.cpp67
-rw-r--r--Userland/Libraries/LibGfx/AffineTransform.h35
2 files changed, 90 insertions, 12 deletions
diff --git a/Userland/Libraries/LibGfx/AffineTransform.cpp b/Userland/Libraries/LibGfx/AffineTransform.cpp
index f711df8dff..2b02f1bac5 100644
--- a/Userland/Libraries/LibGfx/AffineTransform.cpp
+++ b/Userland/Libraries/LibGfx/AffineTransform.cpp
@@ -31,6 +31,26 @@ float AffineTransform::y_scale() const
return hypotenuse(m_values[2], m_values[3]);
}
+FloatPoint AffineTransform::scale() const
+{
+ return { x_scale(), y_scale() };
+}
+
+float AffineTransform::x_translation() const
+{
+ return e();
+}
+
+float AffineTransform::y_translation() const
+{
+ return f();
+}
+
+FloatPoint AffineTransform::translation() const
+{
+ return { x_translation(), y_translation() };
+}
+
AffineTransform& AffineTransform::scale(float sx, float sy)
{
m_values[0] *= sx;
@@ -40,6 +60,25 @@ AffineTransform& AffineTransform::scale(float sx, float sy)
return *this;
}
+AffineTransform& AffineTransform::scale(const FloatPoint& s)
+{
+ return scale(s.x(), s.y());
+}
+
+AffineTransform& AffineTransform::set_scale(float sx, float sy)
+{
+ m_values[0] = sx;
+ m_values[1] = 0;
+ m_values[2] = 0;
+ m_values[3] = sy;
+ return *this;
+}
+
+AffineTransform& AffineTransform::set_scale(const FloatPoint& s)
+{
+ return set_scale(s.x(), s.y());
+}
+
AffineTransform& AffineTransform::translate(float tx, float ty)
{
m_values[4] += tx * m_values[0] + ty * m_values[2];
@@ -47,6 +86,23 @@ AffineTransform& AffineTransform::translate(float tx, float ty)
return *this;
}
+AffineTransform& AffineTransform::translate(const FloatPoint& t)
+{
+ return translate(t.x(), t.y());
+}
+
+AffineTransform& AffineTransform::set_translation(float tx, float ty)
+{
+ m_values[4] = tx;
+ m_values[5] = ty;
+ return *this;
+}
+
+AffineTransform& AffineTransform::set_translation(const FloatPoint& t)
+{
+ return set_translation(t.x(), t.y());
+}
+
AffineTransform& AffineTransform::multiply(const AffineTransform& other)
{
AffineTransform result;
@@ -71,8 +127,8 @@ AffineTransform& AffineTransform::rotate_radians(float radians)
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]);
- mapped_y = (m_values[1] * unmapped_x + m_values[3] * unmapped_y + m_values[5]);
+ mapped_x = a() * unmapped_x + b() * unmapped_y + m_values[4];
+ mapped_y = c() * unmapped_x + d() * unmapped_y + m_values[5];
}
template<>
@@ -80,7 +136,7 @@ IntPoint AffineTransform::map(const IntPoint& point) const
{
float mapped_x;
float mapped_y;
- map(point.x(), point.y(), mapped_x, mapped_y);
+ map(static_cast<float>(point.x()), static_cast<float>(point.y()), mapped_x, mapped_y);
return { roundf(mapped_x), roundf(mapped_y) };
}
@@ -96,7 +152,10 @@ FloatPoint AffineTransform::map(const FloatPoint& point) const
template<>
IntSize AffineTransform::map(const IntSize& size) const
{
- return { roundf(size.width() * x_scale()), roundf(size.height() * y_scale()) };
+ return {
+ roundf(static_cast<float>(size.width()) * x_scale()),
+ roundf(static_cast<float>(size.height()) * y_scale()),
+ };
}
template<>
diff --git a/Userland/Libraries/LibGfx/AffineTransform.h b/Userland/Libraries/LibGfx/AffineTransform.h
index 3e2642f40d..63277b1049 100644
--- a/Userland/Libraries/LibGfx/AffineTransform.h
+++ b/Userland/Libraries/LibGfx/AffineTransform.h
@@ -6,6 +6,7 @@
#pragma once
+#include <AK/Format.h>
#include <AK/Forward.h>
#include <LibGfx/Forward.h>
@@ -36,18 +37,28 @@ public:
template<typename T>
Rect<T> map(const Rect<T>&) const;
- float a() const { return m_values[0]; }
- float b() const { return m_values[1]; }
- float c() const { return m_values[2]; }
- float d() const { return m_values[3]; }
- float e() const { return m_values[4]; }
- float f() const { return m_values[5]; }
+ [[nodiscard]] ALWAYS_INLINE float a() const { return m_values[0]; }
+ [[nodiscard]] ALWAYS_INLINE float b() const { return m_values[1]; }
+ [[nodiscard]] ALWAYS_INLINE float c() const { return m_values[2]; }
+ [[nodiscard]] ALWAYS_INLINE float d() const { return m_values[3]; }
+ [[nodiscard]] ALWAYS_INLINE float e() const { return m_values[4]; }
+ [[nodiscard]] ALWAYS_INLINE float f() const { return m_values[5]; }
- float x_scale() const;
- float y_scale() const;
+ [[nodiscard]] float x_scale() const;
+ [[nodiscard]] float y_scale() const;
+ [[nodiscard]] FloatPoint scale() const;
+ [[nodiscard]] float x_translation() const;
+ [[nodiscard]] float y_translation() const;
+ [[nodiscard]] FloatPoint translation() const;
AffineTransform& scale(float sx, float sy);
+ AffineTransform& scale(const FloatPoint& s);
+ AffineTransform& set_scale(float sx, float sy);
+ AffineTransform& set_scale(const FloatPoint& s);
AffineTransform& translate(float tx, float ty);
+ AffineTransform& translate(const FloatPoint& t);
+ AffineTransform& set_translation(float tx, float ty);
+ AffineTransform& set_translation(const FloatPoint& t);
AffineTransform& rotate_radians(float);
AffineTransform& multiply(const AffineTransform&);
@@ -56,3 +67,11 @@ private:
};
}
+
+template<>
+struct AK::Formatter<Gfx::AffineTransform> : Formatter<FormatString> {
+ void format(FormatBuilder& builder, Gfx::AffineTransform value)
+ {
+ return Formatter<FormatString>::format(builder, "[{} {} {} {} {} {}]", value.a(), value.b(), value.c(), value.d(), value.e(), value.f());
+ }
+};