A-buffer 8x4 antialiased polygon fill (see file header).
MAXN caps the vertex count (vertices beyond it are ignored, polygon closes from #MAXN-1 back to #0 — same as the exact kernel). There is no accumulator to size: the working set is the per-edge state plus one u32 mask, so the stack stays shallow regardless of canvas size. It still scales with MAXN: vertices (8*MAXN B) + edges (40-48*MAXN B) + crossing events (16*MAXN B), too much for an AVR Uno at MAXN = 32, so the default MAXN is platform-dependent (measured frames at FL_GFX_POLYGON_DEFAULT_MAXN in fl/gfx/canvas.h).
276 {
277 FL_STATIC_ASSERT(MAXN >= 3 && MAXN <= 256,
"drawPolygon: MAXN must be in [3, 256]");
278 const fl::i32 n =
static_cast<fl::i32
>(pts.
size());
279 if (n < 3) return;
280 PixelT* pixels =
canvas.pixels;
284
285 const int nv = (n < MAXN) ? n : MAXN;
286
287
288 fl::i32 vx[MAXN];
289 fl::i32 vy[MAXN];
290 for (int i = 0; i < nv; ++i) {
293 }
294
295
296
297 AbEdge edges[MAXN];
298 int nedges = 0;
299 int ymin =
height, ymax = -1;
300 for (int i = 0; i < nv; ++i) {
301 const int j = (i + 1 == nv) ? 0 : (i + 1);
302 const fl::i32 dy = vy[j] - vy[i];
303 if (dy == 0) continue;
304
305 fl::i32 ytop_q, xtop,
B;
306 if (dy > 0) { ytop_q = vy[i]; xtop = vx[i];
B = vx[j] - vx[i]; }
307 else { ytop_q = vy[j]; xtop = vx[j];
B = vx[i] - vx[j]; }
308 const fl::i32 dy2 = (dy > 0) ? dy : -dy;
309 const fl::i32 ybot_q = ytop_q + dy2;
310
311
312
313 int py0 = ytop_q >> 16;
314 int py1 = (ybot_q - 1) >> 16;
315 if (py0 < 0) py0 = 0;
317 if (py0 > py1) continue;
318
319 AbEdge& e = edges[nedges++];
320 e.py0 = py0;
321 e.py1 = py1;
322
323
324
325
326
327
328
329
330 e.ytop = (ytop_q + 16383) >> 14;
331 e.ybot = (ybot_q + 16383) >> 14;
332
334 e.q = e.xinc >> 2;
335
336
337
338
339
340
341
342
345
346
347
348
349
350 const fl::i32 m = (fl::i32)(e.xinc & 3);
351 const fl::i64 n0 = ((off & 65535L) * (e.xinc & 65535L)) & 65535L;
352 e.corr[0] = 0;
356
357 if (e.py0 < ymin) ymin = e.py0;
358 if (e.py1 > ymax) ymax = e.py1;
359 }
360 if (nedges == 0) return;
361
362
363
364
365 fl::i32 cp[4 * MAXN];
366
367 for (int py = ymin; py <= ymax; ++py) {
368 fl::u32 mask = 0;
369 int ncp = 0;
370
371 for (int e = 0; e < nedges; ++e) {
373 if (py < ed.py0 || py > ed.py1) continue;
374 if (py > ed.py0) ed.
x0 += ed.xinc;
375
376
377
378
379 const fl::i32 sub = (fl::i32)4 * py;
380 fl::i32 rlo32 = ed.ytop - sub;
381
382 if (rlo32 < 0) rlo32 = 0;
383 fl::i32 rhi32 = ed.ybot - 1 - sub;
384 if (rhi32 < 0) rhi32 = 0;
385 if (rhi32 > 3) rhi32 = 3;
386 if (rlo32 > rhi32) continue;
387 const int rlo = (int)rlo32, rhi = (int)rhi32;
388
389
391 for (int r = 0; r <= rhi; ++r) {
392 if (r > 0) X += ed.q + (
fl::i64)((fl::i32)(ed.corr[r] - ed.corr[r - 1]));
393 if (r < rlo) continue;
394 if (X < 0) {
395
396
397 mask ^= (fl::u32)0xFFu << (8 * r);
398 } else {
399 const fl::i32 p = (fl::i32)(X >> 16);
401 const fl::u32 bo = (fl::u32)((X - ((
fl::i64)p << 16)) >> 13);
402 cp[ncp] = (p << 5) | (r << 3) | (fl::i32)bo;
403 ++ncp;
404 }
405 }
406 }
407 }
408
409 if (ncp == 0) {
410
411 if (mask != 0)
414 continue;
415 }
416
417
418 for (int a = 1; a < ncp; ++a) {
419 const fl::i32 v = cp[a];
420 int b = a - 1;
421 while (b >= 0 && cp[b] > v) { cp[b + 1] = cp[b]; --b; }
422 cp[b + 1] = v;
423 }
424
425
426
427
429 for (int k = 0; k < ncp; ) {
430 const int p = cp[k] >> 5;
432 if (mask != 0)
436 }
437
438
439 fl::u32 rowxor = 0;
440 while (k < ncp && (cp[k] >> 5) == p) {
441 rowxor ^= (fl::u32)((1u << (8 - (cp[k] & 7u))) - 1u)
442 << (8 * ((cp[k] >> 3) & 3u));
443 ++k;
444 }
445 mask ^= rowxor;
446 if (mask != 0) {
448 PixelT c = color;
449 if (br < 255u) c.nscale8(br);
450 PixelT& px = pixels[py *
width + p];
451 if (Overwrite) px = c; else px += c;
452 }
453
454
455
456
457 mask = (mask & 0x01010101u) * 255u;
459 }
460 if (
x <
width && mask != 0)
463 }
464}
fl::CanvasRGB canvas(fl::span< CRGB >(leds, NUM_LEDS), WIDTH, HEIGHT)
constexpr fl::size size() const FL_NO_EXCEPT
fl::u8 polyABAlpha(fl::u32 mask) FL_NO_EXCEPT
Alpha (0..255) for an 8x4 mask: 8 per subpixel, saturating at 255 (32 subpixels would otherwise wrap ...
void polyABFillRun(PixelT *pixels, int py, int width, int x0, int x1, const PixelT &color, fl::u8 br, bool overwrite) FL_NO_EXCEPT
Fill a run of [x0, x1) pixels of row py with color at alpha br.
fl::i64 polyABFloorDivI64(fl::i64 a, fl::i64 b) FL_NO_EXCEPT
Floor-divide a by positive b (rounds toward -inf).
fl::i32 polyABToQ16(Coord val) FL_NO_EXCEPT
Convert any Coord type to Q16.16, clamped to ±8191 px.
#define FL_STATIC_ASSERT(...)