prevent drawLine from drawing out of bounds

the "smooth" variant was sometimes producing XY positions outside the canvas.
This commit is contained in:
Frank
2024-10-24 18:16:43 +02:00
parent fe5eaed99c
commit 05adb7dfae

View File

@@ -806,8 +806,13 @@ void Segment::drawLine(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint3
int y = int(intersectY);
if (steep) std::swap(x,y); // temporarily swap if steep
// pixel coverage is determined by fractional part of y co-ordinate
setPixelColorXY(x, y, color_blend(c, getPixelColorXY(x, y), keep, true));
setPixelColorXY(x+int(steep), y+int(!steep), color_blend(c, getPixelColorXY(x+int(steep), y+int(!steep)), seep, true));
// WLEDMM added out-of-bounds check: "unsigned(x) < cols" catches negative numbers _and_ too large values
if ((unsigned(x) < unsigned(cols)) && (unsigned(y) < unsigned(rows))) setPixelColorXY(x, y, color_blend(c, getPixelColorXY(x, y), keep, true));
int xx = x+int(steep);
int yy = y+int(!steep);
if ((unsigned(xx) < unsigned(cols)) && (unsigned(yy) < unsigned(rows))) setPixelColorXY(xx, yy, color_blend(c, getPixelColorXY(xx, yy), seep, true));
intersectY += gradient;
if (steep) std::swap(x,y); // restore if steep
}