Added bounds checking for triangles.
This commit is contained in:
26
src/geom.c
Normal file
26
src/geom.c
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user