cocos2dx-lua判斷點是否在凸多邊形內的方法
每兩個邊,加一個輔助邊圍成一個三角形,若是在某個三角形內存在,則一定在這個多邊形內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