Here you will learn about dda line drawing algorithm in C and C++.

In Computer Graphics the first basic line drawing algorithm is Digital Differential Analyzer (DDA) Algorithm.

A line connects two points. It is a basic element in graphics. To draw a line, you need two points between which you can draw a line.

**Also Read: Bresenham’s Line Drawing Algorithm in C and C++**

### Digital Differential Analyzer (DDA) Algorithm

**Step 1: **Read the input of the 2 end points of the line as (x1, y1) & (x2, y2) such that x1 != x2 and y1 != y2

**Step 2: **Calculate dx = x2 – x1 and dy = y2 – y1

**Step 3:**

if(dx>=dy)

step=dx

else

step=dy

**Step 4: **xin = dx / step & yin = dy / step

**Step 5:** x = x1 + 0.5 & y = y1 + 0.5

**Step 6: **

for(k = 0; k < step; k++)

{

x = x + xin

y = y + yin

putpixel(x, y)

}

## Program for DDA Line Drawing Algorithm in C

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#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(); } |

**Outptut**

## Program for DDA Line Drawing Algorithm in C++

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#include <graphics.h> #include <iostream.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"); cout<<"Enter the value of x1 and y1 : "; cin>>x1>>y1; cout<<"Enter the value of x2 and y2: "; cin>>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(); } |

Comment below if you have any doubts related above algorithm.

vishalmadheshiyaNo such file or directory

#include

^

compilation terminated.

this error is encountered while compilation of the above written program

Adminwhich compiler you are using?

Rahul mehrai an using HP computer Intel Pentium inside

Dhananjay PanageHe was asking name of your compiler not your processor! Maybe he meant editor or IDE!

RAMI DO NO BRO

Srashti GuptaThere may be no graphics added in your system.

import them for internet

Dhananjay PanageWhat do you mean by “no graphics added in your system”?

Ashutosh Bajpaiyes you right there may be no graphics added in his computer

AyeshaPlz tel me dda line draw algo example

Bubai kjndu3 error on my desktop screen

Hassan javedall programs are awesome, Thanks for such a nice blog, where can I get all the commands used in graphics.h ??? I want to know, making circle, and the arguments of circle, what is first one? Or x and y are used to give it postiion, you things like that,

Nithyanandhan.V.GrTq, works well

Mark VA simple way to explain this might be that you’re simply counting the number of ‘steps’ to take between Point 1 and Point 2, calculating the x-axis length and y-axis of each ‘step’, and then looping to add that step length to X and Y respectively for each step between point 1 to point 2.

Unless I’m mistaken, your algorithm here (and your Bresenham algorithm too) only works when X2 > X1 and Y2 > Y1. It’s easy to fix for any two arbitrary points: simply subtract the axis length instead of adding it (dx or dy), in the case that X1 > X2 or Y1 > Y2 respectively.

CS18instead of abs in x2-x1 use abs at place of comparing in if statements ,it will work for all cases

aravindno error………………output wiil not display…………..

sandychange the bgi path

hiteshhow to do that??

Sreeleshfile>>Change directory >> [set to bgi] >> click Change

coder/gamer69THANK YOU SO MUCH IT WORKED!!!

hiteshi faced the same problem….

manishchange to ‘c://tc//bgi”

SathyaBresenham’s algorithm for square

vivek{

putpixel(x,y,5);

x=x+dx;

y=y+dy;

i=i+1;

delay(100);

}

ye smjh nhi aaya ki kya use h iska.

sidrayah chal nai raha

eskay bad

Enter the value of x1 and y1

Enter the value of x2 and y2

window bgi screen dev pay line show nai ho rahi???????/???

tamimgraphics.h Error why no such file

hiteshuse turbo c++ instead of other softwares.

Ashish Saxenagraphics.h is not available try without Graphics.h and their functions

Srashti GuptaSame Problem There must be no graphics added in your compiler. Import them from internet.

Chkikan guniyaTHanks a lot brother

sandeshTHanks a lot brother

vivek morenot running

devathen u run

AyushHow to run this on macOS or Linux?? The BGI(Borland Graphics Library) was created for Windows. This code doesn’t work on macOS or Linux?

gaganafter setting path,,,still not displaying output…..

ganeshdeclerition not aloted her in gd=DETECT

atul pusamcan u tell how run c program in command prompt in win 8……….

Dhananjay PanageThis would help you

First: Go to the working directory (place your file is stored)

Second: open cmd(command promt there) or press Win+r and then enter cmd in the search box then manually change the path (it will be C: by default)

Third: when you are in working directory enter gcc “filename.c” -o “filename.exe” (if it is a c program) if the file compiles correctly then press ./”filename.exe” this will run your code

AND PLEASE MAKE SURE THAT YOU HAVE SET UP YOUR PATH FOR GCC AND REMOVE THE QUOTES WHILE ENTERING THE FILENAME.

chaitanyahow to draw multiple lines using dda

Aditya NayakHey just checked your code its not applicable for cases like initial points (80,20) and (20,20). Please modify the code

Purushottam Prabhakarcan you explain why are you using step 5 with xinc of 0.5 .

snehaI got output on the top of window. But according to algorithm output may be shown in x and y axis.

Cool Dudeyour program is not working dude