function is_point_in_triangle(p, t)
local a = {
t[2].x*t[3].y,
-t[2].y*t[3].x,
t[1].x*( t[2].y-t[3].y),
t[1].y*(-t[2].x+t[3].x)
}
local b = {
t[1].x*t[2].y,
-t[1].y*t[2].x,
p.x*(t[1].y-t[2].y),
p.y*(t[2].x-t[1].x)
}
local c = {
t[1].y*t[3].x,
-t[1].x*t[3].y,
p.x*(t[3].y-t[1].y),
p.y*(t[1].x-t[3].x)
}
local sa = a[1]+a[2]+a[3]+a[4]
local sb = b[1]+b[2]+b[3]+b[4]
local sc = c[1]+c[2]+c[3]+c[4]
local sign = (sa>=0) and 1 or -1
if (sign*sb>0) and (sign*sc>0) and (sb+sc)<(sign*sa) then
return true
else
return false
end
end

Attachments

2021-04-08T15_29_52-point-in-triangle-lua.png (8.29 KiB) Viewed 2361 times

Hello, I haven't tested your code, but I think there are a lot simpler and more efficient ways to test if a point is inside a triangle: https://2dengine.com/?p=intersections#P ... e_triangle
The issues with your code are:
1. The intermediate a,b,c tables. That's very inefficient
2. No "early bailout". What I mean is that you can avoid a number of calculations by returning ASAP:

function pointInTriangle(px, py, x1, y1, x2, y2, x3, y3)
local ax, ay = x1 - px, y1 - py
local bx, by = x2 - px, y2 - py
local cx, cy = x3 - px, y3 - py
local sab = ax*by - ay*bx < 0
if sab ~= (bx*cy - by*cx < 0) then
return false
end
return sab == (cx*ay - cy*ax < 0)
end

If I remember correctly from Ericson's book, it may be possible to do even further performance optimizations - but you have to know the triangle's winding/orientation in advance.