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
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;
uint16_t height = 1 + btm.y - top.y;
int16_t longBound[height];
int16_t splitBound[height];
// int16_t rightBound[height];
Helpers::bresenham(0, top.x, height - 1, btm.x, longBound);
if ((int16_t)mid.x < longBound[mid.y - top.y]) {
// left-handed triangle
// rightBound = longBound;
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 {
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;
// right-handed triangle
// leftBound = longBound;
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.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);