line drawing using DDA algorithm

This program is for Drawing a line using DDA algorithm . This is a part of Mumbai University MCA Colleges CG Programs MCA Computer Graphics

#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();
}

Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

No comments:

Post a comment