Direct Use of Line Equation in Computer Graphics
Direct Use of Line Equation in Computer Graphics
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.
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.
Properties of Line Drawing Algorithm
The following are the properties that a line must hold in any line drawing algorithm,
There are some set of rules and steps which help draw a line. These algorithms are given below,
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.
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
y = 3x + c
Origin point is (0,0). So,
c=0
y = 3x
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)
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.
write a code for direct line drawing algorithm?
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)
}
Write Advantages and disadvantages of DDA Algorithm and Compare Bresenham 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.
The disadvantages of DDA Algorithm are-
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
Arithmetic DDA algorithm uses floating points i.e. Bresenhams algorithm uses fixed points i.e. integer
Real Arithmetic Arithmetic.
Operations DDA algorithms uses multiplication and Bresenhams algorithm uses only subtraction and addition
division in its operations. in its
Speed DDA algorithm is rather slowly than Bresenhams algorithm is faster than DDA algorithm in
Bresenhams algorithm in line drawing line drawing because it performs only addition and
because it uses real arithmetic (floating subtraction in its calculations and uses only integer
point operations). arithmetic so it runs significantly faster.
Accuracy DDA algorithm is not as accurate and Bresenhm algorithm is more accurate and efficient as
& efficient as Bresenhm algorithm. than DDA algorithm.
Efficiency
Drawing DDA algorithm can draw circles and Bresenhm algorithm can draw circles and curves with
curves but that are not as accurate as much more accuracy than DDA algorithm.
Bresenhm algorithm.
Round off DDA algorithm round off the coordinates Bresenhm algorithm does not round off but takes the
to integer that is nearest to the line. incremental value in its operation.