code consolidation in drawCircle() (#4302)

- saves about 600bytes of flash
- speed tradoff: drawing is now a tiny bit slower but insignificant in my tests
This commit is contained in:
Damian Schneider 2024-12-19 20:19:42 +01:00 committed by GitHub
parent 26397ee8ad
commit 83da7569f5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -507,31 +507,33 @@ void Segment::drawCircle(uint16_t cx, uint16_t cy, uint8_t radius, uint32_t col,
if (!isActive() || radius == 0) return; // not active if (!isActive() || radius == 0) return; // not active
if (soft) { if (soft) {
// Xiaolin Wus algorithm // Xiaolin Wus algorithm
int rsq = radius*radius; const int rsq = radius*radius;
int x = 0; int x = 0;
int y = radius; int y = radius;
unsigned oldFade = 0; unsigned oldFade = 0;
while (x < y) { while (x < y) {
float yf = sqrtf(float(rsq - x*x)); // needs to be floating point float yf = sqrtf(float(rsq - x*x)); // needs to be floating point
uint16_t fade = float(0xFF) * (ceilf(yf) - yf); // how much color to keep uint8_t fade = float(0xFF) * (ceilf(yf) - yf); // how much color to keep
if (oldFade > fade) y--; if (oldFade > fade) y--;
oldFade = fade; oldFade = fade;
setPixelColorXY(cx+x, cy+y, color_blend(col, getPixelColorXY(cx+x, cy+y), fade)); int px, py;
setPixelColorXY(cx-x, cy+y, color_blend(col, getPixelColorXY(cx-x, cy+y), fade)); for (uint8_t i = 0; i < 16; i++) {
setPixelColorXY(cx+x, cy-y, color_blend(col, getPixelColorXY(cx+x, cy-y), fade)); int swaps = (i & 0x4 ? 1 : 0); // 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1
setPixelColorXY(cx-x, cy-y, color_blend(col, getPixelColorXY(cx-x, cy-y), fade)); int adj = (i < 8) ? 0 : 1; // 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1
setPixelColorXY(cx+y, cy+x, color_blend(col, getPixelColorXY(cx+y, cy+x), fade)); int dx = (i & 1) ? -1 : 1; // 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1
setPixelColorXY(cx-y, cy+x, color_blend(col, getPixelColorXY(cx-y, cy+x), fade)); int dy = (i & 2) ? -1 : 1; // 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1
setPixelColorXY(cx+y, cy-x, color_blend(col, getPixelColorXY(cx+y, cy-x), fade)); if (swaps) {
setPixelColorXY(cx-y, cy-x, color_blend(col, getPixelColorXY(cx-y, cy-x), fade)); px = cx + (y - adj) * dx;
setPixelColorXY(cx+x, cy+y-1, color_blend(getPixelColorXY(cx+x, cy+y-1), col, fade)); py = cy + x * dy;
setPixelColorXY(cx-x, cy+y-1, color_blend(getPixelColorXY(cx-x, cy+y-1), col, fade)); } else {
setPixelColorXY(cx+x, cy-y+1, color_blend(getPixelColorXY(cx+x, cy-y+1), col, fade)); px = cx + x * dx;
setPixelColorXY(cx-x, cy-y+1, color_blend(getPixelColorXY(cx-x, cy-y+1), col, fade)); py = cy + (y - adj) * dy;
setPixelColorXY(cx+y-1, cy+x, color_blend(getPixelColorXY(cx+y-1, cy+x), col, fade)); }
setPixelColorXY(cx-y+1, cy+x, color_blend(getPixelColorXY(cx-y+1, cy+x), col, fade)); uint32_t pixCol = getPixelColorXY(px, py);
setPixelColorXY(cx+y-1, cy-x, color_blend(getPixelColorXY(cx+y-1, cy-x), col, fade)); setPixelColorXY(px, py, adj ?
setPixelColorXY(cx-y+1, cy-x, color_blend(getPixelColorXY(cx-y+1, cy-x), col, fade)); color_blend(pixCol, col, fade) :
color_blend(col, pixCol, fade));
}
x++; x++;
} }
} else { } else {
@ -539,14 +541,12 @@ void Segment::drawCircle(uint16_t cx, uint16_t cy, uint8_t radius, uint32_t col,
int d = 3 - (2*radius); int d = 3 - (2*radius);
int y = radius, x = 0; int y = radius, x = 0;
while (y >= x) { while (y >= x) {
setPixelColorXY(cx+x, cy+y, col); for (int i = 0; i < 4; i++) {
setPixelColorXY(cx-x, cy+y, col); int dx = (i & 1) ? -x : x;
setPixelColorXY(cx+x, cy-y, col); int dy = (i & 2) ? -y : y;
setPixelColorXY(cx-x, cy-y, col); setPixelColorXY(cx + dx, cy + dy, col);
setPixelColorXY(cx+y, cy+x, col); setPixelColorXY(cx + dy, cy + dx, col);
setPixelColorXY(cx-y, cy+x, col); }
setPixelColorXY(cx+y, cy-x, col);
setPixelColorXY(cx-y, cy-x, col);
x++; x++;
if (d > 0) { if (d > 0) {
y--; y--;