How to implement DDA algorithm for Drawing Line?

DDA stands for Digital Differential Analyzer Algorithm. In computer graphics, a digital differential analyzer (DDA) is hardware or software used for interpolation of variables over an interval between start and end point.

DDA is a scan conversion line algorithm based on calculating either ∆y or ∆x.

If the slope of the line ‘m’ is less than or equal to 1, we sample at unit ‘x’ interval (∆x= 1) and compute each successive ‘y’ as: –

Yk+1 = mxk+1 + b

Yk+1 = m(xk+1) + b

Yk+1 = mxk + b + m

 Yk+1 = yk + m     ————————————– 1

 If the slope of the line ‘m’ is greater than or 1, we sample at unit ‘y interval (∆y= 1) and compute each successive ‘x’ as:

Yk+1 = mxk+1 + b

Yk + 1 = mxk+1 + b

Xk+1      = (yk + 1 – b)/m

Xk+1     = (mxk + 1)/m

Xk+1 = xk + 1/m     ————————————– 2

 Equation 1 and 2 are based on the assumption that lines are to processed from left to right end. If the process is reversed, so that the starting end point is at the right, then

Xk+1 = xk – 1/m

 Yk+1 = yk – m

 Note that y-intercept doesn’t appear in the above equation which is a feature of DDA algorithm

Algorithm

For a line drawn from left end to right end.

Read (x 1 , y 1 ) and (x 2 , y 2 ) as the two end points of a line.

Compute delx = x 2 – x 1

Compute dely = y 2 – y 1

If |delx| ≥ |dely| i.e. |m|<=1

steps = delx

else

steps = dely

Calculate

  1.  xinc =  delx/steps

    yinc = delx/steps.

for i = 0 to steps in steps of 1

Plot(x 1,x y 1 );

y 1 = y 1 + yinc;

x 1 = x 1 + xinc;

end for.

End

Program Code:

#include <stdio.h>

#include <graphics.h>

#include <stdlib.h>

#include <conio.h>

#include <math.h>

int main (void){

int gdriver = DETECT, gmode, errorcode;

initgraph(&gdriver,&gmode,”c:\\turboc3\\bgi”);

float x1,x2,y1,y2,delx,dely,slope;

float steps,xinc,yinc;

printf(“Enter the starting point (x1,y1): \n”);

scanf(“%f%f”,&x1,&y1);

printf(“Enter the end point (x2,y2): \n”);

scanf(“%f%f”,&x2,&y2);

delx = x2-x1;

dely = y2-y1;

if(fabs(delx)>=fabs(dely))

{

steps = delx;

}

else {

steps = dely;

}

xinc = delx/steps;

yinc = dely/steps;

for(int i=1;i<=steps;i++)

{

putpixel(x1,y1,RED);

x1= x1+ xinc;

y1= y1 +yinc;

}

getch();

closegraph();

return 0;

}

       

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: