Search code examples
c++openglmathprogramming-languagesopengl-3

Draw Square With Polar Coordinates in OpenGL


How can I draw a square with polar coordinates in OpenGL?

I know these Equations:

 x = r * cos ( theta ) 
 y = r * sin ( theta )
 r = n * cos ( theta )

My code:

float baseX = width / 2.f; 
float baseY = height / 2.f
int n = 7;
glBegin(GL_POINTS);{
    for (float tempAngle = 0.0 ; tempAngle <= PI/4 ; tempAngle++) {
        radius = n * cos(PI/4);
        x = baseX + radius * cos(tempAngle);
        y = baseY + radius * sin(tempAngle);
        glVertex2f(x, y);
    }
}glEnd();

Solution

  • I found the solution.

    int n = 70;
    int i = 1;
    glBegin(GL_TRIANGLE_FAN);{
        for (float tempAngle = 0.0 ; tempAngle <= 2 * PI ; tempAngle += PI/8) {
            radius = n * cos(tempAngle);
            x = baseX + radius * cos(tempAngle);
            y = baseY + radius * sin(tempAngle);
            if (i % 2 == 0)
                glVertex2f(x, y);
            i++;
        }
    }glEnd();