How to draw a circle by CCDrawNode

how to draw a circle by CCDrawNode?

Hello

I believe the easiest way to do this considering you have drawSegment and drawDot functions in DrawNode is to simply use circle parametric equation. Something like this for a filled circle:

DrawNode *circle = DrawNode::create();

for (float angle = 0; angle <= 2 * M_PI; angle += 0.01)
{
      circle->drawSegment(Point(0.0, 0.0), Point(radius * cos(angle), radius * sin(angle)), 1, Color4F(1.0, 0.0, 0.0, 1.0));
}

Replace drawSegment call with a drawDot call and you will get unfilled circle.

That is quite dumb solution, but works for me so far. More clever solution would be to use Midpoint circle algorithm, I think. http://en.wikipedia.org/wiki/Midpoint_circle_algorithm

Hi,
I know this post is old but this is how circles can be drawn using polygons with CCDrawNode.

std::vector vertex;
float precision = .05f;
float cir = 2 * M\_PI;
for (float a = .0f; a < cir; a += precision) {
   float x = mCenter.x + mRadius * cos(a);
   float y = mCenter.y + mRadius * sin(a);
   vertex.push_back(ccp(x,y));
}
draw->drawPolygon(&vertex[0], vertex.size(), ccc4f(0,0,0,0), 2, ccc4f(1,1,1,1));

Hope it helps.
Laurent

Before I can draw something on the screen, may I know how to override the draw function? I’m using version 3.2.

I am no Cocos2d-x expert since I haven’t used it since 3.0-beta. I think the correct way of overriding draw function is shown in cpp-tests/Classes/DrawPrimitives/.

But if I remember correctly, when drawing circles and segments, I was simply adding DrawNode objects to my layer object which was added to the scene.