DDA Line Drawing program in c
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
//variable declaration
int dx,dy,length,x1,y1,x2,y2,k,p,q;
float x,y,m;
float stepx,stepy;
//to autodetect the graphic drivers and mode
// set gd=DETECT,DETECT is a macro declared in graphics.h
int gd=DETECT,gm;
//call the pre-defined method initgraph with the address of gd nd gm
//input
printf("Enter the x co-ordinate of start pt\t");
scanf("%d",&x1);
printf("Enter the y co-ordinate of start pt\t");
scanf("%d",&y1);
printf("Enter the x co-ordinate of end pt\t");
scanf("%d",&x2);
printf("Enter the y co-ordinate of end pt\t");
scanf("%d",&y2);
//diff. in x co-ordinate
dx=(x2-x1);
//diff. in y-co-ordinate
dy=(y2-y1);
if(dx>=dy)
length=dx;
else
length=dy;
stepx=(float)dx/length;
printf("%f",stepx);
stepy=(float)dy/length;
printf("%f",stepy);
x=x1;
y=y1;
getch();
initgraph(&gd,&gm,"c:\\TC\\BGI");
//setviewport(getmaxx()/2,getmaxy()/2,getmaxx(),getmaxy(),100);
//for loop to print the line
// with every iteration x increases 1 and y by the slope
cleardevice();
p=getmaxx();
q=getmaxy();
line(p/2,0,p/2,q);
line(0,q/2,p,q/2);
for(k=1;k<=length;k++)
{
//use the putpixel method of graphics.h
delay(50);
putpixel((p/2)+x,(q/2)-y,25);
x=x+stepx;
y=y+stepy;
}
getch();
//close the graph and come back to the text mode
closegraph();
}