若触摸点在三角形三条边的同一侧,则说明此点在三角形内。
判断点在直线的哪一侧,通过直线上的两点计算出直线方程 a*x + b*y + c = 0 的三个参数a,b,c,然后再计算d = a*x + b*y + c,
d<0表示在直线左侧,d=0表示在直线上,d>0表示在直线右侧。
function GenieMenuView:checkTouchInTriangle(point, parent_node, node_table)
if #node_table ~= 3 then
return false
end
local node_pos_table = {}
for i, node in ipairs(node_table) do
node_pos_table[i] = cc.p(node:getPositionX() + parent_node:getPositionX(), node:getPositionY() + parent_node:getPositionY())
end
local d1 = self:evaluatePointToLine(point, node_pos_table[1], node_pos_table[2])
local d2 = self:evaluatePointToLine(point, node_pos_table[2], node_pos_table[3])
local d3 = self:evaluatePointToLine(point, node_pos_table[3], node_pos_table[1])
return (d1 * d2 > 0) and (d2 * d3 > 0)
end
function GenieMenuView:evaluatePointToLine(point, node1, node2)
local a = node2.y - node1.y
local b = node1.x - node2.x
local c = node2.x * node1.y - node1.x * node2.y
return a * point.x + b * point.y + c
end