cocos2d-iphone画椭圆

cocos2d-iphone 画椭圆


添加到cocos2d下的CCDrawingPrimitives.h文件


void ccDrawEllipse(CGPoint center, float rx, float ry, float a, int segs, BOOL drawLineToCenter);




添加到cocos2d下的CCDrawingPrimitives.m文件


void ccDrawEllipse(CGPoint center,float rx, float ry,float a, int segs,BOOL drawLineToCenter)
{
    lazy_init();
    
   int additionalSegment = 1;
   if (drawLineToCenter)
        additionalSegment++;
    
   const float coef =2.0f * (float)M_PI / segs;
    
   GLfloat *vertices = calloc( sizeof(GLfloat)*2*(segs+2),1);
   if(!vertices)
       return;
    
   float rads, distance, angle, j, k;
   for(NSUInteger i =0; i <= segs; ++i)
    {
        rads = i * coef;
        distance =sqrt(pow(sinf(rads) * rx,2) + pow(cosf(rads) * ry,2));
        angle =atan2(sinf(rads) * rx,cosf(rads) * ry);
        j = distance *cosf(angle + a) + center.x;
        k = distance *sinf(angle + a) + center.y;
        
        vertices[i*2] = j;
        vertices[i*2+1] = k;
    }
    vertices[(segs+1)*2] = center.x;
    vertices[(segs+1)*2+1] = center.y;

    [shader_use];
[shader_setUniformsForBuiltins];
[shader_setUniformLocation:colorLocation_with4fv:(GLfloat*) &color_.rcount:1];
    
ccGLEnableVertexAttribs(kCCVertexAttribFlag_Position );
    
glVertexAttribPointer(kCCVertexAttrib_Position,2, GL_FLOAT,GL_FALSE, 0, vertices);
glDrawArrays(GL_LINE_STRIP,0, (GLsizei) segs+additionalSegment);
    
free( vertices );
CC_INCREMENT_GL_DRAWS(1);
}


你可能感兴趣的:(cocos2d-iphone画椭圆)