Let us understand what is DDA algorithm in computer graphics and then let us see how to implement DDA line drawing algorithm in C programming using different methods.

#### What is DDA Line Algorithm?

DDA is an abbreviation for **Digital Differential Analyzer**. It is primarily used to find the interpolation over an interval between the initial and end coordinates.

It is an **incremental scan-conversion** line drawing algorithm. The calculations performed at every increment is based on the previous increment.

To draw any line in computer graphics, you need two coordinates on a 2-dimensional planar surface. Every line will have the start point **(X _{0}, Y_{0})** and end point

**(X**.

_{1}, Y_{1})For drawing a DDA line, we cannot directly connect the start and end points. We have to calculate intermediate point’s coordinate. Then, we have to insert an increment for each intermediate point on the line.

In this algorithm, we need to use floating-point arithmetic and it can use multiplication and division operations. This algorithm is a faster method for calculating the positions of the pixels.

There are, however, a lot of disadvantages with this line drawing algorithm. It is not very efficient when it comes to optimisation. It is not accurate as compared to other line drawing algorithms.

The c program for DDA line drawing algorithm rounds off the line coordinates to an integer which is an approximation to the expected line. However, the** round operations** in the algorithm are too inefficient.

There are several other line drawing algorithms that are much more efficient and accurate than DDA algorithm such as **Bresenham line drawing algorithm**.

#### DDA Line Generation Algorithm

Step 1: Inputs: Initial Line Co-ordinates(x1, y1) and End (x2, y2) line coordinates Step 2: Calculate dy = y2 – y1 and dx = x2 – x1 Step 3: if(dx >= dy) increment = dx else increment = dy Step 4: Compute dx = dx / increment & dy = dy / increment Step 5: x = x1 + 0.3 and y = y1 + 0.3 Step 6: for(count = 0; count < increment; count++) { putpixel(x, y) x = x + dx y = y + dy }

**IMPORTANT:** x1 != x2 and y1 != y2

If you face any issues while compiling this line drawing graphics program, you may have to **install graphics.h** in your operating system.

**Note:** This C program for DDA line generation algorithm is compiled with Turbo C compiler on Microsoft Windows 10 operating system.

#### Method 1: C Program To Implement DDA Line Drawing Algorithm

#include <stdio.h> #include <graphics.h> int main() { int count = 1, gdriver = DETECT, gmode; int delay_time, x, y, dx, dy, increment; int x1, y1, x2, y2; initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi"); printf("\nEnter Initial Line Coordinates:\n"); printf("\nEnter the value of x1:\t"); scanf("%d", &x1); printf("\nEnter the value of y1:\t"); scanf("%d", &y1); printf("\nEnter Final Line Coordinates:\n"); printf("\nEnter the value of x2:\t"); scanf("%d", &x2); printf("\nEnter the value of y2:\t"); scanf("%d", &y2); printf("\nEnter delay time:\t"); scanf("%d", &delay_time); dx = abs(x2 - x1); dy = abs(y2 - y1); if(dx >= dy) { increment = dx; } else { increment = dy; } dx = dx / increment; dy = dy / increment; x = x1; y = y1; for(count = 1; count <= increment; count++) { putpixel(x, y, 4); x = x + dx; y = y + dy; delay(delay_time); } closegraph(); return 0; }

#### Method 2: DDA line drawing algorithm in C Programming using Function

#include <stdio.h> #include <graphics.h> void dda_algorithm(int dx, int dy, int x1, int x2, int y1, int y2, int x, int y, int delay_time); int main() { int gdriver = DETECT, gmode; int delay_time, x, y, dx, dy; int x1, y1, x2, y2; initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi"); printf("\nEnter Initial Line Coordinates:\n"); printf("\nEnter the value of x1:\t"); scanf("%d", &x1); printf("\nEnter the value of y1:\t"); scanf("%d", &y1); printf("\nEnter Final Line Coordinates:\n"); printf("\nEnter the value of x2:\t"); scanf("%d", &x2); printf("\nEnter the value of y2:\t"); scanf("%d", &y2); printf("\nEnter delay time:\t"); scanf("%d", &delay_time); dda_algorithm(dx, dy, x1, x2, y1, y2, x, y, delay_time); return 0; } void dda_algorithm(int dx, int dy, int x1, int x2, int y1, int y2, int x, int y, int delay_time) { int count = 1; int increment; dx = abs(x2 - x1); dy = abs(y2 - y1); if(dx >= dy) { increment = dx; } else { increment = dy; } dx = dx / increment; dy = dy / increment; x = x1; y = y1; for(count = 1; count <= increment; count++) { putpixel(x, y, 4); x = x + dx; y = y + dy; delay(delay_time); } closegraph(); }

#### Output

Enter Initial Line Coordinates

Enter the value of x1: 50

Enter the value of y1: 70

Enter Final Line Coordinates

Enter the value of x2: 80

Enter the value of y2: 120

Enter delay time: 4

Let us discuss more on this DDA line drawing algorithm in C programming in the comment section below.

Do let us know if you have any doubts or have any information to share. For information, check Wikipedia.

Why don’t you guys use OpenGL for all these graphics line drawing codes? Its much better and advanced than graphics.h or sdl.h.

Thank you so much.