浏览代码

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!
Shannon Booth 5 年之前
父节点
当前提交
123b5c9d34
共有 2 个文件被更改,包括 19 次插入0 次删除
  1. 18 0
      Libraries/LibDraw/Painter.cpp
  2. 1 0
      Libraries/LibDraw/Painter.h

+ 18 - 0
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());

+ 1 - 0
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);