In the implementation, the tri_sign function computes this 2D cross product:
function tri_sign(x1, y1, x2, y2, x3, y3) {
return (x1 - x3) * (y2 - y3) - (x2 - x3) * (y1 - y3);
}
function point_in_triangle(px, py, ax, ay, bx, by, cx, cy) {
var d1 = tri_sign(px, py, ax, ay, bx, by);
var d2 = tri_sign(px, py, bx, by, cx, cy);
var d3 = tri_sign(px, py, cx, cy, ax, ay);
return !((d1 < 0 || d2 < 0 || d3 < 0) && (d1 > 0 || d2 > 0 || d3 > 0));
}
The final boolean expression is equivalent to checking
!(has_negative && has_positive),
which is true only when all signs agree.