# DDA Algorithm

DDA stands for Digital Differential Analyzer. It is an incremental method of scan conversion of line. In this method calculation is performed at each step but by using results of previous steps.

Suppose at step i, the pixels is (x_{i},y_{i})

The line of equation for step i

y_{i}=mx_{i+b}………………….equation 1

Next value will be

y_{i+1}=m_{xi+1}+b……………..equation 2

m =

y_{i+1}-y_{i}=∆y…………………..equation 3

y_{i+1}-x_{i}=∆x………………….equation 4

y_{i+1}=y_{i}+∆y

∆y=m∆x

y_{i+1}=y_{i}+m∆x

∆x=∆y/m

x_{i+1}=x_{i}+∆x

x_{i+1}=x_{i}+∆y/m

**Case1:** When |M|<1 then (assume that x_{1}2)

x= x_{1},y=y_{1} set ∆x=1

y_{i+1}=y_{1+m}, x=x+1

Until x = x_{2}_{}

**Case2:** When |M|<1 then (assume that y_{1}2)

x= x_{1},y=y_{1} set ∆y=1

x_{i+1}=, y=y+1

Until y → y_{2}_{}

### 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

**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

**Example:** If a line is drawn from (2, 3) to (6, 15) with use of DDA. How many points will needed to generate such line?

**Solution:** P_{1} (2,3) P_{11} (6,15)

x_{1}=2

y_{1}=3

x_{2}= 6

y_{2}=15

dx = 6 – 2 = 4

dy = 15 – 3 = 12

m =

For calculating next value of x takes x = x +

### Program to implement DDA Line Drawing Algorithm:

**Output:**

## Symmetrical DDA:

The Digital Differential Analyzer (DDA) generates lines from their differential equations. The equation of a straight line is

The DDA works on the principle that we simultaneously increment x and y by small steps proportional to the first derivatives of x and y. In this case of a straight line, the first derivatives are constant and are proportional to ∆x and ∆y . Therefore, we could generate a line by incrementing x and y by ϵ ∆x and ϵ ∆y, where ϵ is some small quantity. There are two ways to generate points

1. By rounding to the nearest integer after each incremental step, after rounding we display dots at the resultant x and y.

2. An alternative to rounding the use of arithmetic overflow: x and y are kept in registers that have two parts, integer and fractional. The incrementing values, which are both less than unity, are repeatedly added to the fractional parts and whenever the results overflows, the corresponding integer part is incremented. The integer parts of the x and y registers are used in plotting the line. In the case of the symmetrical DDA, we choose ε=2^{-n},where 2^{n-1}≤max (|∆x|,|∆y|)<2^{π}

A line drawn with the symmetrical DDA is shown in fig:

**Example:** If a line is drawn from (0, 0) to (10, 5) with a symmetrical DDA

1. How many iterations are performed?

2. How many different points will be generated?

**Solutions:** Given: P^{1} (0,0) P^{2} (10,5)

x^{1}=0

y^{1}=0

x^{2}=10

y^{2}=5

dx = 10 – 0 = 10

dy = 5 – 0 = 0

P^{1} (0,0) will be considered starting points

P^{3} (1,0.5) point not plotted

P^{4} (2, 1) point plotted

P^{5} (3, 1.5) point not plotted

P^{6} (4,2) point plotted

P^{7} (5,2.5) point not plotted

P^{8} (6,3) point plotted

P^{9} (7,3.5) point not plotted

P^{10} (8, 4) point plotted

P^{11} (9,4.5) point not plotted

P^{12} (10,5) point plotted

Following Figure show line plotted using these points.