DDA Line Drawing Algorithm Using C Programming

🎯 Introduction of DDA Line Algorithm

In computer graphics, line drawing algorithms are used to generate lines and curves on a computer screen. The DDA (Digital Differential Analyzer) algorithm is a simple and efficient method for drawing lines. This blog post will provide an enhanced and explained version of the DDA line drawing algorithm implemented in C programming language. We will discuss the algorithm, its implementation, and provide a step-by-step explanation of the code.

🎯 Prerequisites

Before we dive into the implementation, make sure you have the necessary libraries installed. In this code, we are using the stdio.h, math.h, conio.h, and graphics.h libraries. The graphics.h library is used to create a graphical window for displaying the line.

🎯 Implementation

Source code in C programming

#include<stdio.h> 

#include<math.h> 

#include<graphics.h> 

#define round(val) (int)(val+0.5) 


void line_dda(int xa, int ya, int xb, int yb) {

   int Dx = xb - xa, Dy = yb - ya, steps, k;

   float xin, yin, X = xa, Y = ya;

   if (abs(Dx) > abs(Dy))

      steps = abs(Dx);

   else

      steps = abs(Dy);


   xin = Dx / (float) steps;

   yin = Dy / (float) steps;

   putpixel(round(X), round(Y), 6);


   for (k = 0; k < steps; k++) {

      X = X + xin;

      Y = Y + yin;

      putpixel(round(X), round(Y), 6);

   }

}


int main() {

   int gd = DETECT, gm;

   int xa, xb, ya, yb;

   printf("Enter the two values: ");

   scanf("%d%d%d%d", &xa, &ya, &xb, &yb);

   initgraph(&gd, &gm, "");

   cleardevice();

   line_dda(xa, ya, xb, yb);

   getch();

   closegraph();

   return 0;

}

🎯 Explanation

Let's go through the code and understand how the DDA line drawing algorithm works.

line_dda Function

The line_dda function takes four parameters: xa (x-coordinate of the starting point), ya (y-coordinate of the starting point), xb (x-coordinate of the ending point), and yb (y-coordinate of the ending point). This function is responsible for drawing the line using the DDA algorithm.

Calculate the differences in x and y coordinates:

int Dx = xb - xa, Dy = yb - ya;

Determine the number of steps required to draw the line:

int steps;

if (abs(Dx) > abs(Dy))

   steps = abs(Dx);

else

   steps = abs(Dy);

Calculate the increments in x and y for each step:

float xin = Dx / (float) steps;

float yin = Dy / (float) steps;

Set the initial point of the line:

float X = xa, Y = ya;

putpixel(round(X), round(Y), 6);

Iterate through each step and update the coordinates:

for (int k = 0; k < steps; k++) {

   X += xin;

   Y += yin;

   putpixel(round(X), round(Y), 6);

}

main Function

The main function is the entry point of our program. It performs the following steps:

Initialize the graphics system:

int gd = DETECT, gm;

initgraph(&gd, &gm, "");

Clear the graphics screen:

cleardevice();

Accept user input for the coordinates of the line:

int xa, xb, ya, yb;

printf("Enter the two values: ");

scanf("%d%d%d%d", &xa, &ya, &xb, &yb);

Call the line_dda function to draw the line:

line_dda(xa, ya, xb, yb);

Wait for a key press before closing the graphics window:

getch();

Close the graphics system and return from the main function:

closegraph();

return 0;

The DDA line drawing algorithm is a simple and effective method for drawing lines on a computer screen. By implementing this algorithm in C programming language, we can generate lines with different slopes and lengths. The enhanced code provided in this blog post includes proper explanations and a user-friendly interface to input the line coordinates.