Cocos2d-x绘制圆角矩形
来源:互联网 发布:ubuntu 14.10下载 编辑:程序博客网 时间:2024/06/06 06:49
原文:点击打开链接
/*
* @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);
}
/** * @brief 画圆角矩形 * @param origin 矩形开始点 * @param destination 矩形结束点 * @param radius 圆角半径 * @param segments 圆角等份数,等份越多,圆角越平滑 * @param bFill 是否填充 * @param color 填充颜色 * @attention */ccDrawRoundRect:function(bgSpr, origin, destination, radius, segments){ var pClip = new cc.ClippingNode(); // 设置是否反向,将决定画出来的圆是透明的还是黑色的 pClip.setInverted(false); pClip.setAnchorPoint(cc.p(0, 0)); pClip.setPosition(cc.p(-7, -7)); // 算出1/4圆 var coef = 0.5*Math.PI/segments; var vertices = new Array(segments+1); for(var i=0; i<= segments;i++){ var rads = (segments-i)*coef; vertices[i].x = parseInt(radius*Math.sin(rads)); vertices[i].y = parseInt(radius*Math.cos(rads)); } // var tagCenter; var minX = Math.min(origin.x, destination.x); var maxX = Math.min(origin.x, destination.x); var minY = Math.min(origin.y, destination.y); var maxY = Math.min(origin.y, destination.y); var polygonArr = new Array((segments+1)*4); var aa = 0; //左上角 tagCenter.x = minX + radius; tagCenter.y = maxY - radius; for(var i=0; i<=segments;i++){ polygonArr[i].x = tagCenter.x-vertices[i].x; polygonArr[i].y = tagCenter.y+vertices[i].y; aa = aa+1; } //右上角 tagCenter.x = maxX - radius; tagCenter.y = maxY - radius; var initVal = aa; for(var i=initVal; i<=(initVal+segments);i++){ polygonArr[i].x = tagCenter.x+vertices[segments-(i-initVal)].x; polygonArr[i].y = tagCenter.y+vertices[segments-(i-initVal)].y; aa = aa+1; } //右下角 tagCenter.x = maxX - radius; tagCenter.y = minY + radius; var initVal = aa; for(var i=initVal; i<=(initVal+segments);i++){ polygonArr[i].x = tagCenter.x+vertices[i-initVal].x; polygonArr[i].y = tagCenter.y-vertices[i-initVal].y; aa = aa+1; } //左下角 tagCenter.x = minX + radius; tagCenter.y = minY + radius; var initVal = aa; for(var i=initVal; i<=(initVal+segments);i++){ polygonArr[i].x = tagCenter.x-vertices[segments-(i-initVal)].x; polygonArr[i].y = tagCenter.y-vertices[segments-(i-initVal)].y; aa = aa+1; } var pStencil = new cc.DrawNode(); pStencil.drawPoly(polygonArr, (segments+1)*4, false, {r:1.0, g:0, b:0, a:1}); pStencil.setPosition(cc.p(0, 0)); pClip.setStencil(pStencil); pClip.addChild(bgSpr, 1); pClip.setContentSize(bgSpr.getContentSize()); return pClip;}
- Cocos2d-x绘制圆角矩形
- Cocos2d-x绘制圆角矩形
- Cocos2d-x--Box2D绘制出两个矩形框的解决方案
- cocos2d-x:矩形碰撞
- 【cocos2d-x 2.x 学习与应用总结】14: 自定义shader绘制混合颜色的矩形
- Ps绘制圆角矩形
- GDI+绘制圆角矩形
- MFC绘制圆角矩形
- Canvas绘制圆角矩形
- GDI+绘制圆角矩形
- android绘制圆角矩形
- C#绘制圆角矩形
- Cocos2d-x--绘制图片
- Cocos2d-x 图形绘制
- Cocos2d-x 绘制图形
- Cocos2d-x 图形绘制
- cocos2d-x 简单绘制
- cocos2d-x图形绘制
- 在Java中,小白对集合框架的认识
- [Drools]JAVA规则引擎 -- Drools
- android-view-windowmanagerbadtokenexception-unable-to-add-window
- Java中,对多线程访问同一变量(并发访问)的认识
- 创建表及设置属性的约束条件
- Cocos2d-x绘制圆角矩形
- JTabel 背景设置半透明 与某些区域的单元格设置背景色
- 简单数构
- 集训心得
- 从Android studio开始安卓之旅
- Nginx安装教程
- java--IO效率分析
- NSAssert用法
- 初探开发者模型——Decrator模型