From 8520d3a4252e2b366598210ebc322ff3561d5381 Mon Sep 17 00:00:00 2001 From: Sergey Bugaev Date: Sun, 24 May 2020 21:03:48 +0300 Subject: [PATCH] LibGfx: Add Painter::fill_ellipse() --- Libraries/LibGfx/Painter.cpp | 19 +++++++++++++++++++ Libraries/LibGfx/Painter.h | 1 + 2 files changed, 20 insertions(+) diff --git a/Libraries/LibGfx/Painter.cpp b/Libraries/LibGfx/Painter.cpp index da6728fe06..543458046a 100644 --- a/Libraries/LibGfx/Painter.cpp +++ b/Libraries/LibGfx/Painter.cpp @@ -232,6 +232,25 @@ void Painter::fill_rect_with_gradient(const Rect& a_rect, Color gradient_start, return fill_rect_with_gradient(Orientation::Horizontal, a_rect, gradient_start, gradient_end); } +void Painter::fill_ellipse(const Rect& a_rect, Color color) +{ + auto rect = a_rect.translated(translation()).intersected(clip_rect()); + if (rect.is_empty()) + return; + + ASSERT(m_target->rect().contains(rect)); + + RGBA32* dst = m_target->scanline(rect.top()) + rect.left() + rect.width() / 2; + const size_t dst_skip = m_target->pitch() / sizeof(RGBA32); + + for (int i = 0; i < rect.height(); i++) { + double y = rect.height() * 0.5 - i; + double x = rect.width() * sqrt(0.25 - y * y / rect.height() / rect.height()); + fast_u32_fill(dst - (int)x, color.value(), 2 * (int)x); + dst += dst_skip; + } +} + void Painter::draw_ellipse_intersecting(const Rect& rect, Color color, int thickness) { constexpr int number_samples = 100; // FIXME: dynamically work out the number of samples based upon the rect size diff --git a/Libraries/LibGfx/Painter.h b/Libraries/LibGfx/Painter.h index 4f695c50d1..4b06c78a90 100644 --- a/Libraries/LibGfx/Painter.h +++ b/Libraries/LibGfx/Painter.h @@ -56,6 +56,7 @@ public: void fill_rect_with_checkerboard(const Rect&, const Size&, Color color_dark, Color color_light); void fill_rect_with_gradient(Orientation, const Rect&, Color gradient_start, Color gradient_end); void fill_rect_with_gradient(const Rect&, Color gradient_start, Color gradient_end); + void fill_ellipse(const Rect&, Color); void draw_rect(const Rect&, Color, bool rough = false); void draw_bitmap(const Point&, const CharacterBitmap&, Color = Color()); void draw_bitmap(const Point&, const GlyphBitmap&, Color = Color());