From 02b3bbcea8e1a5a7c19fdd18eabe04a459416578 Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Sat, 15 Jun 2019 14:16:27 +0200 Subject: [PATCH] Added methods for drawing lines and circles to Window. --- include/Window.h | 5 +++- src/Window.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/include/Window.h b/include/Window.h index 6b7b053..1c01d65 100644 --- a/include/Window.h +++ b/include/Window.h @@ -18,7 +18,10 @@ public: void clear(CRGB* color); void setPixel(uint8_t x, uint8_t y, CRGB* color); void setPixelByIndex(uint16_t index, CRGB* color); - void line(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, CRGB* color, bool antialias=false); + void line(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, CRGB* color); + void lineWithAngle(uint8_t x, uint8_t y, uint8_t angle, uint8_t length, CRGB* color); + void lineWithAngle(uint8_t x, uint8_t y, uint8_t angle, uint8_t startdist, uint8_t length, CRGB* color); + void circle(uint8_t x, uint8_t y, uint8_t r, CRGB* color); uint16_t coordsToGlobalIndex(uint8_t x, uint8_t y); uint16_t localToGlobalIndex(uint16_t); void drawChar(Font* f, uint8_t x, uint8_t y, const char c, CRGB* color, bool mask=false); diff --git a/src/Window.cpp b/src/Window.cpp index f2353a0..92b19c7 100644 --- a/src/Window.cpp +++ b/src/Window.cpp @@ -60,3 +60,75 @@ void Window::fadeToBlackBy(fract8 speed) { leds[coordsToGlobalIndex(x, y)].nscale8(255 - speed); } } + +void Window::line(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, CRGB* color) { + // Bresenham algorithm + int16_t dx = abs(x2-x1); + int16_t dy = abs(y2-y1); + int8_t sx = x1 dy) { err += dy; x1 += sx; } + if (e2 < dx) { err += dx; y1 += sy; } + } +} + +void Window::circle(uint8_t x0, uint8_t y0, uint8_t radius, CRGB* color) { + // Again, Bresenham + uint8_t f = 1 - radius; + int16_t ddF_x = 0; + int16_t ddF_y = -2 * radius; + uint8_t x = 0; + uint8_t y = radius; + + setPixel(x0, y0 + radius, color); + setPixel(x0, y0 - radius, color); + setPixel(x0 + radius, y0, color); + setPixel(x0 - radius, y0, color); + + while (x < y) { + if (f >= 0) { + y--; + ddF_y += 2; + f += ddF_y; + } + x++; + ddF_x += 2; + f += ddF_x + 1; + + setPixel(x0 + x, y0 + y, color); + setPixel(x0 - x, y0 + y, color); + setPixel(x0 + x, y0 - y, color); + setPixel(x0 - x, y0 - y, color); + + setPixel(x0 + y, y0 + x, color); + setPixel(x0 - y, y0 + x, color); + setPixel(x0 + y, y0 - x, color); + setPixel(x0 - y, y0 - x, color); + } +} + +void Window::lineWithAngle(uint8_t x, uint8_t y, uint8_t angle, uint8_t length, CRGB* color) { + lineWithAngle(x, y, angle, 0, length, color); +} + +void Window::lineWithAngle(uint8_t x, uint8_t y, uint8_t angle, uint8_t startdist, uint8_t length, CRGB* color) { + int16_t x1 = x; + int16_t y1 = y; + + if (startdist > 0) { + x1 = x + scale8(startdist, cos8(angle)); + y1 = y + scale8(startdist, sin8(angle)); + } + + int16_t x2 = x + scale8(startdist + length, cos8(angle)); + int16_t y2 = y + scale8(startdist + length, sin8(angle)); + + line(x1, y1, x2, y2, color); +}