Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: How does drawing circle work?

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2015
    Posts
    18

    How does drawing circle work?

    Code :
    void drawCircle(float cx, float cy, float r, float num_segments){
    	glBegin(GL_LINE_LOOP);
     
    	for(int i = 0; i < num_segments; i++){
    		float theta = 2.0f * PI * i / num_segments;
     
    		float x = r * cosf(theta);
    		float y = r * sinf(theta);
     
    		glVertex2f(x + cx, y + cy);
    	}
     
    	glEnd();
    }


    This is newbie question but how does above code work? why is this function able to draw a circle?

    Where can I learn computer graphical maths such as above? any recommended website/videos?

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,402
    Quote Originally Posted by yj1214 View Post
    This is newbie question but how does above code work? why is this function able to draw a circle?

    Code :
    		float x = r * cosf(theta);
    		float y = r * sinf(theta);
    The above converts polar coordinates (radius and angle) to rectangular (Cartesian) coordinates (X and Y). Thus, the loop generates a sequence of points with a fixed radius r and increasing angles (theta is proportional to the loop counter, i).

    This follows from the definition of sine and cosine, which should be explained in any introductory text on trigonometry.

    Quote Originally Posted by yj1214 View Post
    Where can I learn computer graphical maths such as above?
    Most texts on the mathematics related to computer graphics would probably assume the above as prior knowledge.

    The main areas of mathematics involved in computer graphics are algebra and linear algebra, with differential calculus being useful for some more advanced topics (e.g. determining tangents and normals to curves and curved surfaces).

    About the only trigonometry that's required for computer graphics is conversion between polar and rectangular coordinates. Conversion in one direction is given above, the inverse (conversion from rectangular to polar coordinates) is:
    Code :
        double r = sqrt(x*x + y*y);    // Pythagoras' theorem
        double theta = atan2(y, x);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •