refactored triangle, still getting segfault on triangles with horizontal upper bound

This commit is contained in:
2026-07-05 20:54:07 -07:00
parent e4febca4bd
commit cfd05df3f5
2 changed files with 70 additions and 41 deletions
BIN
View File
Binary file not shown.
+70 -41
View File
@@ -307,49 +307,78 @@ namespace UwU {
} }
} }
// handle case where top or bottom is flat uint16_t height = 1 + btm.y - top.y;
if (mid.y == top.y) { int16_t longBound[height];
// flat top int16_t splitBound[height];
if (mid.x < top.x) { // int16_t rightBound[height];
trapezoid(Trapezoid(top.y, btm.y, mid.x, btm.x, top.x, btm.x), color); Helpers::bresenham(0, top.x, height - 1, btm.x, longBound);
return; if ((int16_t)mid.x < longBound[mid.y - top.y]) {
} else { // left-handed triangle
trapezoid(Trapezoid(top.y, btm.y, top.x, btm.x, mid.x, btm.x), color); // rightBound = longBound;
return; Helpers::bresenham(0, top.x, mid.y - top.y, mid.x, splitBound);
Helpers::bresenham(mid.y - top.y, mid.x, height - 1, btm.x, splitBound);
for (uint32_t i = 0; i < height; i++) {
for (uint32_t x = (uint32_t)splitBound[i]; x <= (uint32_t)longBound[i]; x++) {
putPixel(x, i + (uint32_t)top.y, color.r, color.g, color.b, color.a);
}
} }
} else if (mid.y == btm.y) {
// flat bottom
if (mid.x < btm.x) {
trapezoid(Trapezoid(top.y, btm.y, top.x, mid.x, top.x, btm.x), color);
return;
} else {
trapezoid(Trapezoid(top.y, btm.y, top.x, btm.x, top.x, mid.x), color);
return;
}
}
// calculate midpoint of straight line
int32_t dy = (int32_t)btm.y - (int32_t)top.y;
if (dy == 0) {
return; // shouldn't be possible, should be detected earlier
}
int32_t dx = (int32_t)btm.x - (int32_t)top.x;
dx *= (mid.y - top.y);
dx /= dy;
dx += top.x;
uint16_t oppx = (uint16_t)dx;
// split triangle into two trapezoids
if (mid.x < oppx) {
trapezoid(Trapezoid(top.y, mid.y, top.x, mid.x, top.x, oppx), color);
trapezoid(Trapezoid(mid.y, btm.y, mid.x, btm.x, oppx, btm.x), color);
return;
} else { } else {
trapezoid(Trapezoid(top.y, mid.y, top.x, oppx, top.x, mid.x), color); // right-handed triangle
trapezoid(Trapezoid(mid.y, btm.y, oppx, btm.x, mid.x, btm.x), color); // leftBound = longBound;
return; Helpers::bresenham(0, top.x, mid.y - top.y, mid.x, splitBound);
Helpers::bresenham(mid.y - top.y, mid.x, height - 1, btm.x, splitBound);
for (uint32_t i = 0; i < height; i++) {
for (uint32_t x = (uint32_t)longBound[i]; x <= (uint32_t)splitBound[i]; x++) {
putPixel(x, i + (uint32_t)top.y, color.r, color.g, color.b, color.a);
}
}
} }
return; // nothing should ever reach this
// // handle case where top or bottom is flat
// if (mid.y == top.y) {
// // flat top
// if (mid.x < top.x) {
// trapezoid(Trapezoid(top.y, btm.y, mid.x, btm.x, top.x, btm.x), color);
// return;
// } else {
// trapezoid(Trapezoid(top.y, btm.y, top.x, btm.x, mid.x, btm.x), color);
// return;
// }
// } else if (mid.y == btm.y) {
// // flat bottom
// if (mid.x < btm.x) {
// trapezoid(Trapezoid(top.y, btm.y, top.x, mid.x, top.x, btm.x), color);
// return;
// } else {
// trapezoid(Trapezoid(top.y, btm.y, top.x, btm.x, top.x, mid.x), color);
// return;
// }
// }
// // calculate midpoint of straight line
// int32_t dy = (int32_t)btm.y - (int32_t)top.y;
// if (dy == 0) {
// return; // shouldn't be possible, should be detected earlier
// }
// int32_t dx = (int32_t)btm.x - (int32_t)top.x;
// dx *= (mid.y - top.y);
// dx /= dy;
// dx += top.x;
// uint16_t oppx = (uint16_t)dx;
// // split triangle into two trapezoids
// if (mid.x < oppx) {
// trapezoid(Trapezoid(top.y, mid.y, top.x, mid.x, top.x, oppx), color);
// trapezoid(Trapezoid(mid.y, btm.y, mid.x, btm.x, oppx, btm.x), color);
// return;
// } else {
// trapezoid(Trapezoid(top.y, mid.y, top.x, oppx, top.x, mid.x), color);
// trapezoid(Trapezoid(mid.y, btm.y, oppx, btm.x, mid.x, btm.x), color);
// return;
// }
return;
} }
}; };
} }
@@ -373,7 +402,7 @@ int main(void) {
draw.rectangleGradient(UwU::Coord2(200, 200), UwU::Coord2(600, 400), red, blue, alpha, red); draw.rectangleGradient(UwU::Coord2(200, 200), UwU::Coord2(600, 400), red, blue, alpha, red);
draw.trapezoid(UwU::Trapezoid(300, 400, 360, 380, 600, 400), green); draw.trapezoid(UwU::Trapezoid(300, 400, 360, 380, 600, 400), green);
draw.triangle(UwU::Triangle(200, 100, 240, 300, 320, 300), green); draw.triangle(UwU::Triangle(200, 100, 240, 100, 320, 300), green);
draw.point(UwU::Coord2(387, 43), green); draw.point(UwU::Coord2(387, 43), green);