C++ program for DDA line drawing algorithm using OpenGL
kw.cpp
#include<GL/glut.h>
#include<iostream>usingnamespacestd;staticfloatx1,y1,x2,y2;voidDDA(){floatx,y,k,xinc,yinc,step,dx,dy;dx=x2-x1;dy=y2-y1;if(dx>dy){step=dx;}else{step=dy;}xinc=dx/step;yinc=dy/step;x=x1;y=y1;glBegin(GL_POINTS);glVertex2i(x,y);glEnd();for(intk=0;k<step;k++){x=x+xinc;y=y+yinc;glBegin(GL_POINTS);glVertex2i(x,y);glEnd();}glFlush();}intmain(intagrc,char**argv){cout<<"Enter the x1 coordinate ";cin>>x1;cout<<"Enter the y1 coordinate ";cin>>y1;cout<<"Enter the x2 coordinate ";cin>>x2;cout<<"Enter the y2 coordinate ";cin>>y2;glutInit(&agrc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGBA|GLUT_DEPTH);glutInitWindowSize(640,480);glutCreateWindow("DDA Line Algorithm");glMatrixMode(GL_PROJECTION);gluOrtho2D(0,640,480,0);glutDisplayFunc(DDA);glutMainLoop();}
Output
kodingwindow@kw:~$ g++ kw.cpp -lGL -lGLU -lglut kodingwindow@kw:~$ ./a.out
Enter the x1 coordinate 20
Enter the y1 coordinate 20
Enter the x2 coordinate 150
Enter the y2 coordinate 150
Dear User, Thank you for visitng KodingWindow. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.