Skip to content

Commit 2ee702e

Browse files
committed
Add 'drawLineAntialiased'
Nicer way for the hands of the clock to be displayed.
1 parent 3473794 commit 2ee702e

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

wled00/FX.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4894,9 +4894,9 @@ uint16_t mode_2DAnalogClock(void) { // By Andras Fekete (bandi1
48944894

48954895
SEGMENT.fill(BLACK);
48964896
SEGMENT.draw_circle_antialiased(centerX, centerY, radius, DARKSLATEGRAY);
4897-
SEGMENT.drawLine(centerX, centerY, second_x, second_y, BLUE);
4898-
SEGMENT.drawLine(centerX, centerY, minute_x, minute_y, GREEN);
4899-
SEGMENT.drawLine(centerX, centerY, hour_x, hour_y, RED);
4897+
SEGMENT.drawLineAntialiased(centerX, centerY, second_x, second_y, BLUE);
4898+
SEGMENT.drawLineAntialiased(centerX, centerY, minute_x, minute_y, GREEN);
4899+
SEGMENT.drawLineAntialiased(centerX, centerY, hour_x, hour_y, RED);
49004900

49014901
return (1000 / WLED_FPS); // calculate once per second
49024902
} // mode_2DAnalogClock()

wled00/FX.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,7 @@ typedef struct Segment {
630630
void fill_circle(uint16_t cx, uint16_t cy, uint8_t radius, CRGB c);
631631
void drawLine(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint32_t c);
632632
inline void drawLine(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, CRGB c) { drawLine(x0, y0, x1, y1, RGBW32(c.r,c.g,c.b,0)); } // automatic inline
633+
void drawLineAntialiased(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint32_t c);
633634
void drawCharacter(unsigned char chr, int16_t x, int16_t y, uint8_t w, uint8_t h, uint32_t color, uint32_t col2 = 0, int8_t rotate = 0);
634635
inline void drawCharacter(unsigned char chr, int16_t x, int16_t y, uint8_t w, uint8_t h, CRGB c) { drawCharacter(chr, x, y, w, h, RGBW32(c.r,c.g,c.b,0)); } // automatic inline
635636
inline void drawCharacter(unsigned char chr, int16_t x, int16_t y, uint8_t w, uint8_t h, CRGB c, CRGB c2, int8_t rotate = 0) { drawCharacter(chr, x, y, w, h, RGBW32(c.r,c.g,c.b,0), RGBW32(c2.r,c2.g,c2.b,0), rotate); } // automatic inline

wled00/FX_2Dfcn.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,33 @@ void Segment::drawLine(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint3
539539
}
540540
}
541541

542+
//line function
543+
void Segment::drawLineAntialiased(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint32_t c) {
544+
if (!isActive()) return; // not active
545+
const uint16_t cols = virtualWidth();
546+
const uint16_t rows = virtualHeight();
547+
if (x0 >= cols || x1 >= cols || y0 >= rows || y1 >= rows) return;
548+
const int16_t dx = abs(x1-x0), sx = x0<x1 ? 1 : -1;
549+
const int16_t dy = abs(y1-y0), sy = y0<y1 ? 1 : -1;
550+
int16_t err = (dx-dy)/2, e2, x2;
551+
int16_t ed = (dx+dy == 0) ? 1 : sqrt((float)dx*dx + (float)dy*dy);
552+
553+
for (;;) {
554+
setPixelColorXY(x0,y0,color_blend(c,getPixelColorXY(x0,y0),0xFFFF * abs(err - dx + dy) / ed, true));
555+
e2 = err; x2 = x0;
556+
if (e2 >= -dx) {
557+
if(x0 == x1) break;
558+
if (e2+dy < ed) setPixelColorXY(x0,y0+sy, color_blend(c,getPixelColorXY(x0,y0+sy), 0xFFFF * (e2+dy)/ed, true));
559+
err -= dy; x0 += sx;
560+
}
561+
if (e2 <= dy) {
562+
if (y0 == y1) break;
563+
if (dx-e2 < ed) setPixelColorXY(x2+sx, y0, color_blend(c,getPixelColorXY(x2+sx,y0), 0xFFFF * (dx-e2)/ed, true));
564+
err += dx; y0 += sy;
565+
}
566+
}
567+
}
568+
542569
#include "src/font/console_font_4x6.h"
543570
#include "src/font/console_font_5x8.h"
544571
#include "src/font/console_font_5x12.h"

0 commit comments

Comments
 (0)