From cdb94f9313d56c0756eb090d424118c626af5203 Mon Sep 17 00:00:00 2001 From: Vladimir Smitka Date: Fri, 16 Oct 2026 21:09:40 +0000 Subject: [PATCH] Avoid modulo in Polygon pixel test get_pixel did two modulo operations per edge for every pixel, only to wrap around on the last edge. The RP2040 has no divide instruction, so this was most of the drawing time. --- shared-module/vectorio/Polygon.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/shared-module/vectorio/Polygon.c b/shared-module/vectorio/Polygon.c index 212d519e298..03a6160f930 100644 --- a/shared-module/vectorio/Polygon.c +++ b/shared-module/vectorio/Polygon.c @@ -161,11 +161,12 @@ uint32_t common_hal_vectorio_polygon_get_pixel(void *obj, int16_t x, int16_t y) int16_t winding_number = 0; int16_t x1 = self->points_list[0]; int16_t y1 = self->points_list[1]; - for (uint16_t i = 2; i <= self->len + 1; ++i) { + for (uint16_t i = 2; i <= self->len; i += 2) { VECTORIO_POLYGON_DEBUG(" {(%3d, %3d),", x1, y1); - int16_t x2 = self->points_list[i % self->len]; - ++i; - int16_t y2 = self->points_list[i % self->len]; + // The last edge closes the polygon back to the first point. + uint16_t next = i == self->len ? 0 : i; + int16_t x2 = self->points_list[next]; + int16_t y2 = self->points_list[next + 1]; VECTORIO_POLYGON_DEBUG(" (%3d, %3d)}\n", x2, y2); if (y1 <= y) { if (y2 > y && line_side(x1, y1, x2, y2, x, y) < 0) {