cocos2d-lua如何绘制圆角矩形？

cocos2d-lua如何绘制圆角矩形？

Nov 15, 2023
1 赞

```-- 画圆角矩形的另一种实现方式
-- 使用多段线的方式模拟画圆角矩形
function zhuanfou_create_solid_round_rect(node, rx, ry, rwidth, rheight, radius, fillColor)
-- pointsCount表示圆角的精细度，值越大越精细
-- pointsCount是段数，也就是cocos2d-lua的api中的segments参数
-- 即使是画圆，也是有该参数的
ry = ry + rheight
local color = cc.c4f(0,0,0,0) -- border-color设置为透明
local borderWidth = 0
local pointsCount = 100
local origin      = cc.p(rx, ry)
local destination = cc.p(rx + rwidth, ry - rheight)
local points      = {}
-- 算出1/4圆
local coef     = math.pi / 2 / pointsCount
local vertices = {}
for i=0, pointsCount do
local rads = (pointsCount - i) * coef
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 = (pointsCount + 1) * 4
local pPolygonPtArr  = {}
-- 左上角
for i=0, pointsCount 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
-- 右上角
for i=0, pointsCount 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
-- 右下角
for i=0, pointsCount 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
-- 左下角
for i=0, pointsCount 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
node:drawPolygon(pPolygonPtArr, #pPolygonPtArr, fillColor, borderWidth, color)
end```

jerkzhang

Apr 16, 2023
1 赞

```-- 返回画圆角矩形的drawNode对象
function drawRoundRect(rx, ry, rwidth, rheight, borderWidth, radius, color, fillColor)
-- pointsCount表示圆角的精细度，值越大越精细
-- pointsCount是段数，也就是cocos2d-lua的api中的segments参数
-- 即使是画圆，也是有该参数的
local pointsCount    = 100
local origin      = cc.p(rx, ry)
local destination = cc.p(rx + rwidth, ry - rheight)
local points      = {}
-- 算出1/4圆
local coef     = math.pi / 2 / pointsCount
local vertices = {}
for i=0, pointsCount do
local rads = (pointsCount - i) * coef
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 = (pointsCount + 1) * 4
local pPolygonPtArr  = {}
-- 左上角
for i=0, pointsCount 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
-- 右上角
for i=0, pointsCount 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
-- 右下角
for i=0, pointsCount 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
-- 左下角
for i=0, pointsCount 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
local drawNode = cc.DrawNode:create()
drawNode:drawPolygon(pPolygonPtArr, #pPolygonPtArr, fillColor, borderWidth, color)
return drawNode
endda```

```function zhuanfou_create_solid_round_rect(node, x, y, w, h, radius, color)
local angle = math.pi / 2
local segments = 100