added trapezoid drawing

This commit is contained in:
2026-07-05 06:41:26 -07:00
parent 076e8d2e89
commit 27a322d135
2 changed files with 25 additions and 8 deletions
BIN
View File
Binary file not shown.
+25 -8
View File
@@ -117,15 +117,20 @@ namespace UwU {
return;
}
void bresenhamHigh(int16_t y0, uint16_t x1, int16_t y1, int16_t* LUT) {
int16_t dx = (int16_t)x1;
void bresenhamHigh(uint16_t x0, int16_t y0, uint16_t x1, int16_t y1, int16_t* LUT) {
int16_t dx = (int16_t)x1 - (int16_t)x0;
int16_t dy = y1 - y0;
int16_t xi = 1;
if (dx < 0) {
xi = -1;
dx = -dx;
}
int16_t d = (2 * dx) - dy;
int16_t x = 0;
int16_t x = (int16_t)x0;
for (int16_t y = y0; y <= y1; y++) {
LUT[x] = y;
if (d > 0) {
x++;
x += xi;
d += (2 * (dx - dy));
} else {
d += (2 * dx);
@@ -147,9 +152,9 @@ namespace UwU {
bresenhamLow(y0, x1, y1, LUT);
} else {
if (y0 > y1) {
bresenhamHigh(y1, x1, y0, LUT);
bresenhamHigh(x1, y1, 0, y0, LUT);
} else {
bresenhamHigh(y0, x1, y1, LUT);
bresenhamHigh(0, y0, x1, y1, LUT);
}
}
return;
@@ -323,7 +328,19 @@ namespace UwU {
// Draw a horizontally bounded trapezoid
// coords.y1 >= coords.y0
void trapezoid (Trapezoid coords, Color color) {
uint16_t height = 1 + coords.y1 - coords.y0;
int16_t LeftBound[height];
int16_t RightBound[height];
this->bresenham(coords.xl0, coords.y1 - coords.y0, coords.xl1, LeftBound);
this->bresenham(coords.xr0, coords.y1 - coords.y0, coords.xr1, RightBound);
for (uint16_t i = 0; i <= height; i++) {
for (uint16_t x = LeftBound[i]; x <= RightBound[i]; x++) {
this->putPixel((uint32_t)x, (uint32_t)i + coords.y0, color.r, color.g, color.b, color.a);
}
}
return;
}
};
}
@@ -346,7 +363,7 @@ int main(void) {
draw.rectangle(UwU::Coord2(69, 69), UwU::Coord2(420, 420), fuchsia);
draw.rectangleGradient(UwU::Coord2(200, 200), UwU::Coord2(600, 400), red, blue, alpha, red);
draw.trapezoid(UwU::Trapezoid(300, 400, 360, 360, 600, 401), green);
draw.point(UwU::Coord2(387, 43), green);