Advantages and Disadv of DDA Algorithm

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

Advantages of DDA Algorithm-

The advantages of DDA Algorithm are-

 It is a simple algorithm.
 It is easy to implement.
 It avoids using the multiplication operation which is costly in terms of time complexity.
 

Disadvantages of DDA Algorithm-


 

The disadvantages of DDA Algorithm are-

 There is an extra overhead of using round off( ) function.


 Using round off( ) function increases time complexity of the algorithm.
 Resulted lines are not smooth because of round off( ) function.
 The points generated by this algorithm are not accurate.

Advantages : It is simple and easy to implement algorithm. It avoid using multiple operations


which have high time complexities. It is faster than the direct use of the line equation because it
does not use any floating point multiplication and it calculates points on the line

Disadvantages of DDA Algorithm-

There is an extra overhead of using round off( ) function. Using round off( ) function increases
time complexity of the algorithm. Resulted lines are not smooth because of round off( )
function. The points generated by this algorithm are not accurate.

Bresenham Algorithm

 Bresenham Algorithm was developed by J.E.Bresenham in 1962 and it is much


accurate and much more efficient than DDA.
 It scans the coordinates but instead of rounding them off it takes the incremental
value in account by adding or subtracting and therefore can be used for drawing
circle and curves.
 Therefore if a line is to be drawn between two points x and y then next
coordinates will be( xa+1, ya) and (xa+1, ya+1) where a is the incremental value
of the next coordinates and difference between these two will be calculated by
subtracting or adding the equations formed by them.

Comparision

 DDA uses floating points where as Bresenham algorithm use fixed points.
 DDA round off the coordinates to nearest integer but Bresenham algorithm does
not.
 Bresenham algorithm is much accurate and efficient than DDA.
 Bresenham algorithm can draw circles and curves with much more accuracy than
DDA.
 DDA uses multiplication and division of equation but Bresenham algorithm uses
subtraction and addition only

- Digital Differential Bresenhams Line Drawing Algorithm


Analyzer,Line Drawing
Algorithm

Arithmetic DDA algorithm uses floating Bresenhams algorithm uses fixed points
points i.e. Real Arithmetic i.e. integer Arithmetic.

Operations DDA algorithms uses Bresenhams algorithm uses only


multiplication and division in its subtraction and addition in its
operations.

Speed DDA algorithm is rather slowly Bresenhams algorithm is faster than DDA
than Bresenhams algorithm in algorithm in line drawing because it
line drawing because it uses real performs only addition and subtraction in
arithmetic (floating point its calculations and uses only integer
operations). arithmetic so it runs significantly faster.
- Digital Differential Bresenhams Line Drawing Algorithm
Analyzer,Line Drawing
Algorithm

Accuracy & DDA algorithm is not as accurate Bresenhm algorithm is more accurate and
Efficiency and efficient as Bresenhm efficient as than DDA algorithm.
algorithm.

Drawing DDA algorithm can draw circles Bresenhm algorithm can draw circles and
and curves but that are not as curves with much more accuracy than DDA
accurate as Bresenhm algorithm. algorithm.

Round off DDA algorithm round off the Bresenhm algorithm does not round off but
coordinates to integer that is takes the incremental value in its
nearest to the line. operation.

- - Bresenhm algorithm is less expensive than


DDA algorithm as it uses only addition and
subtraction.

Comparions between DDA and Bresenham


Line Drawing algorithm
 Last Updated : 03 Jun, 2019
In lighting tricks, there are 2 algorithmic rules used for drawing a line over the screen
that’s DDA stands for Digital Differential Analyser algorithmic rule and Bresenham
line algorithm.

The main distinction between DDA algorithm and Bresenham line algorithm is that, the
DDA algorithmic rule uses floating purpose values whereas in Bresenham, spherical off
functions is used.
DDA algorithmic rule involves multiplication as well as division whereas in bresenham
algorithmic rule, addition and subtraction are the most performed operations.
Let’s see that the difference between DDA algorithm and Bresenham line drawing
algorithm:

S.NO DDA Line Algorithm Bresenham line Algorithm

DDA stands for Digital Differential


1. Analyzer. While it has no full form.

DDA algorithm is less efficient than While it is more efficient than DDA
2. Bresenham line algorithm. algorithm.

The calculation speed of DDA While the calculation speed of


algorithm is less than Bresenham Bresenham line algorithm is faster
3. line algorithm. than DDA algorithm.

DDA algorithm is costlier than While Bresenham line algorithm is


4. Bresenham line algorithm. cheaper than DDA algorithm.

DDA algorithm has less precision While it has more precision or


5. or accuracy. accuracy.
S.NO DDA Line Algorithm Bresenham line Algorithm

In DDA algorithm, the complexity While in this, the complexity of


6. of calculation is more complex. calculation is simple.

In DDA algorithm, optimization is While in this, optimization is


7. not provided. provided.

Write an algorithm for direct line drawing equation?

In computer graphics, a line drawing algorithm is an algorithm for approximating a


line segment on discrete graphical media, such as pixel-based displays and printers.
On such media, line drawing requires an approximation (in nontrivial cases). Basic
algorithms rasterize lines in one color.

