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)
{
glBegin(GL_LINE_STRIP);
for(int ii = 0; ii < num_segments; ii++)
{
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
}
glEnd();
}
I used LINE_STRIP for Concert the code to Drawing an arc but didn’t work.
Does anyone can help me?
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_lengthparameter to the function signature. Replace2.0f * 3.1415926fwitharc_length. If you need to start the arc at a given offset, you can add another parameter calledfloat arc_start. Then addarc_starttothetain each iteration of yourforloop.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:
http://devmag.org.za/2011/04/05/bzier-curves-a-tutorial/
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 withthetain your original circle code. For each iteration, draw a point.