My Project - Video Waker Alarm

c program for dda algorithm

C program for dda algorithm. DDA (Digital Differential Analyzer) Algorithm also know as Vector Generation algorithm.

Why DDA algorithm ?

DDA Algorithm is used to plot line between two nodes i.e two end points in computer system.
While our computer understand pixels, if we want to plot a line, we should have maximum intermediate vertices of the line i.e intermediate points, so as to generate a straight line. Here DDA does the same.

c program for dda algorithm :

#include <graphics.h>
#include <stdio.h>
#include <math.h>

int main( )
{
float x,y,x1,y1,x2,y2,dx,dy,pixel;
int i,gd,gm;

printf("Enter the value of x1 : ");
scanf("%f",&x1);
printf("Enter the value of y1 : ");
scanf("%f",&y1);
printf("Enter the value of x2 : ");
scanf("%f",&x2);
printf("Enter the value of y1 : ");
scanf("%f",&y2);

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

dx=abs(x2-x1);
dy=abs(y2-y1);

if(dx>=dy)
pixel=dx;
else
pixel=dy;

dx=dx/pixel;
dy=dy/pixel;

x=x1;
y=y1;

i=1;
while(i<=pixel)
{
putpixel(x,y,1);
x=x+dx;
y=y+dy;
i=i+1;
delay(100);
}
getch();
closegraph();
}

Output :

DDA algorithm :

1. Define the nodes, i.e end points in form of (x1,y1) and (x2,y2).
2. Calculate the distance between the two end points vertically and horizontally, i.e dx=|x1-x2| and dy=|y1-y2|.
3. Define new variable name ‘pixel’, and compare dx and dy values,
if dx > dy then
pixel=dx
else
pixel =dy.
4. dx=dx/pixel
and dy=dy/pixel
5. x=x1;
y=y1;
6. while (i<=pixel) compute the pixel and plot the pixel with x=x+dx and y=y+dy.

If you are having any problem related to the post, do comment below.

32 thoughts on “c program for dda algorithm”

1. Prayag khannal says:

Why didn’t you do the rounding in this program?

1. neeti joshi says:

because we want pixel in integer format so we have to round it

2. kavitha says:

It showing an error that GRAPHICS.H is No such file or directory.

Do it in Turbo C++.fedora does’t contain graphics

1. shiv says:

I hav done it in cpp but after taking the 4 inputs closes the output vindow

3. without rounding off would it work successfully ??
the principle of digital differential analyzer is it rounding off the point which contain a fractional value, for which it takes more time to calculate and can’t able draw the original line.

then how it will put pixel if it doesn’t have the actual x co-ordinate or y -coordinate in the above program as integer.
becuase for y=y0+m will give us the floating point and similarly
if(dy>dx)then
x=x0+1/m will yield a fractional value .but in the above program
you didn’t round of to the nearest pixel .
then how can you put (x,y) on the screen.

4. musty says:

please help me…..jab b me is code ko run krn to aik warning deta hay k (function should have a returned value) and jab m int main() ki jaga void mein likhn to warning to remove ho jati hay but output ni ati just points get kr k wapis coding screen p a jata ha plzzzzzzzzzzzz help me

1. JJJ says:

Curly brackets se pehle return(0) function likhna hai!!!

2. KDKD says:

make it void main instead of int main

3. shashwat says:

#include
#include

5. ganesh says:

how the pixel is genrated

6. megha says:

include conio.h
den only it will support getch() function…nd den output will be shown on the screen

7. ameer says:

program is right
the only thing which you have to do
include two libraries
conio.h for getch();
dos.h for using delay(100);

1. Ramya Thangakumar says:

What is dos.h???for what we use this library??

8. naaz says:

why used libarary in progarams

9. Jitender says:

I want to run exe file of graphics program that I have created from turbo c in windows 7. Please help me.

1. Kaush says:

install dosbox and open exe on dosbox.exe file not open at windows os.Its open in dos os.So use dosbox.

10. rohit says:

this is right program but run time it is not gives satisfied output.

11. Md. Muzammil Hussain says:

when i, input four values and enter, it reply a message
” muzammil.exe stoped working “. please tell me how can i solve this problem.

12. Daniel says:

The DDA Line algorithm presented here is much easier to work with in Qbasic or the modern QB64. No library files are required, as it simply just works. Here the code is modified from C.
Hold down a key on your keyboard to make the program draw a line while it is running. This feature is from the SLEEP statement included in the program.
Qbasic64:

SCREEN 12
x1 = 20: y1 = 100
x2 = 120: y2 = 220

PSET (x1, y1), 11
PSET (x2, y2), 11

dx = ABS(x2 – x1)
dy = ABS(y2 – y1)

IF dx >= dy THEN
pixel = dx
ELSE
pixel = dy
END IF

dx = dx / pixel
dy = dy / pixel

x = x1
y = y1

i = 1

WHILE i <= pixel
SLEEP
PSET (x, y), 10
x = x + dx
y = y + dy
i = i + 1

WEND

13. kurude sanket says:

it works with some points but when i give input as (300,50)and(100,400).it doesn’t.i want a generalised code pls provide it. . . .as soon . . .

14. rishi says:

when we run the progrm nd it tke all the four values but after it the screen disapear. bt when i press alt+f5, it show the massage -“graphics is not initialize”, plse help me any one….

15. sandip acharya says:

BGI error….

16. dinesh says:

BGI ERROR(use’initgraph’)

17. Shubhra says:

the graphics are not generating.How to generate it…???

18. prashant mehariya says:

plzzzzzzz muje computer graphics nhi aati h

19. ratnesh vishwakarma says:

I want to run program but it can’t run. When i run it then it go reverse to the program page. What can I do?

when I type this code in turbocpp . it doesn’t show any errors or warning but it doesn’t show any O/P on screen.

add these things than run 100% run:-

#include
#include
REPLACE
initgraph(&gd,&gm,””);
BY BELOW CODE
initgraph(&gd, &gm, “C:\\TurboC3\\BGI”);
ENJOY@@@