You are on page 1of 2

EXPERIMENT 1

AIM : DDA LINE DRAWING ALGORITHM

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 .

In this algorithm for finding a position of a point, there can be an increment in


both x coordinate or can be in a y coordinate. On the basis of it, there are two
cases generated which are as given below:

1. If the value of m is less than or equal to 1, then x=x+1 and y=y+m.


2. If the value of m is greater than 1, then x=x+1/m and y=y+1.

You might also like