Ass1 PDF
Ass1 PDF
Assignment No. 1
1.Title :- Write C++/Java program to draw line using DDA and Bresenham’s algorithm.
Inherit pixel class and Use function overloading.
3. Theory:
3.1 Digital Differential Analyzer (DDA) :-DDA Algorithm generates a line from
differential equation of line
The slope(m) of a straight line is given by
m= y2-y1/x2-x1 = ∆y /∆x
where (x1,y1) and (x2,y2) are two end points of a line.
For any given x interval(∆x) we can calculate corresponding y interval(∆y)
∆y=y2-y1/ x2-x1 *∆x
Once the intervals are known the values for next x and next y on straight line can
be obtained as
and
x i+1 =x i+1 +∆x -eq (1)
y i+1 =y i+1 +∆y -eq (2)
For simple DDA either ∆x or ∆y whichever is larger is chosen as one raster unit
i.e.
if |∆x|>=|∆y| then
length=∆x
Else
length=∆y
3.2 ALGORITHM:
1. Read the line end points(x1,y1) and (x2,y2) such that they are not equal [if equal
then plot that point and exit]
2. ∆x=|x2-x1| and ∆y=|y2-y1|
3. If(∆x>=y) then
Department of Computer Engineering , LoGMIEER, Nashik 1
Computer graphics Lab (2015 pattern) SE COMPUTER
length=∆x
else
length=∆y
end if
4. ∆x=(x2-x1) /length
∆y=(y2-y2)/length
5. x=x1+0.5* sign(∆x)
y=y1+0.5 * sign(∆y)
[Here sign function makes algorithm works in all quadrants
returns -1,0,1 when argument is <0,=0, >0 resp.]
6. i=1
while (i<=length)
{
plot(integer(x),integer(y))
x=x+∆x
y=y+∆y
i=i+1
}
7. Stop.
Advantages of DDA Algorithm:
- Simple to implement
- It is faster method for calculating pixel positions than the direct use of equation
y=mx+b. It eliminates the multiplication in the equation by making use of raster
characteristics, so that appropriate increments are applied in the x or y direction to
find the pixel positions along the line path.
Disadvantages of DDA Algorithm:
One serious drawback of the DDA algorithm is that it is very time consuming as it
deals with a rounding off operation and floating point arithmetic. Moreover
successive addition of floating point increment causes accumulation of round-off
error and eventually a drift away of the plotted pixels from the true line path in
case of long line segments.
3.3 FLOWCHART:
The Bresenham algorithm is another incremental scan conversion algorithm. The big
advantage of this algorithm is that, it uses only integer calculations. Moving across the x
axis in unit intervals and at each step choose between two different y coordinates.
The basic principle of Bresenham’s line algorithm is to select the optimum raster
locations to represent a straight line. To get this the algorithm always increments either x
or y by one unit depending on the slope of line. The increment in the other variable is
determined by examining the distance between the actual line location and the
nearest pixel. This distance is called decision variable or the error. In mathematical terms
decision variable or the error is defined as
E= DB -DA or D A - D B
If e>0, then it implies that D B > D A , i.e. the pixel above the line is closer to the true line.
If
D B < D A , then we can say that the pixel below the line is closer to the true line.
The error term is initially set as
e=2*∆y-∆x
Where ∆y=y2-y1,
∆x=x2-x1
Then according to value of e following actions are taken.
While (e>=0)
{
y=y+1
e=e-2*∆x
}
x=x+1
e=e+2*∆y
3.5 ALGORITHM:
1. Read the line end points (x1, y1) and (x2, y2) such that they are not equal.
(If equal then plot that point and exit)
2. ∆x = | x2-x1| and ∆y = |y2-y1|
3. [Initialize starting point]
x=x1
y=y1
4. e=2*∆y-∆x
Department of Computer Engineering , LoGMIEER, Nashik 4
Computer graphics Lab (2015 pattern) SE COMPUTER
Conclusion:
In this assignment we have studied computer graphics programs in C++ using the line
drawing algorithms such as DDA and Bresenham's algorithm.