Saturday, May 2, 2009

C program to implement Digital Differential Analyzer Line drawing algorithm

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>

void main()
{
int gd=DETECT,gm;
int x1,x2,y1,y2,dx,dy,steps,k;
float xi,yi,x,y;
clrscr();

initgraph (&gd,&gm,"C:\\TC\\BGI");
printf("Enter the co-ordinates of the first point \n");
printf("x1= ");
scanf("%d/n",&x1);
printf("y1= ");
scanf("%d/n",&y1);
printf("Enter the co-ordinates of the second point \n");
printf("x2= ");
scanf("%d/n",&x2);
printf("y2= ");
scanf("%d/n",&y2);
clrscr();
dx= x2-x1;

dy= y2-y1;
if (abs(dx) > abs(dy))
steps = abs(dx);

else
steps = abs(dy);
xi=(float)dx/steps;

yi=(float)dy/steps;
x=x1;

y=y1;
for(k=0;k<steps;k++)
{
putpixel (x,y,BLUE);
x=x+xi;
y=y+yi;
}
getch();

closegraph();
}