Cocos2dx-lua rounded rectangle fillet drawNode

The official category used is: drawNode

The function is drawNode:drawPolygon ().

C++The parameters of the function are as follows:

//Draw a polygon, verts is the point set, count is the number of points, fill color is the fill color, borderWidth is the edge line width, and borderColor is the edge line color.

void drawPolygon(Vec2 *verts, int count, const Color4F &fillColor, float borderWidth, const Color4F &borderColor);

luaUsing table as a point set

 

Code such asBelow:

function drawNodeRoundRect(drawNode, rect, borderWidth, radius, color, fillColor)
        -- segmentsThe finer the fillet is, the bigger the value is, the more refined it is.
        local segments = 100
        --local segments = 4
        local origin = cc.p(rect.x, rect.y)
        local destination = cc.p(rect.x + rect.width, rect.y - rect.height)
        local points = { }

        -- Calculate the 1/4 circle
        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 = { }

        -- Top left corner
        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

        -- Upper right corner
        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

        -- Lower right corner
        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

        -- Lower left quarter
        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(1, 1, 1, 1)
        end

        drawNode:drawPolygon(pPolygonPtArr, #pPolygonPtArr, fillColor, borderWidth, color)
    end


    --Create DrawNode
local imagePath = "res/test.png" head = cc.DrawNode:createWithFilename(imagePath) head:setAnchorPoint(cc.p(0.5, 0.5)) head:setPosition(cc.p(156+700-190, 134+170+120)) scene:addChild(head,9999) --configuration parameter local TestRect = {} TestRect.x = 0 TestRect.width = 535 TestRect.y=0 TestRect.height = 235 local TestBorderWidth = 0 local TestColor = cc.c4f(1,1,1,1) local Testradius = 20 head:clear() drawNodeRoundRect(head,TestRect,TestBorderWidth,Testradius,TestColor,TestColor)

 

Leave a Reply

Your email address will not be published. Required fields are marked *