summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShannon Booth <shannon.ml.booth@gmail.com>2019-12-27 11:19:45 +1300
committerAndreas Kling <awesomekling@gmail.com>2019-12-27 00:52:17 +0100
commit123b5c9d34b8b47cd9912a03404863b4a2f7c4ed (patch)
treeb25a9886598329d7a250f3abcda028d22bed7dd2
parent0b3a8687299fddb65ab6c1966ad82dde8fa26187 (diff)
downloadserenity-123b5c9d34b8b47cd9912a03404863b4a2f7c4ed.zip
LibDraw: Add draw_ellipse_intersecting function
This functon will draw an ellipse which is intersecting the corners of the rect given. It is a very naive implementation, taking 200 samples of points around the ellipse, and drawing straight lines between each of these points. The ellipses look good enough to me though!
-rw-r--r--Libraries/LibDraw/Painter.cpp18
-rw-r--r--Libraries/LibDraw/Painter.h1
2 files changed, 19 insertions, 0 deletions
diff --git a/Libraries/LibDraw/Painter.cpp b/Libraries/LibDraw/Painter.cpp
index fd46e51ab4..5e966fde83 100644
--- a/Libraries/LibDraw/Painter.cpp
+++ b/Libraries/LibDraw/Painter.cpp
@@ -140,6 +140,24 @@ void Painter::fill_rect_with_gradient(const Rect& a_rect, Color gradient_start,
}
}
+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
+ double increment = M_PI / number_samples;
+
+ auto ellipse_x = [&](double theta) -> int {
+ return (cos(theta) * rect.width() / sqrt(2)) + rect.center().x();
+ };
+
+ auto ellipse_y = [&](double theta) -> int {
+ return (sin(theta) * rect.height() / sqrt(2)) + rect.center().y();
+ };
+
+ for (float theta = 0; theta < 2 * M_PI; theta += increment) {
+ draw_line({ ellipse_x(theta), ellipse_y(theta) }, { ellipse_x(theta + increment), ellipse_y(theta + increment) }, color, thickness);
+ }
+}
+
void Painter::draw_rect(const Rect& a_rect, Color color, bool rough)
{
Rect rect = a_rect.translated(translation());
diff --git a/Libraries/LibDraw/Painter.h b/Libraries/LibDraw/Painter.h
index 575f69373f..ce88595c2d 100644
--- a/Libraries/LibDraw/Painter.h
+++ b/Libraries/LibDraw/Painter.h
@@ -25,6 +25,7 @@ public:
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());
+ void draw_ellipse_intersecting(const Rect&, Color, int thickness = 1);
void set_pixel(const Point&, Color);
void draw_line(const Point&, const Point&, Color, int thickness = 1, bool dotted = false);
void draw_scaled_bitmap(const Rect& dst_rect, const GraphicsBitmap&, const Rect& src_rect);