OpenGL draws lines and circles through the midpoint method

Time:2021-11-22

This example shares the specific code of OpenGL drawing lines and circles for your reference. The specific contents are as follows

#include <gl/glut.h>
#include <math.h>
 
static int i=1;
 
void Initial(void)
{
 glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set the window background color to white
 glMatrixMode(GL_PROJECTION); // Specify and set projection parameters
 gluOrtho2D(-200.0,200.0,-200.0,200.0); // Set the projection parameter. // it refers to the view range. If (0200,0200), it can only be displayed in the first quadrant, i.e. 1 / 4 circle. The parameters 1 and 3 are the coordinates of the lower left corner, and the parameters 2 and 4 are the coordinates of the upper right corner
}
/*void Display(void)
{
 glClear(GL_COLOR_BUFFER_BIT); // Fills the window with the current background color
 glColor3f(1.0f, 0.0f, 0.0f);  // Sets the current drawing color to red
 glRectf(50.0f, 100.0f, 150.0f, 50.0f); // Draw a rectangle
  glFlush();    // Handle all OpenGL programs
}
*/
 
void DDALine(int x0,int y0,int x1,int y1)
{
 //glVertex2f(10,10);
 int dx,dy,epsl,k;
 float x,y,xIncre,yIncre;
 dx = x1 - x0;
 dy = y1 - y0;
 x = x0;
 y = y0;
 
 if(abs(dx)>abs(dy))
 epsl=abs(dx);
 else
 epsl=abs(dy);
 
 xIncre=(float)dx/(float)epsl;
 yIncre=(float)dy/(float)epsl;
 glPointSize(2);
 glBegin(GL_POINTS);
 for(k = 0;k <= epsl; k++)
 {
 glColor3f(1.0f, 0.0f, 0.0f);
 glVertex2f(int(x+0.5),int(y+0.5));
 x += xIncre;
 y += yIncre;
 }
 glEnd();
 
}
 
 
 
void Mid_B(int x0,int y0,int x1,int y1)
{
 int dx,dy,d,UpIncre,DownIncre,x,y;
 if(x0 > x1)
 {
 x = x1;
 x1 = x0;
 x0 = x;
 y = y1;
 y1 = y0;
 y0 = y;
 }
 x = x0;
 y = y0;
 dx = x1 - x0;
 dy = y1 - y0;
 d = dx - 2*dy;
 UpIncre = 2*dx - 2*dy;
 DownIncre =- 2*dy;
 
 glPointSize(2);
 glBegin(GL_POINTS);
 while(x <= x1)
 {
 glColor3f(1.0f, 0.0f, 0.0f);
 glVertex2f(x,y);
 x++;
 if(d < 0)
 {
 y++;
 d += UpIncre;
 }
 else
 d += DownIncre;
 }
 glEnd();
 
}
 
void G_B(int x0,int y0,int x1,int y1)
{
 int x,y,dx,dy,e;
 dx = x1 - x0;
 dy = y1 - y0;
 e =- dx;
 x=x0;
 y=y0;
 
 glPointSize(2);
 glBegin(GL_POINTS);
 while(x <= x1)
 {
 glColor3f(1.0f, 0.0f, 0.0f);
 glVertex2f(x,y);
 x++;
 e = e + 2*dy;
 if(e > 0)
 {
 y++;
 e = e - 2*dx;
 }
 
 }
 glEnd();
}
 
void CirclePoint(int x, int y)
{
 glPointSize(2);
 glBegin(GL_POINTS);
 glColor3f(1.0f, 0.0f, 0.0f);
 glVertex2f(x,y);
 glVertex2f(y,x); 
 glVertex2f(-y,x); 
 glVertex2f(-x,y); 
 glVertex2f(-x,-y); 
 glVertex2f(-y,-x); 
 glVertex2f(y,-x); 
 glVertex2f(x,-y);
 glEnd();
}
 
void MidBresenhamCircle(int r)
{
 int x = 0, y = r, d = 1-r;
 //glPointSize(2);
 //glBegin(GL_POINTS);
 while(x <= y)
 {
 CirclePoint(x,y);
 
 if(d < 0)
 d += 2*x+3;
 else
 {
 d += 2*(x-y)+5;
 y--;
 }
 x++;
 }
 glEnd();
}
 
void ProcessMenu(int value)
{
 i = value; 
 glutPostRedisplay();
}
 
 
 
void Display(void)
{
 glClear(GL_COLOR_BUFFER_BIT); // Fills the window with the current background color
 glViewport(0,0,400,400); // The first two parameters change the origin coordinates, and the last two parameters change the length and width of the figure (zoom in and out)
 switch(i)
 {
 case 1:
 DDALine(2,3,55,83);
 break;
 case 2:
 Mid_B(2,3,55,83);
 break;
 case 3:
 G_B(2,3,55,83);
 break;
 case 4:
 MidBresenhamCircle(50);
 break;
 
 }
 glFlush(); 
}
 
int main(int argc, char* argv[])
 
{
 glutInit(&argc, argv); // Initialize the glut library and process command line parameters 
 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Initializes the display mode of the window
 glutInitWindowSize(400,400);  // Sets the size of the window
 glutInitWindowPosition(100,100); // Sets the position of the window
 Glutcreatewindow ("line")// Create a window called rectangle
 
 int MainMenu = glutCreateMenu(ProcessMenu); // Create main menu
 Glutaddmenuentry ("DDA algorithm", 1);
 Glutaddmenuentry ("midpoint Bresenham algorithm", 2);
 Glutaddmenuentry ("improved Bresenham algorithm", 3);
 Glutaddmenuentry ("draw a circle at Bresenham", 4);
 glutAttachMenu(GLUT_RIGHT_BUTTON); 
 
 glutDisplayFunc(Display); // Sets the display callback function of the current window
 Initial();     // Complete window initialization
 glutMainLoop();   // Start the main glut event handling cycle
 return 0;
 
 
}

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.