This program is for Drawing a line using DDA algorithm .
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#define round(a) (int (a+0.5))
void main()
{
int xa,ya,xb,yb,dx,dy,steps,k;
float xincrement,yincrement,x,y;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
cleardevice();
printf("Enter the end co-ordinates of the line(xa,ya) & (xb,yb) ");
scanf("%d%d%d%d",&xa,&ya,&xb,&yb);
x=xa;
y=ya;
printf("%f %f",x,y);
dx=(xb-xa);
dy=(yb-ya);
if(abs(dx)>=abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xincrement=float(dx)/steps;
yincrement=float(dy)/steps;
putpixel(round(x),round(y),1);
for(k=1;k<=steps;k++)
{
x=x+xincrement;
y=y+yincrement;
putpixel(round(x),round(y),1);
}
getch();
closegraph();
}
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#define round(a) (int (a+0.5))
void main()
{
int xa,ya,xb,yb,dx,dy,steps,k;
float xincrement,yincrement,x,y;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
cleardevice();
printf("Enter the end co-ordinates of the line(xa,ya) & (xb,yb) ");
scanf("%d%d%d%d",&xa,&ya,&xb,&yb);
x=xa;
y=ya;
printf("%f %f",x,y);
dx=(xb-xa);
dy=(yb-ya);
if(abs(dx)>=abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xincrement=float(dx)/steps;
yincrement=float(dy)/steps;
putpixel(round(x),round(y),1);
for(k=1;k<=steps;k++)
{
x=x+xincrement;
y=y+yincrement;
putpixel(round(x),round(y),1);
}
getch();
closegraph();
}