cocos2dx&lua绘制圆角矩形

原文地址:

c++: http://www.cnblogs.com/leehongee/p/3639623.html

lua: http://www.tuicool.com/articles/FrQRJjM

代码:

 ----c++: 

/*
* @brief        画圆角矩形    
* @param        origin            矩形开始点
* @param        destination        矩形结束点
* @param        radius            圆角半径
* @param        segments        圆角等份数,等份越多,圆角越平滑
* @param        bFill            是否填充
* @param        color            填充颜色
* @attention        
*/
void DrawPrimitivesTest::ccDrawRoundRect( Point origin, Point destination, float radius, unsigned int segments, bool bFill, Color4F color)
{
        //算出1/4圆
    
    const float coef    = 0.5f * (float)M_PI / segments;
    Point * vertices    = new Point[segments + 1];
    Point * thisVertices = vertices;
    for(unsigned int i = 0; i <= segments; ++i, ++thisVertices)
    {
        float rads        = (segments - i)*coef;
        thisVertices->x    = (int)(radius * sinf(rads));
        thisVertices->y    = (int)(radius * cosf(rads));
    }
    //
    Point tagCenter;
    float minX    = MIN(origin.x, destination.x);
    float maxX    = MAX(origin.x, destination.x);
    float minY    = MIN(origin.y, destination.y);
    float maxY    = MAX(origin.y, destination.y);
    
    unsigned int dwPolygonPtMax = (segments + 1) * 4;
    Point * pPolygonPtArr = new Point[dwPolygonPtMax];
    Point * thisPolygonPt = pPolygonPtArr;
    int aa = 0;
    //左上角
    tagCenter.x        = minX + radius;
    tagCenter.y        = maxY - radius;
    thisVertices    = vertices;
    for(unsigned int i = 0; i <= segments; ++i, ++thisPolygonPt, ++thisVertices)
    {
        thisPolygonPt->x    = tagCenter.x - thisVertices->x;
        thisPolygonPt->y    = tagCenter.y + thisVertices->y;
        ++aa;
    }
    //右上角
    tagCenter.x        = maxX - radius;
    tagCenter.y        = maxY - radius;
    thisVertices    = vertices + segments;
    for(unsigned int i = 0; i <= segments; ++i, ++thisPolygonPt, --thisVertices)
    {
        thisPolygonPt->x    = tagCenter.x + thisVertices->x;
        thisPolygonPt->y    = tagCenter.y + thisVertices->y;
        ++aa;
    }
    //右下角
    tagCenter.x        = maxX - radius;
    tagCenter.y        = minY + radius;
    thisVertices    = vertices;
    for(unsigned int i = 0; i <= segments; ++i, ++thisPolygonPt, ++thisVertices)
    {
        thisPolygonPt->x    = tagCenter.x + thisVertices->x;
        thisPolygonPt->y    = tagCenter.y - thisVertices->y;
        ++aa;
    }
    //左下角
    tagCenter.x        = minX + radius;
    tagCenter.y        = minY + radius;
    thisVertices    = vertices + segments;
    for(unsigned int i = 0; i <= segments; ++i, ++thisPolygonPt, --thisVertices)
    {
        thisPolygonPt->x    = tagCenter.x - thisVertices->x;
        thisPolygonPt->y    = tagCenter.y - thisVertices->y;
        ++aa;
    }
    
    if(bFill){
        DrawPrimitives::drawSolidPoly(pPolygonPtArr, dwPolygonPtMax, color);
    }else
    {
        DrawPrimitives::setDrawColor4F(color.r, color.g, color.b, color.a);
        DrawPrimitives::drawPoly(pPolygonPtArr, dwPolygonPtMax, true);
    }
    
    CC_SAFE_DELETE_ARRAY(vertices);
    CC_SAFE_DELETE_ARRAY(pPolygonPtArr);
}

 ----lu a: 

-- 传入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) * chef  

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值