Code to Implement DDA Line Generation


#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
void main()
{
clrscr();
int gd,gm;
gd=DETECT;
gm=0;

int xa,ya,xb,yb;
cout<<"Enter ordinate of first point";
cin>>xa;
cout<<"Enter abissca of first point";
cin>>ya;
cout<<"Enter ordinate of second point";
cin>>xb;
cout<<"Enter abissca of second point";
cin>>yb;
double dx,dy,steps;
double xinc,yinc;
dx=xb-xa;
dy=yb-ya;
if(dx>=dy)
steps=abs(dx);
else
steps=abs(dy);
xinc=dx/steps;
yinc=dy/steps;
initgraph(&gd,&gm,"Z:\tc\BGI");
putpixel(xa,ya,4);
double x,y;
x=xa+xinc;
y=ya+yinc;

for(int i=0;i<steps;i++)
{
putpixel(ceil(x),ceil(y),4);
x+=xinc;
y+=yinc;
}
getch();
}

0 comments :