Cocos2d-x繪制圓角矩形


/*
* @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);
}        

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM