Take the 2-minute tour ×

I need a function to draw an arc in opengl. I also need a sample code for using it.

i can draw a circle with Lines,i want to draw arc with Line too. This is My Function for drawing circule :

void DrawCircle(float cx, float cy, float r, int num_segments) 
        float theta = 2.0f * 3.1415926f * float(ii) / float(num_segments);//get the current angle 

        float x = r * cosf(theta);//calculate the x component 
        float y = r * sinf(theta);//calculate the y component 

        glVertex2f(x + cx, y + cy);//output vertex 

I used LINE_STRIP for Concert the code to Drawing an arc but didn't work. Does anyone can help me?

share|improve this question
This is not a give me codz plz website. You need to be more specific. Show what you have tried. What is the code you have written so far? Which part of the code is problematic? Is it the OpenGL API you are having problem with, or the algorithm? Do you have a problem with the math, or implementing it as a program? –  Shahbaz Nov 12 '12 at 17:36
i want to create arc with Line in openGL. –  Saman Gholami Nov 12 '12 at 17:40
Fastest solution: Let the for go to num_segments and divide the angle by float(something_bigger) instead of float(num_segments). But generally there are much better solutions. –  Shahbaz Nov 12 '12 at 17:46
Seems you're using an ancient OpenGL version, please specify which version –  Kos Nov 12 '12 at 17:49
Yes,you right,please make your comment to an answer that i can choose it for best answer my friend.The angle of arc is constant now...how i can make it variable ? –  Saman Gholami Nov 12 '12 at 17:54
add comment

2 Answers

up vote 1 down vote accepted

Not necessarily the best way, but if you just need to get something working, you can modify your circle code slightly. Add a float arc_length parameter to the function signature. Replace 2.0f * 3.1415926f with arc_length. If you need to start the arc at a given offset, you can add another parameter called float arc_start. Then add arc_start to theta in each iteration of your for loop.

Edit based on Saman's comments:

What you actually want is not an arc, but a more general representation of a curve. An arc is a kind of curve, but it's a very particular kind--i.e. one with a constant radius. It sounds like you want to draw arbitrary curves, potentially with varying radii. If so, then my recommendation is Bezier curves. Here is a pretty solid introduction:


Note the part later in the tutorial about drawing them, where the author says "the simplest approach is to use small increments of t to calculate successive points." This is pretty much what you have to do in order to draw a Bezier curve in OpenGL. Pick a value of t, and increment it in a for loop, just like you did with theta in your original circle code. For each iteration, draw a point.

share|improve this answer
Thank you for writing Jarrett.your solution is very interesting but how i can set the arc angle? –  Saman Gholami Nov 12 '12 at 20:33
The arc_length parameter I suggested is for that very thing. –  Jarrett Nov 12 '12 at 22:32
When i change the arc_length ,arc draws like an Incomplete circle but i want to set the angle that causes the arc not like circle anymore –  Saman Gholami Nov 12 '12 at 23:19
"Arc draws like an incomplete circle"--That is the definition of an arc. Perhaps you're thinking of a different geometric shape? –  Jarrett Nov 13 '12 at 5:01
Yes,something like Curve but implementing with GL_LINE –  Saman Gholami Nov 13 '12 at 14:22
show 3 more comments

You have different solutions, based on how you draw your circle. Currently, what you are doing is to divide 2*pi into n segments and connect them together. Well, the first solution that comes to mind, is to change the range of the arc.

For example, to make the arc only 3*PI/4, you can simply change your

float theta = 2.0f * PI * float(ii) / float(num_segments);


float theta = 3.0f * PI / 4.0f * float(ii) / float(num_segments);

Or more generally, if you consider the full circle to correspond to 1 and nothing to correspond to 0, you can make an arc which is s times the circle (for example 3/8), then you can simply write:

float theta = 2.0f * PI * s * float(ii) / float(num_segments);

Note that, you may not want the arc to start at angle 0. Therefore, you can add a constant, which is the starting angle to this formula too:

float theta = 2.0f * PI * s * float(ii) / float(num_segments) + start;

Another solution would be to instead of computing angles within the arc, you get the start and end angles, and a step size. Then you have:

for (theta = start; theta < end; theta += step)

and make sure that in the end of your GL_LINE_STRIP you do a glVertex2f of theta corresponding to end angle to finish the arc correctly.

From your comments to the other answer, it seems possible that you are in fact looking for other curves such as bezier curves or splines or others.

share|improve this answer
Thank you for writing Shahbaz.i think i need to implement Spline but i limited to use Line and Line_Loop ... –  Saman Gholami Nov 13 '12 at 21:30
@SamanGholami, splines are also drawn with GL_LINE_STRIP. It is the computation of the vertices that is important. You would need to look for spline rendering algorithms, preferably from a book on computer graphics. –  Shahbaz Nov 14 '12 at 0:00
Yes i can use GL_LINE_STRIP,could you help me to find a reference for it? –  Saman Gholami Nov 14 '12 at 7:58
How about a google search for "opengl spline drawing algorithm"? The third link for example are slides from carnegie melon. –  Shahbaz Nov 14 '12 at 13:01
Thank you Shahbaz.this reference used glVertex3 but i limited to use glVertex2.i should create a project for university and i should draw a 2d cartoon shape like Garfield and we are limited to use glVertex2.i try to produce some functions and using them for shapes more and more.what's your idea? –  Saman Gholami Nov 14 '12 at 13:52
show 1 more comment

Your Answer


