微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

cocos2d-x画圆角矩形的Lua实现


-- 传入DrawNode对象,画圆角矩形 function drawNodeRoundRect(drawNode,rect,borderWidth,radius,color,fillColor) -- segments表示圆角的精细度,值越大越精细 local segments = 100 local origin = cc.p(rect.x,rect.y) local destination = cc.p(rect.x + rect.width,rect.y - rect.height) local points = {} -- 算出1/4圆 local coef = math.pi / 2 / segments local vertices = {} for i=0,segments do local rads = (segments - i) * coef local x = radius * math.sin(rads) local y = radius * math.cos(rads) table.insert(vertices,cc.p(x,y)) end local tagCenter = cc.p(0,0) local minX = math.min(origin.x,destination.x) local maxX = math.max(origin.x,destination.x) local minY = math.min(origin.y,destination.y) local maxY = math.max(origin.y,destination.y) local dwpolygonPtMax = (segments + 1) * 4 local ppolygonPtArr = {} -- 左上角 tagCenter.x = minX + radius; tagCenter.y = maxY - radius; for i=0,segments do local x = tagCenter.x - vertices[i + 1].x local y = tagCenter.y + vertices[i + 1].y table.insert(ppolygonPtArr,y)) end -- 右上角 tagCenter.x = maxX - radius; tagCenter.y = maxY - radius; for i=0,segments do local x = tagCenter.x + vertices[#vertices - i].x local y = tagCenter.y + vertices[#vertices - i].y table.insert(ppolygonPtArr,y)) end -- 右下角 tagCenter.x = maxX - radius; tagCenter.y = minY + radius; for i=0,segments do local x = tagCenter.x + vertices[i + 1].x local y = tagCenter.y - vertices[i + 1].y table.insert(ppolygonPtArr,y)) end -- 左下角 tagCenter.x = minX + radius; tagCenter.y = minY + radius; for i=0,segments do local x = tagCenter.x - vertices[#vertices - i].x local y = tagCenter.y - vertices[#vertices - i].y table.insert(ppolygonPtArr,y)) end if fillColor == nil then fillColor = cc.c4f(0,0) end drawNode:drawpolygon(ppolygonPtArr,#ppolygonPtArr,fillColor,color) end

参考:链接描述

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