diff --git a/src/geom.c b/src/geom.c index d14e651..c33004a 100644 --- a/src/geom.c +++ b/src/geom.c @@ -24,3 +24,8 @@ bool tri_within(Pt pt, Tri* tri) { double c = 1 - a - b; return 0 <= a && a <= 1 && 0 <= b && b <= 1 && 0 <= c && c <= 1; } + +Pt tri_center(Tri* tri) { + return (Pt){(tri->a.x + tri->b.x + tri->c.x) / 3.0, + (tri->a.y + tri->b.y + tri->c.y) / 3.0}; +} diff --git a/src/include/geom.h b/src/include/geom.h index 66b75d0..7abf65f 100644 --- a/src/include/geom.h +++ b/src/include/geom.h @@ -32,6 +32,9 @@ void tri_destroy(Tri* tri); // Check if a point is within the triangle. Uses barycentric coordinate system. bool tri_within(Pt pt, Tri* tri); +// Get the center point of a Tri. +Pt tri_center(Tri* tri); + typedef struct { Color col; size_t ntris; diff --git a/src/include/util.h b/src/include/util.h index f9a7d6e..d842515 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -1,9 +1,9 @@ #ifndef UTIL_H #define UTIL_H -#define MAX(a, b) a ? a > b : b -#define MAX3(a, b, c) MAX(a, MAX(b, c)) -#define MIN(a, b) a ? a < b : b -#define MIN3(a, b, c) MIN(a, MIN(b, c)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define MAX3(a, b, c) (MAX((a), MAX((b), (c)))) +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MIN3(a, b, c) (MIN((a), MIN((b), (c)))) #endif diff --git a/src/render.c b/src/render.c index 7c07bb2..ede028e 100644 --- a/src/render.c +++ b/src/render.c @@ -27,16 +27,19 @@ void draw_tri(Tri* tri) { ); } -// bad. void draw_tri_inside(Tri* tri, Color col) { - // int maxx = MAX3(tri->a.x, tri->b.x, tri->c.x); - // int maxy = MAX3(tri->a.y, tri->b.y, tri->c.y); - // int minx = MIN3(tri->a.y, tri->b.y, tri->c.y); - // int miny = MIN3(tri->a.y, tri->b.y, tri->c.y); - for (int x = 0; x <= WORLD_SZ; x++) { - for (int y = 0; y < WORLD_SZ; y++) { + int maxx = MAX3(tri->a.x, tri->b.x, tri->c.x); + int maxy = MAX3(tri->a.y, tri->b.y, tri->c.y); + int minx = MIN3(tri->a.x, tri->b.x, tri->c.x); + int miny = MIN3(tri->a.y, tri->b.y, tri->c.y); + for (int x = minx; x <= maxx; x++) { + for (int y = miny; y <= maxy; y++) { if (tri_within((Pt){x, y}, tri)) { - DrawCircleV(PointToVector2((Pt){x, y}), 4.f, col); + // DrawCircleV(PointToVector2((Pt){x, y}), 4.f, col); + DrawLineV( + PointToVector2((Pt){x, y}), PointToVector2(tri_center(tri)), + col + ); } } }