You are on page 1of 2

#include<glut.

h>
#include<iostream>
#include<math.h>
using namespace std;
int X0,Y0,xEnd,yEnd;
int Init()
{
glClearColor(1.0,1.0,1.0,0);
glColor3f(1.0,0.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0,640,0,480);
}
int round(int a)
{
return a+0.5;
}
void dda_dline()
{
int x1,y1,x2,y2,count=0,temp, dx,dy,step;
if(X0>xEnd)
{
x1=xEnd;
y1=yEnd;
x2=X0;
y2=Y0;
X0=x1;
Y0=y1;
xEnd=x2;
yEnd=y2;
}
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
glVertex2i(X0,Y0);
step=abs(abs(y2-y1)/abs(x2-x1));
while(x1<x2)
{
x1++;
if(step<1)
{
y1=round(y1+step);
count++;
}
else
{
x1=round(x1+(1/step));
y1=y1+1;
count++;
}
if(count==4)
{
x1=x1+2;
count=0;
}
glClear(GL_COLOR_BUFFER_BIT);
glVertex2i(x1,y1);
}
glEnd();
glFlush();
}
int main(int argc,char **argv)
{
cout<<"Enter the starting point(x,y) : ";
cin>>X0>>Y0;
cout<<"\nEnter the ending point(x,y) : ";
cin>>xEnd>>yEnd;
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(0,0);
glutInitWindowSize(640,480);
glutCreateWindow("DDA Deshed Line");
Init();
glutDisplayFunc(dda_dline);
glutMainLoop();
}

You might also like