diff options
author | Tobias Christiansen <tobyase@serenityos.org> | 2022-01-20 19:55:14 +0100 |
---|---|---|
committer | Ali Mohammad Pur <Ali.mpfard@gmail.com> | 2022-01-23 15:48:27 +0330 |
commit | 0277118cb4094e7a2a92b5d33263cda9a5fd2b75 (patch) | |
tree | a204a6a5dd5ec74f41965a8abf5763bfb80d631e /Userland/Libraries/LibGfx | |
parent | 7b51102495a807bd785df83295b9f979260699e6 (diff) | |
download | serenity-0277118cb4094e7a2a92b5d33263cda9a5fd2b75.zip |
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.
Diffstat (limited to 'Userland/Libraries/LibGfx')
-rw-r--r-- | Userland/Libraries/LibGfx/Painter.cpp | 20 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Painter.h | 1 |
2 files changed, 21 insertions, 0 deletions
diff --git a/Userland/Libraries/LibGfx/Painter.cpp b/Userland/Libraries/LibGfx/Painter.cpp index d58b506e10..3576d32f5b 100644 --- a/Userland/Libraries/LibGfx/Painter.cpp +++ b/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; diff --git a/Userland/Libraries/LibGfx/Painter.h b/Userland/Libraries/LibGfx/Painter.h index 8b9e407846..e95be51324 100644 --- a/Userland/Libraries/LibGfx/Painter.h +++ b/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); |