C++ program code for drawing line using DDA

//use Turbo-c and press alt+f5 to see output
#include<math.h>
#include<stdlib.h>
#include<iostream.h>
#include<dos.h>
#include<graphics.h>
using namespace std
void dda(int x1, int y1,int x2,int y2);
int main()
{
int gd= DETECT,gm; //detecting graphics driver automatically
int x1,y1,x2,y2;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
cleardevice();
cout<<"DDA LINE GENERATION ALGORITHM\n\n";
cout<<"ENTER STARTING COORDINATES OF DRAWING LINE\n";
cin>>x1>>y1;
cout<<"\nENTER ENDING COORDINATES OF LINE\n";
cin>>x2>>y2;
dda(x1,y1,x2,y2);
cout<<"\n INPUT ANY NUMBER + ENTER TO CLOSE\n";
cin>>x1;
closegraph();
}
void dda(int x1, int y1, int x2,int y2)
{
int i,dx,dy,step;
float x,y;
float xinc,yinc;
dx=(x2-x1);
dy=(y2-y1);
if(abs(dx)>=abs(dy))
step=dx;
else
step=dy;
xinc=(float)dx/step;
yinc=(float)dy/step;
x=x1;
y=y1;
putpixel(x,y,WHITE);
for(i=1;i<step;i++)
{
x=x+xinc;
y=y+yinc;
x1=x+0.5;
y1=y+0.5;
putpixel(x1,y1,WHITE);
}
}
 Output :
line drawing algorithm output

Comments

Popular posts from this blog

MATLAB code for Circular Convolution using Matrix method

Positive number pipe in angular 2+