Professional Documents
Culture Documents
PROGRAM CODE:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#define ROUND(a)((int)(a+0.5))
void line_DDA(int ,int ,int ,int );
void main()
{ int x1,x2,y1,y2;
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("enter the first end point x1 ");
scanf("%d",&x1);
printf("enter the first end point y1 ");
scanf("%d",&y1);
printf("enter the second end point x2 ");
scanf("%d",&x2);
printf("enter the second end point y2 ");
scanf("%d",&y2);
line_DDA(x1,y1,x2,y2);
getch();
closegraph();
}
void line_DDA(int x1,int y1,int x2,int y2)
{
int dx,dy,steps,k;
float xinc,yinc,x,y;
dx=x2-x1;
dy=y2-y1;
x=x1;
y=y1;
if(abs(dx) > abs(dy))
{
steps=abs(dx);
}
else
{
steps=abs(dy);
}
xinc=dx/(float)steps;
yinc=dy/(float)steps;
putpixel(ROUND(x),ROUND(y),YELLOW);
for(k=0;k<steps;k++)
{
x+=xinc;
y+=yinc;
putpixel(ROUND(x),ROUND(y),YELLOW);
}
}
OUTPUT:
CONCLUSION:
The DDA Algorithm is a faster method for calculating pixel positions than the
direct use of a line equation y = mx + b .