LibGfx: Add support for dashed lines

Non-configurable for now.
This commit is contained in:
Linus Groh 2020-05-10 16:17:26 +01:00 committed by Andreas Kling
parent 3667677008
commit 0669dbcf5d
Notes: sideshowbarker 2024-07-19 06:45:24 +09:00
3 changed files with 32 additions and 11 deletions

View file

@ -92,20 +92,28 @@ void Canvas::draw()
painter.draw_rect({ 380, 140, 100, 100 }, Color::Yellow);
painter.draw_line({ 500, 20 }, { 750, 20 }, Color::Green, 1, Gfx::Painter::LineStyle::Solid);
painter.draw_line({ 500, 40 }, { 750, 40 }, Color::Red, 5, Gfx::Painter::LineStyle::Solid);
painter.draw_line({ 500, 60 }, { 750, 60 }, Color::Blue, 10, Gfx::Painter::LineStyle::Solid);
painter.draw_line({ 500, 30 }, { 750, 30 }, Color::Red, 5, Gfx::Painter::LineStyle::Solid);
painter.draw_line({ 500, 45 }, { 750, 45 }, Color::Blue, 10, Gfx::Painter::LineStyle::Solid);
painter.draw_line({ 500, 80 }, { 750, 80 }, Color::Green, 1, Gfx::Painter::LineStyle::Dotted);
painter.draw_line({ 500, 100 }, { 750, 100 }, Color::Red, 5, Gfx::Painter::LineStyle::Dotted);
painter.draw_line({ 500, 120 }, { 750, 120 }, Color::Blue, 10, Gfx::Painter::LineStyle::Dotted);
painter.draw_line({ 500, 60 }, { 750, 60 }, Color::Green, 1, Gfx::Painter::LineStyle::Dotted);
painter.draw_line({ 500, 70 }, { 750, 70 }, Color::Red, 5, Gfx::Painter::LineStyle::Dotted);
painter.draw_line({ 500, 85 }, { 750, 85 }, Color::Blue, 10, Gfx::Painter::LineStyle::Dotted);
painter.draw_line({ 500, 100 }, { 750, 100 }, Color::Green, 1, Gfx::Painter::LineStyle::Dashed);
painter.draw_line({ 500, 110 }, { 750, 110 }, Color::Red, 5, Gfx::Painter::LineStyle::Dashed);
painter.draw_line({ 500, 125 }, { 750, 125 }, Color::Blue, 10, Gfx::Painter::LineStyle::Dashed);
painter.draw_line({ 500, 140 }, { 500, 240 }, Color::Green, 1, Gfx::Painter::LineStyle::Solid);
painter.draw_line({ 520, 140 }, { 520, 240 }, Color::Red, 5, Gfx::Painter::LineStyle::Solid);
painter.draw_line({ 540, 140 }, { 540, 240 }, Color::Blue, 10, Gfx::Painter::LineStyle::Solid);
painter.draw_line({ 510, 140 }, { 510, 240 }, Color::Red, 5, Gfx::Painter::LineStyle::Solid);
painter.draw_line({ 525, 140 }, { 525, 240 }, Color::Blue, 10, Gfx::Painter::LineStyle::Solid);
painter.draw_line({ 560, 140 }, { 560, 240 }, Color::Green, 1, Gfx::Painter::LineStyle::Dotted);
painter.draw_line({ 580, 140 }, { 580, 240 }, Color::Red, 5, Gfx::Painter::LineStyle::Dotted);
painter.draw_line({ 600, 140 }, { 600, 240 }, Color::Blue, 10, Gfx::Painter::LineStyle::Dotted);
painter.draw_line({ 540, 140 }, { 540, 240 }, Color::Green, 1, Gfx::Painter::LineStyle::Dotted);
painter.draw_line({ 550, 140 }, { 550, 240 }, Color::Red, 5, Gfx::Painter::LineStyle::Dotted);
painter.draw_line({ 565, 140 }, { 565, 240 }, Color::Blue, 10, Gfx::Painter::LineStyle::Dotted);
painter.draw_line({ 580, 140 }, { 580, 240 }, Color::Green, 1, Gfx::Painter::LineStyle::Dashed);
painter.draw_line({ 590, 140 }, { 590, 240 }, Color::Red, 5, Gfx::Painter::LineStyle::Dashed);
painter.draw_line({ 605, 140 }, { 605, 240 }, Color::Blue, 10, Gfx::Painter::LineStyle::Dashed);
painter.draw_line({ 640, 190 }, { 740, 240 }, Color::Green, 1, Gfx::Painter::LineStyle::Solid);
painter.draw_line({ 640, 140 }, { 740, 240 }, Color::Red, 5, Gfx::Painter::LineStyle::Solid);

View file

@ -992,6 +992,12 @@ void Painter::draw_line(const Point& p1, const Point& p2, Color color, int thick
if (style == LineStyle::Dotted) {
for (int y = min_y; y <= max_y; y += thickness * 2)
draw_pixel({ x, y }, color, thickness);
} else if (style == LineStyle::Dashed) {
for (int y = min_y; y <= max_y; y += thickness * 6) {
draw_pixel({ x, y }, color, thickness);
draw_pixel({ x, min(y + thickness, max_y) }, color, thickness);
draw_pixel({ x, min(y + thickness * 2, max_y) }, color, thickness);
}
} else {
for (int y = min_y; y <= max_y; ++y)
draw_pixel({ x, y }, color, thickness);
@ -1015,6 +1021,12 @@ void Painter::draw_line(const Point& p1, const Point& p2, Color color, int thick
if (style == LineStyle::Dotted) {
for (int x = min_x; x <= max_x; x += thickness * 2)
draw_pixel({ x, y }, color, thickness);
} else if (style == LineStyle::Dashed) {
for (int x = min_x; x <= max_x; x += thickness * 6) {
draw_pixel({ x, y }, color, thickness);
draw_pixel({ min(x + thickness, max_x), y }, color, thickness);
draw_pixel({ min(x + thickness * 2, max_x), y }, color, thickness);
}
} else {
for (int x = min_x; x <= max_x; ++x)
draw_pixel({ x, y }, color, thickness);
@ -1022,7 +1034,7 @@ void Painter::draw_line(const Point& p1, const Point& p2, Color color, int thick
return;
}
// FIXME: Implement dotted diagonal lines.
// FIXME: Implement dotted/dashed diagonal lines.
ASSERT(style == LineStyle::Solid);
const double adx = abs(point2.x() - point1.x());

View file

@ -47,6 +47,7 @@ public:
enum class LineStyle {
Solid,
Dotted,
Dashed,
};
void clear_rect(const Rect&, Color);