DDA Line Drawing Algorithm

1)


CODE:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,dx,dy,a1,a2,b1,b2,i;
float step,xinc,yinc,x,y;
char a[20],b[20],c[20],d[20];
clrscr();
initgraph(&gd,&gm," ");
printf("\nEnter the starting point");
scanf("%d%d",&x1,&y1);
printf("\nEnter the ending point");
scanf("%d%d",&x2,&y2);
cleardevice();
a1=x1;
a2=x2;
b1=y1;
b2=y2;
x1+=20;
x2+=20;
y1=470-y1;
y2=470-y2;
line(20,20,20,470);
line(20,470,590,470);
outtextxy(190,60,"**********DDA LINE DRAWING ALGORITHM**********");
sprintf(a,"%c%d%c%d%c",'(',a1,',',b1,')');
sprintf(b,"%c%d%c%d%c",'(',a2,',',b2,')');
outtextxy(15,15,"^y");
outtextxy(590,469,">x");
outtextxy(x1,y1,a);
outtextxy(x2,y2,b);
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
step=abs(dx);
else
step=abs(dy);
xinc=dx/step;
yinc=dy/step;
x=x1;
y=y1;
for(i=1;i<=step;i++)
{
putpixel(x,y,RED);
x=x+xinc;
y=y+yinc;
}
getch();
closegraph();
}

OUTPUT:



Comments

POPULAR POSTS

POPULAR POSTS