cocos2dx-lua检测触摸点是否在三角形内

若触摸点在三角形三条边的同一侧,则说明此点在三角形内。
判断点在直线的哪一侧,通过直线上的两点计算出直线方程 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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值