diff options
author | Matthew Olsson <matthewcolsson@gmail.com> | 2020-07-22 14:50:54 -0700 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-07-26 14:53:43 +0200 |
commit | 9cce7f57ddf99050101e46d4294ea89ed310cb4e (patch) | |
tree | 07e299cac4c489565242e8a796bcbf84a84f0c2d /Libraries/LibGfx | |
parent | 5985eac81df10e3d3ce65510baa7e06e519a341c (diff) | |
download | serenity-9cce7f57ddf99050101e46d4294ea89ed310cb4e.zip |
LibGfx: Add FloatPoint methods
Adds some conversion constructors, as well as the missing arithmetic
operations.
Diffstat (limited to 'Libraries/LibGfx')
-rw-r--r-- | Libraries/LibGfx/FloatPoint.h | 34 | ||||
-rw-r--r-- | Libraries/LibGfx/Painter.cpp | 2 |
2 files changed, 34 insertions, 2 deletions
diff --git a/Libraries/LibGfx/FloatPoint.h b/Libraries/LibGfx/FloatPoint.h index ef50987e0b..59b10a06c0 100644 --- a/Libraries/LibGfx/FloatPoint.h +++ b/Libraries/LibGfx/FloatPoint.h @@ -30,6 +30,7 @@ #include <AK/String.h> #include <LibGfx/Orientation.h> #include <LibGfx/Point.h> +#include <LibM/math.h> namespace Gfx { @@ -38,12 +39,25 @@ class FloatRect; class FloatPoint { public: FloatPoint() { } + + FloatPoint(int x, int y) + : m_x(x) + , m_y(y) + { + } + FloatPoint(float x, float y) : m_x(x) , m_y(y) { } + FloatPoint(double x, double y) + : m_x(x) + , m_y(y) + { + } + explicit FloatPoint(const IntPoint& other) : m_x(other.x()) , m_y(other.y()) @@ -96,6 +110,7 @@ public: FloatPoint operator-() const { return { -m_x, -m_y }; } + FloatPoint operator-(float number) const { return { m_x - number, m_y - number }; } FloatPoint operator-(const FloatPoint& other) const { return { m_x - other.m_x, m_y - other.m_y }; } FloatPoint& operator-=(const FloatPoint& other) { @@ -104,14 +119,24 @@ public: return *this; } + FloatPoint operator+(float number) const { return { m_x + number, m_y + number }; } + FloatPoint operator+(const FloatPoint& other) const { return { m_x + other.m_x, m_y + other.m_y }; } FloatPoint& operator+=(const FloatPoint& other) { m_x += other.m_x; m_y += other.m_y; return *this; } - FloatPoint operator+(const FloatPoint& other) const { return { m_x + other.m_x, m_y + other.m_y }; } + FloatPoint operator*(float factor) const { return { m_x * factor, m_y * factor }; } + FloatPoint& operator*=(float factor) + { + m_x *= factor; + m_y *= factor; + return *this; + } + + FloatPoint operator/(float factor) const { return { m_x / factor, m_y / factor }; } FloatPoint& operator/=(float factor) { m_x /= factor; @@ -119,6 +144,13 @@ public: return *this; } + float distance_from(const FloatPoint& other) const + { + if (*this == other) + return 0; + return sqrtf(powf(m_x - other.m_x, 2.0f) + powf(m_y - other.m_y, 2.0f)); + } + String to_string() const { return String::format("[%g,%g]", x(), y()); } bool is_null() const { return !m_x && !m_y; } diff --git a/Libraries/LibGfx/Painter.cpp b/Libraries/LibGfx/Painter.cpp index c80356ad33..770f5f03de 100644 --- a/Libraries/LibGfx/Painter.cpp +++ b/Libraries/LibGfx/Painter.cpp @@ -1565,7 +1565,7 @@ void Painter::fill_path(Path& path, Color color, WindingRule winding_rule) #ifdef FILL_PATH_DEBUG size_t i { 0 }; for (auto& segment : segments) - draw_line(IntPoint(segment.from.x(), segment.from.y()), IntPoint(segment.to.x(), segment.to.y()), Color::from_hsv(++i / segments.size() * 255, 255, 255), 1); + draw_line(segment.from, segment.to, Color::from_hsv(++i / segments.size() * 255, 255, 255), 1); #endif } |