Point in triangle
Posted: Thu Apr 08, 2021 1:31 pm
Hi All!
I've made a small program how to get the information if the point is in the triangle.
The main function:
I've made a small program how to get the information if the point is in the triangle.
The main function:
Code: Select all
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