Преглед изворни кода

LibGfx: Add Painter::draw_triangle_wave()

This patch adds support for drawing triangular waves.
For now those can only be horizontal, but as they are intended for
underlining text, it's an okay way to handle this.
Tobias Christiansen пре 3 година
родитељ
комит
0277118cb4
2 измењених фајлова са 21 додато и 0 уклоњено
  1. 20 0
      Userland/Libraries/LibGfx/Painter.cpp
  2. 1 0
      Userland/Libraries/LibGfx/Painter.h

+ 20 - 0
Userland/Libraries/LibGfx/Painter.cpp

@@ -3,6 +3,7 @@
  * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  * Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org>
  * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
+ * Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org>
  *
  * SPDX-License-Identifier: BSD-2-Clause
  */
@@ -1821,6 +1822,25 @@ void Painter::draw_line(IntPoint const& a_p1, IntPoint const& a_p2, Color color,
     }
 }
 
+void Painter::draw_triangle_wave(IntPoint const& a_p1, IntPoint const& a_p2, Color color, int amplitude, int thickness)
+{
+    // FIXME: Support more than horizontal waves
+    VERIFY(a_p1.y() == a_p2.y());
+
+    auto const p1 = thickness > 1 ? a_p1.translated(-(thickness / 2), -(thickness / 2)) : a_p1;
+    auto const p2 = thickness > 1 ? a_p2.translated(-(thickness / 2), -(thickness / 2)) : a_p2;
+
+    auto point1 = to_physical(p1);
+    auto point2 = to_physical(p2);
+
+    auto y = point1.y();
+
+    for (int x = 0; x <= point2.x() - point1.x(); ++x) {
+        auto y_offset = abs(x % (2 * amplitude) - amplitude) - amplitude;
+        draw_physical_pixel({ point1.x() + x, y + y_offset }, color, thickness);
+    }
+}
+
 static bool can_approximate_bezier_curve(FloatPoint const& p1, FloatPoint const& p2, FloatPoint const& control)
 {
     constexpr static int tolerance = 15;

+ 1 - 0
Userland/Libraries/LibGfx/Painter.h

@@ -59,6 +59,7 @@ public:
     void set_pixel(IntPoint const&, Color);
     void set_pixel(int x, int y, Color color) { set_pixel({ x, y }, color); }
     void draw_line(IntPoint const&, IntPoint const&, Color, int thickness = 1, LineStyle style = LineStyle::Solid, Color alternate_color = Color::Transparent);
+    void draw_triangle_wave(IntPoint const&, IntPoint const&, Color color, int amplitude, int thickness = 1);
     void draw_quadratic_bezier_curve(IntPoint const& control_point, IntPoint const&, IntPoint const&, Color, int thickness = 1, LineStyle style = LineStyle::Solid);
     void draw_cubic_bezier_curve(IntPoint const& control_point_0, IntPoint const& control_point_1, IntPoint const&, IntPoint const&, Color, int thickness = 1, LineStyle style = LineStyle::Solid);
     void draw_elliptical_arc(IntPoint const& p1, IntPoint const& p2, IntPoint const& center, FloatPoint const& radii, float x_axis_rotation, float theta_1, float theta_delta, Color, int thickness = 1, LineStyle style = LineStyle::Solid);