0% found this document useful (0 votes)
119 views

Ass1 PDF

This document describes an assignment to implement line drawing algorithms in C++ or Java. It covers the Digital Differential Analyzer (DDA) algorithm and Bresenham's algorithm for drawing lines on a digital display. The DDA algorithm uses slope to calculate intervals to plot successive points along the line, while Bresenham's algorithm uses integer calculations and decision variables to optimize pixel selection for the line raster. Pseudocode and flowcharts are provided to illustrate the steps of each algorithm. The objectives are to understand and programmatically implement these common line drawing techniques.

Uploaded by

Pooja Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
119 views

Ass1 PDF

This document describes an assignment to implement line drawing algorithms in C++ or Java. It covers the Digital Differential Analyzer (DDA) algorithm and Bresenham's algorithm for drawing lines on a digital display. The DDA algorithm uses slope to calculate intervals to plot successive points along the line, while Bresenham's algorithm uses integer calculations and decision variables to optimize pixel selection for the line raster. Pseudocode and flowcharts are provided to illustrate the steps of each algorithm. The objectives are to understand and programmatically implement these common line drawing techniques.

Uploaded by

Pooja Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Computer graphics Lab (2015 pattern) SE COMPUTER

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.

2.Objectives : Identify the Line Drawing algorithms of computer graphics.

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

Similarly for given ∆y we can calculate corresponding ∆x as


∆x= x2-x1/y2-y1 * ∆y

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.

Department of Computer Engineering , LoGMIEER, Nashik 2


Computer graphics Lab (2015 pattern) SE COMPUTER

3.3 FLOWCHART:

3.4 Bresenham’s Line Drawing Algorithm:

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.

Department of Computer Engineering , LoGMIEER, Nashik 3


Computer graphics Lab (2015 pattern) SE COMPUTER

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

[Initialize value of decision variable or error to compensate for nonzero intercepts]


4. I=1 [Initialize counter]
5. Plot (x, y)
6. While (e>=0)
{ y=y+1
e=e-2*∆x
}
x=x+1
e=e+2*∆y
7. I=I+1
8. If ( I<=∆x) then go to step 6.
9. Stop
3.6 FLOWCHART:

Conclusion:

Department of Computer Engineering , LoGMIEER, Nashik 5


Computer graphics Lab (2015 pattern) SE COMPUTER

In this assignment we have studied computer graphics programs in C++ using the line
drawing algorithms such as DDA and Bresenham's algorithm.

Department of Computer Engineering , LoGMIEER, Nashik 6

You might also like