C++ Program to implement Digital differential analyzer (DDA graphics algorithm)

In computer graphics, a digital differential analyzer (DDA) is hardware or software used for linear interpolation of variables over an interval between start and end point. DDAs are used for rasterization of lines, triangles and polygons. In its simplest implementation, the DDA algorithm interpolates values in interval by computing for each xi the equations xi = xi−1+1/m, yi = yi−1 + m, where Δx = xend − xstart and Δy = yend − ystart and m = Δy/Δx

Program

#include <iostream.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
#include <process.h>
void main()
{
float i,x1,x2,y1,y2,dx,dy,x,y,step,xinr,yinr;
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"C:\\Tc\\BGI");
cout<<"Enter the values\n";
cin>>x1>>y1>>x2>>y2;
dx=x2-x1;
dy=y2-y1;
if(dx==0&&dy==0)
{
putpixel(x1,y1,15);
getch();
exit(0);
}
if(abs(dx)>=abs(dy))
step=abs(dx);
else
step=abs(dy);
xinr=dx⁄step;
yinr=dy⁄step;
x=x1;
y=y1;
for(i=1;i<=step;i++)
{
putpixel(x,y,15);
x=x+xinr;
y=y+yinr;
}
getch();
closegraph();
}

0 comments:

Post a Comment