Added bounds checking for triangles.

This commit is contained in:
2025-12-20 10:33:12 -05:00
parent cffc64f3e9
commit 3bf2a7b429
10 changed files with 126 additions and 116 deletions

26
src/geom.c Normal file
View File

@@ -0,0 +1,26 @@
#include "include/geom.h"
Tri* alltris[512];
size_t allsize = 0;
Tri* tri_init(Pt a, Pt b, Pt c) {
Tri* tri = malloc(sizeof(Tri));
*(alltris + allsize) = tri;
allsize++;
*tri = (Tri){a, b, c};
return tri;
}
bool tri_within(Pt pt, Tri* tri) {
double denom =
((tri->b.y - tri->c.y) * (tri->a.x - tri->c.x) +
(tri->c.x - tri->b.x) * (tri->a.y - tri->c.y));
double a = ((tri->b.y - tri->c.y) * (pt.x - tri->c.x) +
(tri->c.x - tri->b.x) * (pt.y - tri->c.y)) /
denom;
double b = ((tri->c.y - tri->a.y) * (pt.x - tri->c.x) +
(tri->a.x - tri->c.x) * (pt.y - tri->c.y)) /
denom;
double c = 1 - a - b;
return 0 <= a && a <= 1 && 0 <= b && b <= 1 && 0 <= c && c <= 1;
}