0% found this document useful (0 votes)
151 views

DDA Line Drawing Algorithm

This document contains C++ code to implement the digital differential analyzer (DDA) algorithm for drawing a line between two points on a graph. It includes functions to initialize the graphics window, round values, draw the line using DDA, and get user input for the start and end points. The main function gets the point coordinates, sets up the OpenGL window, and calls the DDA line drawing function in a loop to display the line between the points.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
151 views

DDA Line Drawing Algorithm

This document contains C++ code to implement the digital differential analyzer (DDA) algorithm for drawing a line between two points on a graph. It includes functions to initialize the graphics window, round values, draw the line using DDA, and get user input for the start and end points. The main function gets the point coordinates, sets up the OpenGL window, and calls the DDA line drawing function in a loop to display the line between the points.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 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