# DDA Algorithm (Digital Differential Analyzer)

DDA algorithm is the simplest line drawing algorithm which is based on the slope of the line. It was the first line drawing algorithm that was developed by the mathematician. Given the starting and ending coordinates of the line, the DDA algorithm generates the points between the starting and ending coordinates.

### Advantage:

- It is a faster method than method of using direct use of line equation.
- This method does not use multiplication theorem.
- It allows us to detect the change in the value of x and y ,so plotting of same point twice is not possible.
- This method gives overflow indication when a point is repositioned.
- It is an easy method because each step involves just two additions.

### Disadvantage:

- It involves floating point additions rounding off is done. Accumulations of round off error cause accumulation of error.
- Rounding off operations and floating point operations consumes a lot of time.
- It is more suitable for generating line using the software. But it is less suited for hardware implementation.

## DDA Algorithm:

**Step1:** Start Algorithm

**Step2:** Declare x_{1},y_{1},x_{2},y_{2},dx,dy,x,y as integer variables.

**Step3:** Enter value of x_{1},y_{1},x_{2},y_{2}.

**Step4:** Calculate dx = x_{2}-x_{1}

**Step5:** Calculate dy = y_{2}-y_{1}

**Step6:** If ABS (dx) > ABS (dy)

Then step = abs (dx)

Else,

step =abs (dy)

**Step7:** x_{inc}=dx/step

y_{inc}=dy/step

assign x = x_{1}

assign y = y_{1}

**Step8:** Set pixel (x, y)

**Step9:** x = x + x_{inc}

y = y + y_{inc}

Set pixels (Round (x), Round (y))

**Step10:** Repeat step 9 until x = x_{2}

**Step11:** End Algorithm.

## Program for DDA line drawing algorithm:

```
#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <dos.h>
void main( ){
float x,y,x1,y1,x2,y2,dx,dy,step;
int i,gd=DETECT,gm;
initgraph(&gd,&gm,"c:\turboc3\bgi");
printf("Enter the value of x1 and y1 : ");
scanf("%f%f",&x1,&y1);
printf("Enter the value of x2 and y2: ");
scanf("%f%f",&x2,&y2);
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy){
step=dx;
}
else{
step=dy;
}
dx=dx/step;
dy=dy/step;
x=x1;
y=y1;
i=1;
while(i=step)
{
putpixel(x,y,5);
x=x+dx;
y=y+dy;
i=i+1;
delay(100);
}
closegraph();
}
```