cocos2dx-lua判断点是否在凸多边形内的方法

cocos2dx-lua判断点是否在凸多边形内的方法

--每两个边,加一个辅助边围成一个三角形,如果在某个三角形内存在,则必定在这个多边形内

local PengZhuangLayer = class("PengZhuangLayer",cc.Layer)

PengZhuangLayer.vecPoint = nil
function PengZhuangLayer:ctor()
    self:initLayer()
	self:onNodeEvent("enter",handler(self,self.onEnterEvent))
end
function PengZhuangLayer:onEnterEvent()
	self:setTouchEnabled(true)
	local touchBeginPoint = nil
	local function onTouchBegan(touch, event)
        local location = touch:getLocation()
        printf("onTouchBegan: %0.2f, %0.2f", location.x, location.y)
        touchBeginPoint = {x = location.x, y = location.y}
        return true
    end

    local function onTouchMoved(touch, event)
        local location = touch:getLocation()
        printf("onTouchMoved: %0.2f, %0.2f", location.x, location.y)
    end

	local function onTouchEnded(touch, event)
        local location = touch:getLocation()
        printf("onTouchEnded: %0.2f, %0.2f", location.x, location.y)
		if self:isContain(location) then
            printf("is in here")
        end
    end

	local listener = cc.EventListenerTouchOneByOne:create()
	listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN )
    listener:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED )
    listener:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED )

	local eventDispatcher = self:getEventDispatcher()
    eventDispatcher:addEventListenerWithSceneGraphPriority(listener, self)
end
--判断是否在凸多边形内的方式:
--每两个边,加一个辅助边围成一个三角形,如果在某个三角形内存在,则必定在这个多边形内
function PengZhuangLayer:initLayer()
	local vecPoint = {}
	vecPoint[1]=cc.p(100,30)
	vecPoint[2]=cc.p(50,70)
	vecPoint[3]=cc.p(80,130)
	vecPoint[4]=cc.p(150,130)
	vecPoint[5]=cc.p(200,130)
	vecPoint[6]=cc.p(150,30)
	self.vecPoint = vecPoint
    self:drawShape(vecPoint)
end
---画一个多边形
function PengZhuangLayer:drawShape(vecTable)
	local drawNode = cc.DrawNode:create()
    self:addChild(drawNode)
    local color = cc.c4f(1.0, 0.0, 0.0, 1.0)
	local size = #vecTable
	for i=1,size do
		if i == size then
			drawNode:drawSegment(vecTable[i],vecTable[1],1,color)
		else
			drawNode:drawSegment(vecTable[i],vecTable[i+1],1,color)
		end
	end
end
--判断点是否在多边形里
function PengZhuangLayer:isContain(point)
	local bRet = true;
    local size = #self.vecPoint
	for i=1,size do
        if i == size then
            if not self:isMeetLine(self.vecPoint[i],self.vecPoint[1],point,self.vecPoint[2]) then
                bRet = false;
                break;
            end
        elseif i == size - 1 then
			if not self:isMeetLine(self.vecPoint[i],self.vecPoint[i+1],point,self.vecPoint[1]) then
                bRet = false;
                break;
            end
        else
            if not self:isMeetLine(self.vecPoint[i],self.vecPoint[i+1],point,self.vecPoint[i+2]) then
                bRet = false;
                break;
            end
        end
    end
    return bRet;
end
--判断点是否在三角形内
function PengZhuangLayer:isMeetLine(point1,point2,point,z)
	local bRet = false;
    if (point1.x == point2.x) and (point1.y ~= point2.y) then
        if (z.x >point1.x and point.x > point1.x) or
		(z.x < point1.x and point.x < point1.x) or (z.x == point1.x) then
            bRet = true;
        end
        return bRet;
    end
    if (point1.y == point2.y and point1.x ~= point2.x) then
        if ((z.y > point1.y and point.y > point1.y) or
		(z.y < point1.y and point.y < point1.y) or z.y == point1.y) then
            bRet = true;
        end
        return bRet;
    end
    if ((self:isLine(point1,point2,z) >= 0 and self:isLine(point1,point2,point) >= 0) or
	(self:isLine(point1,point2,z) < 0 and self:isLine(point1,point2,point) < 0)) then
        bRet = true;
    else
        bRet = false;
    end
    return bRet;
end
--判断点是否在直线内
function PengZhuangLayer:isLine(point1,point2,z)
	local number1 = point2.x - point1.x;
    local number2 = z.y - point1.y;
    local number3 = point2.y - point1.y;
    local number4 = z.x - point1.x;
    local number5 = number1 * number2 - number3 * number4;
    return number5;
end
return PengZhuangLayer

转载于:https://my.oschina.net/djy2130/blog/867743

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值