Friday, July 27, 2012

DDA algorithm using DOSBox 0.74

Hello friends!!Here is a code for DDA line drawing algorithm which prints the line pixel by pixel.The code is written in C++ and executed using DOSBox 0.74 which is a software to get graphics printout.
Note: It is not possible to get the screenshot of the output of any graphics program executed in DOS window,so we use a modified DOS environment called DOSBox 0.74.
Download DOSBox 0.74

Source code:
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#include<stdlib.h>
#include<dos.h>
void dda(int x1,int y1,int x2,int y2);
void main()
{int gd=DETECT,gm;
int x1,x2,y1,y2;
initgraph( &gd, &gm,"C:\\TC\\BGI");
cleardevice();
cout<<"DDA Line generation Algorithm";
cout<<"\n enter the starting co-ordinates for drawing line";
cin>>x1>>y1;
cout<<"\n enter the ending co-ordinates";
cin>>x2>>y2;
dda(x1,y1,x2,y2);
cout<<"\n Thank you";
getch();
closegraph();
}
void dda(int x1,int y1,int x2,int y2)
{
int j=1,dx,dy,steps;
float x,y;
float xinc,yinc;
dx=(x2-x1);
dy=(y2-y1);
if(abs(dx)>=abs(dy))
steps=dx;
else
steps=dy;
xinc=(float)dx/steps;
yinc=(float)dy/steps;
x=x1;
y=y1;
putpixel(x,y,WHITE);
cout<<"("<<x<<","<<y<<")";
getch();
while(j<=steps)
{
x=x+xinc;
y=y+yinc;
x1=x+0.5;
y1=y+0.5;
putpixel(x1,y1,WHITE);
cout<<"("<<x1<<","<<y1<<")";
getch();
j++;
}
}

Output:
After installing DOSBox 0.74,double-click and open the DOSBox 0.74 window.


The TC window will be displayed:


Run your code and you will get the following output: