Below is an example C++ program that uses OpenGL with GLUT to draw a circle using the GL_POINTS primitive and the midpoint (Bresenham) circle drawing algorithm. In this example, the algorithm computes points only in one octant and then uses the symmetry of the circle to plot equivalent points in the other seven octants.
Complete Code
cpp
#include <GL/glut.h>
// Window dimensions
const int windowWidth = 400;
const int windowHeight = 400;
// Circle parameters
const int centerX = 200; // x-coordinate of circle center
const int centerY = 200; // y-coordinate of circle center
const int radius = 100; // Circle radius
// Function to plot the eight symmetric points of the circle.
void plot8Points(int cx, int cy, int x, int y) {
glVertex2i(cx + x, cy + y);
glVertex2i(cx - x, cy + y);
glVertex2i(cx + x, cy - y);
glVertex2i(cx - x, cy - y);
glVertex2i(cx + y, cy + x);
glVertex2i(cx - y, cy + x);
glVertex2i(cx + y, cy - x);
glVertex2i(cx - y, cy - x);
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 1.0f, 1.0f); // Set drawing color to white
glBegin(GL_POINTS);
// Start at the top of the circle (0, radius)
int x = 0;
int y = radius;
// Decision parameter, initialized for the midpoint algorithm.
int d = 1 - radius;
// Plot the initial set of symmetric points
plot8Points(centerX, centerY, x, y);
// Generate circle points using the Midpoint Circle Algorithm
while (x < y) {
if (d < 0) {
// Midpoint is inside the circle
d += 2 * x + 3;
} else {
// Midpoint is outside or on the circle
d += 2 * (x - y) + 5;
y--;
}
x++;
// Plot all eight symmetric points for the current (x, y)
plot8Points(centerX, centerY, x, y);
}
glEnd();
glFlush();
}
void init() {
// Set the clear color to black
glClearColor(0.0, 0.0, 0.0, 1.0);
// Set up the view with an orthographic projection.
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, windowWidth, 0.0, windowHeight);
}
int main(int argc, char** argv) {
// Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(windowWidth, windowHeight);
glutInitWindowPosition(100, 100);
glutCreateWindow("Circle Drawing using GL_POINTS");
// Set up our display environment
init();
// Register the display callback function
glutDisplayFunc(display);
// Enter the GLUT main loop
glutMainLoop();
return 0;
}
Explanation
Setting Up the Window and Projection:
The init() function sets the background color to black.
We also set up an orthographic projection with gluOrtho2D. This creates a 2D coordinate system where both the x and y coordinates range from 0 to the window dimensions (400 in this case).
Midpoint Circle Drawing Algorithm:
The algorithm starts at the top of the circle with the initial point (0, radius).
The decision parameter d is initialized as 1 - radius. For each iteration, we decide whether to move horizontally or diagonally:
If d < 0, then the midpoint is inside the circle, so we update d using d += 2*x + 3.
If d >= 0, then the midpoint is outside or on the circle, and we update d using d += 2*(x - y) + 5 and decrement y.
The variable x is incremented each iteration until x >= y.
Plotting Points with Symmetry:
Due to the symmetry of a circle, it is enough to compute the circle points in one octant.
The helper function plot8Points uses the eightfold symmetry of a circle to plot 8 points (one from each octant) for every computed (x, y) pair.
These points are drawn using glVertex2i inside a glBegin(GL_POINTS)/glEnd() block.
Running the Application:
The main function initializes GLUT, sets the display mode, window size, and callback function, and finally enters the GLUT main loop.
Compile and run the above code in an environment where OpenGL and GLUT are installed. You should see a white circle drawn with points on a black background.