const point = (x, y) => ({ x, y });
const A = point(-1, 1);
const B = point(1, 1);
const C = point(1, -1);
const D = point(-1, -1);
const E = point(0, 0);
const side = (a, b, p) => Math.sign((b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x));
const inArea = side(A, B, E) === -1 &&
side(B, C, E) === -1 &&
side(C, D, E) === -1 &&
side(D, A, E) === -1;
console.log(inArea); // true
На Lua
function point(x, y)
return { ["x"] = x, ["y"] = y }
end
function sign(number)
if (number < 0) then
return -1
elseif (number > 0) then
return 1
else
return number
end
end
function side(a, b, p)
return sign((b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x))
end
A = point(-1, 1)
B = point(1, 1)
C = point(1, -1)
D = point(-1, -1)
E = point(0, 0)
inArea = side(A, B, E) == -1 and
side(B, C, E) == -1 and
side(C, D, E) == -1 and
side(D, A, E) == -1;
print(inArea) -- true