fixed issue with vertex sorting leading to segfault

This commit is contained in:
2026-07-05 21:05:15 -07:00
parent c8db7a8933
commit 1d27f6e5d1
2 changed files with 14 additions and 4 deletions
BIN
View File
Binary file not shown.
+14 -4
View File
@@ -263,7 +263,7 @@ namespace UwU {
Coord2 mid = Coord2(); Coord2 mid = Coord2();
Coord2 btm = Coord2(); Coord2 btm = Coord2();
// cases: flat top, flat bottom, left point, right point // cases: flat top, flat bottom, left point, right point
if (coords.y0 < coords.y1 && coords.y0 < coords.y2) { if (coords.y0 <= coords.y1 && coords.y0 <= coords.y2) {
top.x = coords.x0; top.x = coords.x0;
top.y = coords.y0; top.y = coords.y0;
if (coords.y1 < coords.y2) { if (coords.y1 < coords.y2) {
@@ -277,7 +277,7 @@ namespace UwU {
btm.x = coords.x1; btm.x = coords.x1;
btm.y = coords.y1; btm.y = coords.y1;
} }
} else if (coords.y1 < coords.y0 && coords.y1 < coords.y2) { } else if (coords.y1 <= coords.y0 && coords.y1 <= coords.y2) {
top.x = coords.x1; top.x = coords.x1;
top.y = coords.y1; top.y = coords.y1;
if (coords.y0 < coords.y2) { if (coords.y0 < coords.y2) {
@@ -306,17 +306,24 @@ namespace UwU {
btm.y = coords.y0; btm.y = coords.y0;
} }
} }
printf("top: (%u, %u)\n", top.x, top.y);
printf("mid: (%u, %u)\n", mid.x, mid.y);
printf("btm: (%u, %u)\n", btm.x, btm.y);
uint16_t height = 1 + btm.y - top.y; uint16_t height = 1 + btm.y - top.y;
int16_t longBound[height]; int16_t longBound[height];
int16_t splitBound[height]; int16_t splitBound[height];
// int16_t rightBound[height]; // int16_t rightBound[height];
Helpers::bresenham(0, top.x, height - 1, btm.x, longBound); Helpers::bresenham(0, top.x, height - 1, btm.x, longBound);
printf("longBound\n");
if ((int16_t)mid.x < longBound[mid.y - top.y]) { if ((int16_t)mid.x < longBound[mid.y - top.y]) {
// left-handed triangle // left-handed triangle
printf("left-handed\n");
// rightBound = longBound; // rightBound = longBound;
Helpers::bresenham(0, top.x, mid.y - top.y, mid.x, splitBound); Helpers::bresenham(0, top.x, mid.y - top.y, mid.x, splitBound);
printf("splitTop\n");
Helpers::bresenham(mid.y - top.y, mid.x, height - 1, btm.x, splitBound); Helpers::bresenham(mid.y - top.y, mid.x, height - 1, btm.x, splitBound);
printf("splitBtm\n");
for (uint32_t i = 0; i < height; i++) { for (uint32_t i = 0; i < height; i++) {
for (uint32_t x = (uint32_t)splitBound[i]; x <= (uint32_t)longBound[i]; x++) { 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); putPixel(x, i + (uint32_t)top.y, color.r, color.g, color.b, color.a);
@@ -324,9 +331,12 @@ namespace UwU {
} }
} else { } else {
// right-handed triangle // right-handed triangle
printf("right-handed\n");
// leftBound = longBound; // leftBound = longBound;
Helpers::bresenham(0, top.x, mid.y - top.y, mid.x, splitBound); Helpers::bresenham(0, top.x, mid.y - top.y, mid.x, splitBound);
printf("splitTop\n");
Helpers::bresenham(mid.y - top.y, mid.x, height - 1, btm.x, splitBound); Helpers::bresenham(mid.y - top.y, mid.x, height - 1, btm.x, splitBound);
printf("splitBtm\n");
for (uint32_t i = 0; i < height; i++) { for (uint32_t i = 0; i < height; i++) {
for (uint32_t x = (uint32_t)longBound[i]; x <= (uint32_t)splitBound[i]; x++) { 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); putPixel(x, i + (uint32_t)top.y, color.r, color.g, color.b, color.a);
@@ -399,9 +409,9 @@ int main(void) {
draw.rectangle(UwU::Coord2(69, 69), UwU::Coord2(420, 420), fuchsia); 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.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(280, 100, 240, 100, 320, 300), green); draw.triangle(UwU::Triangle(280, 100, 240, 100, 320, 300), green);