Skip to content

Commit 8a0b97e

Browse files
committed
ARC mapping optimization
The biggest optimization was to avoid sin_t / cos_t. Now let's try to help the compiler optimize the drawing loop.
1 parent e0f0886 commit 8a0b97e

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

wled00/FX_fcn.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -889,13 +889,19 @@ void IRAM_ATTR_YN Segment::setPixelColor(int i, uint32_t col) //WLEDMM: IRAM_ATT
889889
else {
890890
//WLEDMM: drawArc(0, 0, i, col); could work as alternative
891891

892-
float step = HALF_PI / (2.85f*i);
893-
for (float rad = 0.0f; rad <= HALF_PI+step/2; rad += step) {
892+
//WLEDMM: some opimizations for the drawing loop
893+
float radius = float(i); // pre-calculate, for some speed
894+
float step = HALF_PI / (2.85f * radius);
895+
unsigned numSteps = 1 + ((HALF_PI + step/2.0f) / step); // pre-calculate, so the compiler can better optimize the for loop
896+
float rad = 0.0f;
897+
for (unsigned count = 0; count < numSteps; count++) {
894898
// may want to try float version as well (with or without antialiasing)
895-
int x = roundf(sinf(rad) * i);
896-
int y = roundf(cosf(rad) * i);
899+
int x = roundf(sinf(rad) * radius);
900+
int y = roundf(cosf(rad) * radius);
897901
setPixelColorXY(x, y, col);
902+
rad += step;
898903
}
904+
899905
// Bresenham’s Algorithm (may not fill every pixel)
900906
//int d = 3 - (2*i);
901907
//int y = i, x = 0;

0 commit comments

Comments
 (0)