Computer Graphics | Direct Use of Line Equation: In this tutorial, we are going to learn
about the Direct Use of Line Equation in Computer Graphics, 2D line and properties of good
line drawing algorithms.
Submitted by Monika Sharma, on April 15, 2020

Computer Graphics | Direct Use of Line Equation

The standard line equation, as we all know is used for drawing a line. It is given by: y = mx
+ c.

We are discussing here in 2D so we all know that there are 2 axes: x and y. Both of the axes
are required to give the equation of any 2D shape. The line is a straight path joining 2
points in the x-y plane. If both the points are given then we can find the equation of a line.
The slope of a line
The slope of a line defines the direction of a line. Its value is equal to the ratio of the
difference of y coordinates and the difference. Assume that the two points
are X( x1,y1 ) and Y( x2,y2 ). Its slope, 'm' will be: m = (y2 - y1) / (x2 - x1).

Properties of Line Drawing Algorithm


The following are the properties that a line must hold in any line drawing algorithm,

i. Line must be straight


ii. Line must terminate accurately
iii. Line must have constant density
iv. Density must be independent of its length
v. Line must be drawn very fast

Line Drawing Algorithms


There are some set of rules and steps which help draw a line. These algorithms are given
below,

1. Direct Use of line equation


2. DDA (Digital Differential Analyzer)
3. Bresenham's Algorithm

Direct use of Line Equation


This is the simplest form of drawing a line. We all know that the equation of the line is y =
mx + c. Here m is slope and c is the length from origin to the point where the line cuts y-
axis. In this method, we will be having the start and endpoint of the line and by the help of
that points, we'll calculate the other points which lie on the line. We have to find the slope
of the line by using the given points.

We'll understand this better with the help of an example,

Example:

We have given two points X and Y. The coordinates of X are (0, 0) and the coordinates
of Y are (5, 15). The slope of the line will be,
m = (15 - 0) / (5-0)
m = 3

We have the slope of the line. Now let us put the slope in the line equation.

y = 3x + c

Origin point is (0,0). So,

c = 0

Putting c=0 in the above equation.

y = 3x

Now we will calculate the intermediate points.

Let x = 1 ⟹ y = 3 x 1 ⟹ y = 3
Let x = 2 ⟹ y = 3 x 2 ⟹ y = 6
Let x = 3 ⟹ y = 3 x 3 ⟹ y = 9
Let x = 4 ⟹ y = 3 x 4 ⟹ y = 12
Let x = 5 ⟹ y = 3 x 5 ⟹ y = 15

We got the intermediate points which are, (1, 3), (2, 6), (3, 9), (4, 12) and finally (5, 15)

Now we'll plot these points on the graph.


Hence, we have plotted the points that lie between the given points through the standard
line equation. By doing so with a very small gap between these pints will give us the entire
line.

ADVERTISEMENT

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 #include <graphics.h>

2 #include <stdio.h>

3 #include <math.h>

4 #include <dos.h>

5  

6 void main( )

7 {

8 float x,y,x1,y1,x2,y2,dx,dy,step;
9 int i,gd=DETECT,gm;

10  

11 initgraph(&gd,&gm,"c:\\turboc3\\bgi");

12  

13 printf("Enter the value of x1 and y1 : ");

14 scanf("%f%f",&x1,&y1);

15 printf("Enter the value of x2 and y2: ");

16 scanf("%f%f",&x2,&y2);

17  

18 dx=abs(x2-x1);

19 dy=abs(y2-y1);

20  

21 if(dx>=dy)

22 step=dx;

23 else

24 step=dy;

25  

26 dx=dx/step;

27 dy=dy/step;

28  

29 x=x1;

30 y=y1;

31  

32 i=1;

33 while(i<=step)

34 {

35 putpixel(x,y,5);

36 x=x+dx;

37 y=y+dy;

38 i=i+1;

39 delay(100);

40 }

41  
42 closegraph();

43 }

Outptut

Program for DDA Line Drawing Algorithm in C++

1 #include <graphics.h>

2 #include <iostream.h>

3 #include <math.h>

4 #include <dos.h>

5  

6 void main( )

7 {

8 float x,y,x1,y1,x2,y2,dx,dy,step;

9 int i,gd=DETECT,gm;

10  

11 initgraph(&gd,&gm,"c:\\turboc3\\bgi");

12  

13 cout<<"Enter the value of x1 and y1 : ";

14 cin>>x1>>y1;

15 cout<<"Enter the value of x2 and y2: ";

16 cin>>x2>>y2;
17  

18 dx=abs(x2-x1);

19 dy=abs(y2-y1);

20  

21 if(dx>=dy)

22 step=dx;

23 else

24 step=dy;

25  

26 dx=dx/step;

27 dy=dy/step;

28  

29 x=x1;

30 y=y1;

31  

32 i=1;

33 while(i<=step)

34 {

35 putpixel(x,y,5);

36 x=x+dx;

37 y=y+dy;

38 i=i+1;

39 delay(100);

40 }

41  

42 closegraph();

43 }

You might also like