--检测一个点是否在一个6边形了内
function g_isContain(touchPos,itemPos,nRad)
local function 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
local function 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 ((isLine(point1,point2,z) >= 0 and isLine(point1,point2,point) >= 0) or
(isLine(point1,point2,z) < 0 and isLine(point1,point2,point) < 0)) then
bRet = true;
else
bRet = false;
end
return bRet;
end
local points = {}
local pts = {}
local rad = nRad or 40
local sx = itemPos.x
local sy = itemPos.y
local sAngle = 30
for i=1,6 do
sAngle = sAngle%360
local ag = sAngle/180*math.pi
local x = sx+rad * math.sin(ag)
local y = sy+rad * math.cos(ag)
local ps = {}
ps.x = x
ps.y = y
table.insert(points,ps)
table.insert(pts,{x,y})
sAngle = sAngle + 60
end
local vecPoint = points
local point = touchPos
local bRet = true;
local size = #vecPoint
for i=1,size do
if i == size then
if not isMeetLine(vecPoint[i],vecPoint[1],point,vecPoint[2]) then
bRet = false
break
end
elseif i == size - 1 then
if not isMeetLine(vecPoint[i],vecPoint[i+1],point,vecPoint[1]) then
bRet = false
break
end
else
if not isMeetLine(vecPoint[i],vecPoint[i+1],point,vecPoint[i+2]) then
bRet = false
break
end
end
end
return bRet
end
--检测是否点击在一个6边形内
function g_isCheckItem(sender,nRad)
local touchPos = sender:getTouchBeganPosition()
local itemPos = sender:getParent():convertToWorldSpace(cc.p(sender:getPosition()))
return g_isContain(touchPos,itemPos,nRad)
end
老卢写的 判断点在六边形内
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 这个由3名大二学生创办的健康餐厅成立于2007年,说起创办的原因其实也非常简单,就是他们一直没有在学校周围找到一家...
- 找到国内谷歌服务器IP地址:进入http://ping.chinaz.com/ 输入g.cn 然后查询出最快的节点...
- 2017年7月27日,早上8点起床,吃过早餐,9点15分坐上汽车,出发去快艇码头,乘快艇前往红树林。快艇像...