Search code examples
c++opengldraw

OpenGL draw spiral


The following code will create a new circle on openGL but how can I make it draw spiral instead of the circle?


    glBegin(GL_POINTS);
    for (float angle = 0; angle < 360; angle += 1)
    {
        x = 50 * cos(angle);
        y = 50 * sin(angle);
        glVertex2f(x, y);
    }
    glEnd();

Solution

  • You need to increase the radius in the loop. e.g.:

    float radius = 0.0f;
    
    glBegin(GL_POINTS);
    for (float angle = 0; angle < 1440; angle += 1)
    {
        x = cos(angle * M_PI / 180) * radius;
        y = sin(angle * M_PI / 180) * radius;
        radius += 0.1f;
        glVertex2f(x, y);
    }
    glEnd();
    

    Note the unit of the angle of sin and cos is Radians.