pad.mattdiesel.co.uk

Snippet - Draw circle opengl

Draw circle opengl (C++)

Draws a circle (no antialiasing)
Created 2014-07-02 14:04:19.880020 by Matt.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/// Draws a circle
/// Starting with an initial vector [0 r]
/// Apply matrix transformation [cos(t) -sin(t); sin(t) cos(t)]
/// Each iteration simplifies to [x*cos(t)-y*sin(t); x*sin(t)+y*cos(t)]
void glutCircle(float cx, float cy, float r, int segments) 
{
    int i;
    float theta = 2.0f * M_PI / (float)segments;
    float cost = cos(theta);
    float sint = sin(theta);

    float vx = -r;
    float vy = 0.0f;

    float v2x;
    
    glBegin(GL_LINE_LOOP);
    for(i = 0; i < segments; i++)
    {
        glVertex2f(cx + vx, cy + vy);

        v2x = vx*cost - vy*sint;
        vy = vx*sint + vy*cost;

        vx = v2x;
    }
    glEnd();

    return;
}