diff --git a/include/Window.h b/include/Window.h index 9a8933c..02095ad 100644 --- a/include/Window.h +++ b/include/Window.h @@ -4,6 +4,8 @@ #include "prototypes.h" class Window { +private: + void _circle_point(int x0, int y0, int x1, int y1, CRGB* color); public: const uint8_t x, y; const uint8_t width, height; diff --git a/src/Window.cpp b/src/Window.cpp index b85d0f7..5208962 100644 --- a/src/Window.cpp +++ b/src/Window.cpp @@ -68,54 +68,52 @@ void Window::fadeToBlackBy(fract8 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; } + int dx = x2-x1; + int dy = y2-y1; + + int x = x1; + int y = y1; + + int p = 2*dy - dx; + + while (x < x2) { + if (p >= 0) { + setPixel(x, y, color); + y++; + p = p + 2*dy - 2*dx; + } else { + setPixel(x, y, color); + p = p + 2*dy; + } + x++; } } +void Window::_circle_point(int x0, int y0, int x1, int y1, CRGB* color) { + setPixel(x0+x1, y0+y1, color); + setPixel(x0-x1, y0+y1, color); + setPixel(x0+x1, y0-y1, color); + setPixel(x0-x1, y0-y1, color); + setPixel(x0+y1, y0+x1, color); + setPixel(x0-y1, y0+x1, color); + setPixel(x0+y1, y0-x1, color); + setPixel(x0-y1, y0-x1, color); +} + 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; - } + int x=0, y=radius; + int d=3 - 2*radius; + _circle_point(x0, y0, x, y, color); + while (y >= x) { 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); + if (d>0) { + y--; + d = d + 4*(x-y) + 10; + } else { + d = d + 4*x + 6; + } + _circle_point(x0, y0, x, y, color); } } diff --git a/test.txt b/test.txt deleted file mode 100644 index 240fa21..0000000 --- a/test.txt +++ /dev/null @@ -1,18 +0,0 @@ -Original: -DATA: [==== ] 40.5% (used 33164 bytes from 81920 bytes) -PROGRAM: [==== ] 36.0% (used 375664 bytes from 1044464 bytes) - - -lots of compares: -DATA: [==== ] 38.6% (used 31640 bytes from 81920 bytes) -PROGRAM: [==== ] 35.6% (used 371740 bytes from 1044464 bytes) - -crc32: -DATA: [==== ] 38.5% (used 31532 bytes from 81920 bytes) -PROGRAM: [==== ] 35.6% (used 371456 bytes from 1044464 bytes) - -original: -DATA: [==== ] 39.4% (used 32256 bytes from 81920 bytes) │pitrix_dev/log MQTT * Received data for topic pitrix_dev/uptime with payload 391 -PROGRAM: [==== ] 37.4% (used 390380 bytes from 1044464 bytes) -DATA: [==== ] 37.4% (used 30608 bytes from 81920 bytes) │pitrix_dev/free_heap 43624 -PROGRAM: [==== ] 37.4% (used 390556 bytes from 1044464 bytes)