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, cc.p(x, 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, cc.p(x, 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, cc.p(x, 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, cc.p(x, y)) end if fillColor == nil then fillColor = cc.c4f(0, 0, 0, 0) end drawNode:drawPolygon(pPolygonPtArr, #pPolygonPtArr, fillColor, borderWidth, color) end

参考:链接描述

你可能感兴趣的:(cocos2d-x画圆角矩形的Lua实现)