Click here to Skip to main content
15,885,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Draw an arc using x,y position, Radius, Start Angle and End Angle in OpenGL

x (center x position of the circle)
y (center y position of the circle)
r (Radius)
Start_Angle (The angle (referenced from the center of the circle) that defines the start
position of the arc.)
End_Angle (The angle (referenced from the center of the circle) that defines the end
position of the arc.)

What I have tried:

I have tried this...
C#
void DrawArc(float cx, float cy, float r, float start_angle, float arc_angle, int num_segments) 
{ 
   float theta = arc_angle / float(num_segments - 1);//theta is now calculated from the arc angle instead, the - 1 bit comes from the fact that the arc is open

   float tangetial_factor = tanf(theta);

   float radial_factor = cosf(theta);


   float x = r * cosf(start_angle);//we now start at the start angle
   float y = r * sinf(start_angle); 

   glBegin(GL_LINE_STRIP);//since the arc is not a closed curve, this is a strip now
   for(int ii = 0; ii < num_segments; ii++)
   { 
      glVertex2f(x + cx, y + cy);

      float tx = -y; 
      float ty = x; 

      x += tx * tangetial_factor; 
      y += ty * tangetial_factor; 

      x *= radial_factor; 
      y *= radial_factor; 
   } 
   glEnd(); 
}

but its not satisfying my requirement.
Posted
Updated 20-May-16 6:57am
v2
Comments
Herman<T>.Instance 18-May-16 5:23am    
but its not satisfying my requirement. ?????
phil.o 18-May-16 7:11am    
Please describe in what the result you get is not satisfying.
Patrice T 18-May-16 20:19pm    
define 'your requirement'

 
Share this answer
 
GL.Begin(PrimitiveType.LineStrip);
double x = 2.047;
double y = 3.5;
double r = .5;
double start_angle = 1.5;
double end_angle = 3.2;
double max_angle = 2 * Math.PI;
double angle_increment = Math.PI / 1000;
for (double theta = start_angle; theta < end_angle; theta += angle_increment)
{

x = r * Math.Cos (theta);
y = r * Math.Sin (theta);
GL.Vertex2(x, y);
}
GL.End();

*******************************************************************************
Could draw the arc, but became oval in shape. please help to draw a proper arc.
 
Share this answer
 
Your solution don't respect your requirements.
Quote:

x (center x position of the circle)
y (center y position of the circle)

You need to make changes like that:
C#
GL.Begin(PrimitiveType.LineStrip);
    double x = 2.047;
    double y = 3.5;
    double r = .5;
    double start_angle = 1.5;
    double end_angle = 3.2;
    double max_angle = 2 * Math.PI;
    double angle_increment = Math.PI / 1000;
    for (double theta = start_angle; theta < end_angle; theta += angle_increment)
    {

        //x = r * Math.Cos (theta);
        //y = r * Math.Sin (theta);
        GL.Vertex2(x+  r * Math.Cos (theta), y+ r * Math.Sin (theta));
    }
    GL.End();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900