Job Saarnee

# DAA is a Line Drawing Algorithm

DAA is a Line Drawing Algorithm that is used to Draw Line on the Basis of Value of Slop that m which is equal to dy/dx.

Let See the Algorithm for Drawing Line Using DDA Line Drawing Algorithm

Step 1: First read the two ends Point of line that is (x1, y1) and (x2, y2).
Step 2: Find out the Value of dy and dx using following formula
dy = y2 – y1
dx = x2 – x1
Step 3: Now we are going to compare the value of dx and dy in order to find out that m <= 1 or                 m > 1

if (dx > dy)
Step = dx
else
Step = dy

Step 4: Now we have to decide how much we have to increase in x direction or in y direction
IncX = dx / Step
IncY = dy / Step

Step 5: Now we are going to plot First Pixel of Line
x = x1
y = y1
Now Put Pixel at (x , y)
make i = 1

Step 6: While  i is less than equal to Step

x = x + IncX
y = y + IncY
Now Put Pixel at (x , y)
i = i +1

Step 7: Stop

Here is Program for Drawing Line Using DDA Line Drawing Algorithm

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int main(void)
{
int x1,y1,x2,y2,dx,dy,step,i=0,m=0;
float incx,incy;
/* request auto detection */
int gdriver = DETECT, gmode;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, “”);

// Here is Step 1 of Algorithm

printf(“enter the starting coordinate of line”);
scanf(“%d %d”, &x1,&y1);
printf(“enter the End coordinate of line”);
scanf(“%d %d”, &x2,&y2);

// Here is Step 2 of Algorithm
dx=x2-x1;
dy=y2-y1;

// Here is Step 3 of Algorithm
if(dx>dy)
{
step=dx;
}
else
{
step=dy;
}

// Here is Step 4 of Algorithm
incx=dx/step;
incy=dy/step;

// Here is Step 5 of Algorithm
putpixel(x1,y1,15);
i=1;

// Here is Step 6 of Algorithm
while(i<=step)
{

x1=x1+incx;
y1=y1+incy;
putpixel(x1,y1,15);
i=i+1;
}
/* clean up */
getch();
closegraph();
return 0;
}
Shopping Cart