opengl draw semi circle c++

#include <GL/glut.h>
#include <cmath>

void drawSemiCircle(float cx, float cy, float r, int numSegments) {
    glBegin(GL_LINE_STRIP);
    for (int i = 0; i <= numSegments; i++) {
        float theta = static_cast<float>(i) / static_cast<float>(numSegments) * (M_PI / 2.0f);
        float x = r * cos(theta) + cx;
        float y = r * sin(theta) + cy;
        glVertex2f(x, y);
    }
    glEnd();
}

void display() {
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0f, 1.0f, 1.0f);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, 800, 0, 600);
    drawSemiCircle(400, 300, 200, 100);
    glFlush();
}

int main(int argc, char argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(800, 600);
    glutCreateWindow("Semi Circle");
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}

This C++ code uses OpenGL to draw a semi-circle. It includes the necessary header files and defines a function drawSemiCircle to draw a semi-circle given the center coordinates (cx, cy), radius r, and the number of line segments to approximate the semi-circle.

The drawSemiCircle function uses glBegin and glEnd to specify the drawing mode and loops through a specified number of segments to calculate the points along the semi-circle using trigonometry (cosine and sine functions).

The display function sets up the OpenGL environment, clears the color buffer, specifies the color, sets the projection matrix, and calls drawSemiCircle with the desired parameters.

The main function initializes the OpenGL environment, creates a window, sets the display function, and enters the main event processing loop using glutMainLoop.