From d975d7d207b1838ecbca936b86343767521bdf7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Ha=C3=9Fel?= Date: Thu, 23 Jan 2025 11:54:10 +0100 Subject: [PATCH] DisplayMatrix::drawLine --- src/patrix/display/DisplayMatrix.h | 2 ++ src/patrix/display/DisplayMatrix_draw.h | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/patrix/display/DisplayMatrix.h b/src/patrix/display/DisplayMatrix.h index f6335f5..afc903d 100644 --- a/src/patrix/display/DisplayMatrix.h +++ b/src/patrix/display/DisplayMatrix.h @@ -26,6 +26,8 @@ public: void fillRect(int w = W, int h = H) const; + void drawLine(int w = W, int h = H, int thickness = 1) const; + // print ---------------------------------------------------------------------------------------- void printf(const char *format, ...); diff --git a/src/patrix/display/DisplayMatrix_draw.h b/src/patrix/display/DisplayMatrix_draw.h index 2199b23..c50caa0 100644 --- a/src/patrix/display/DisplayMatrix_draw.h +++ b/src/patrix/display/DisplayMatrix_draw.h @@ -18,4 +18,27 @@ void DisplayMatrix::fillRect(const int w, const int h) const { } } +template +void DisplayMatrix::drawLine(const int w, const int h, const int thickness) const { + if (w >= h) { + const auto m = static_cast(h) / w; + for (auto t = 0; t < thickness; ++t) { + const auto offset = t % 2 == 0 ? t / 2 : -t / 2; + for (auto x = 0; x < w; ++x) { + const auto y = static_cast(round(offset + x * m)); + matrix[cursorY + y][cursorX + x].blend(foreground, alpha); + } + } + } else { + const auto m = static_cast(w) / h; + for (auto t = 0; t < thickness; ++t) { + const auto offset = t % 2 == 0 ? t / 2 : -t / 2; + for (auto y = 0; y < w; ++y) { + const auto x = static_cast(round(offset + y * m)); + matrix[cursorY + y][cursorX + x].blend(foreground, alpha); + } + } + } +} + #endif