Sie sind auf Seite 1von 12

EXPERIMENT NO.

Group A -1

Title: Line drawing using DDA and Bresenham’s Algorithm.

Objectives: To learn function to Draw a line using DDA and Bresenham’s Algorithm.

Problem Statement: Write a c++ class for a line drawing using DDA andBresenham’s
Algorithm, Inheriting the Pixel or Point
Software Requirement:Fedora,C++
Input: Read the X & Y co-ordinates From the Users.

Output: It Display the Line on the Screen.

Theory:

Write and Explain DDA Line Drawing Algorithm


 DDA Line Drawing Algorithm:
Digital differential analyzer (DDA) is hardware or software used for linear
interpolation of variables over an interval between start and end point. DDAs are used for
rasterization of lines, triangles and polygons.DDA algorithm is an incremental scan
conversion method. Here we perform calculations at each step using the results from the
preceding step. The characteristic of the DDA algorithm is to take unit steps along one
coordinate and compute the corresponding values along the other coordinate.

Algorithm:
1. Read the line end points (𝑥1,1) and (𝑥2,𝑦2) such that they are not equal. [If
equal then
plot that point and exit]
2.Δ𝑥 = |𝑥2 − 𝑥1|and Δ𝑦 = |𝑦2 –𝑦1|
3. If (Δ𝑥 ≥ Δ𝑦) 𝑡ℎ𝑒𝑛
Length=Δ𝑥
else
Length=Δ𝑦
end if
4. Δ𝑥 = (𝑥2 − 𝑥1)/𝑙𝑒𝑛𝑔𝑡ℎ
Δ𝑦 = (𝑦2 − 𝑦1)/𝑙𝑒𝑛𝑔𝑡ℎ
[This makes either Δ𝑥 or Δ𝑦 equal to 1 because length is either |𝑥2 − 𝑥1| or |𝑦2−
𝑦1| .
Therefore, the incremental value for either x or y is one.]

5. 𝑥 = 𝑥1 + 0.5 (Δ𝑥)

𝑌 = 𝑦1 + 0.5 (Δ𝑦)
[Here, sign function makes the algorithm work in all quadrants. It returns -
1, 0, 1
Depending on whether its argument is <0, =0, >0, respectively.]
6. i=1
[begins the loop, in this points are plotted]
While (i≤length)
{
Plot (integer(x), integer(y))
𝑥 = 𝑥 + Δ𝑥
𝑦 = 𝑦 + Δ𝑦
𝑖=𝑖+1
}
Advantages of DDA Algorithm:
1. It is the simplest algorithm and it does not require special skills for implementation.
2. It is a 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:
1. Floating point arithmetic in DDA algorithm is still time-consuming.
2. The algorithm is orientation dependent. So the end point accuracy is poor.

What is Ceil and Floor?


Ceil and Floor:

Static double ceil (double d)

The method ceil () returns the smallest double value that is greater than or equal to the
argument d, and is equal to a mathematical integer.

Static double floor (double d)


The method floor () returns the largest double value that is less than or equal to the
argument d, and is equal to a mathematical integer.

The floor and ceiling functions give you the nearest integer up or down.

Example: What is the floor and ceiling of 2.31?

The Floor of 2.31 is 2


The Ceiling of 2.31 is 3

Floor and Ceiling of Integers

What if you want the floor or ceiling of a number that is already an integer?

Example: What is the floor and ceiling of 5?

The Floor of 5 is 5
The Ceiling of 5 is 5

Here are some example values for you:


x Floor Ceiling

-1.1 -2 -1

0 0 0

1.01 1 2

2.9 2 3

3 3 3

Symbols

The symbols for floor and ceiling are like the square brackets [ ] with the top or
bottom part missing:

But prefer to use the word form: floor(x) and ceil(x)

1.6.3 Write and Explain Bresenham’s Line Drawing algorithm.


Bresenham's line algorithm

Bresenham's line algorithm is an algorithm that determines which points in an n-


dimensional raster should be plotted in order to form a close approximation to a straight line
between two given points. It is commonly used to draw lines on a computer screen, as it uses
only integer addition, subtraction and bit shifting, all of which are very cheap operations in
standard computer architectures. It is one of the earliest algorithms developed in the field of
computer graphics. A minor extension to the original algorithm also deals with drawing circles.
Algorithm:

Illustration of the result of Bresenham's line algorithm. (0,0) is at the top left corner of the grid,
(1,1) is at the top left end of the line and (11, 5) is at the bottom right end of the line.

The common conventions will be used:

 the top-left is (0,0) such that pixel coordinates increase in the right and down directions
(e.g. that the pixel at (7,4) is directly above the pixel at (7,5)), and
 that the pixel centers have integer coordinates.

The endpoints of the line are the pixels at (x0, y0) and (x1, y1), where the first coordinate of the
pair is the column and the second is the row.

The algorithm will be initially presented only for the octant in which the segment goes down and
to the right (x0 ≤ x1 and y0 ≤ y1), and its horizontal projection is longer than the vertical
projection (the line has a negative slope whose absolute value is less than 1). In this
octant, for each column x between and , there is exactly one row y (computed by the
algorithm) containing a pixel of the line, while each row between and may contain multiple
rasterized pixels.

Bresenham's algorithm chooses the integer y corresponding to the pixel center that is closest to
the ideal (fractional) y for the same x; on successive columns y can remain the same or increase
by 1. The general equation of the line through the endpoints is given by:
.

Since we know the column, x, the pixel's row, y, is given by rounding this quantity to the nearest
integer:

The slope depends on the endpoint coordinates only and can be


precomputed, and the ideal y for successive integer values of x can be computed starting from
and repeatedly adding the slope.

In practice, the algorithm can track, instead of possibly large y values, a small error value
between −0.5 and 0.5: the vertical distance between the rounded and the exact y values for the
current x. Each time x is increased, the error is increased by the slope; if it exceeds 0.5, the
rasterizationy is increased by 1 (the line continues on the next lower row of the raster) and the
error is decremented by 1.0.

In the following pseudo code sample plot(x,y) plots a point and abs returns absolutevalue:

Function:
line(x0, x1, y0, y1)
intdeltax := x1 - x0
intdeltay := y1 - y0
real error := 0
realdeltaerr := abs (deltay / deltax) // Assume deltax != 0 (line is not vertical),
// note that this division needs to be done in a way that preserves the fractional part
int y := y0
for x from x0 to x1
plot (x,y)
error := error + deltaerr
if error ≥ 0.5 then
y := y + 1
error := error - 1.0

1.6.4 Explain Line Styles & What Is The Difference Between DDA and Bresenham’s Line
Drawing Algorithm?
Syntax:
#include <graphics.h>
Void setlinestyle (intlinestyle, unsigned upattern, int thickness);
Description:

setlinestyle sets the style for all lines drawn by line, lineto, rectangle, drawpoly,
and so on.

The linesettingstype structure is defined in graphics.h as follows:

Structlinesettingstype {

intlinestyle;

unsignedupattern;

int thickness;

};

Linestyle specifies in which of several styles subsequent lines will be drawn (such as
solid, dotted, centered, dashed). The enumeration line_styles, which is defined in graphics.h,
gives names to these operators:
Name Value Description
SOLID_LINE 0 Solid line
DOTTED_LINE 1 Dotted line
CENTER_LINE 2 Centered line
DASHED_LINE 3 Dashed line
USERBIT_LINE 4 User-defined line style
Thickness specifies whether the width of subsequent lines drawn will be normal or thick.

Name Value Description


NORM_WIDTH 1 1 pixel wide
THICK_WIDTH 3 3 pixels wide

upattern is a 16-bit pattern that applies only if linestyle is USERBIT_LINE (4). In that case,
whenever a bit in the pattern word is 1, the corresponding pixel in the line is drawn in the current
drawing color. For example, a solid line corresponds to a pattern of 0xFFFF (all pixels drawn),
and a dashed line can correspond to aupattern of 0x3333 or 0x0F0F. If the linestyle parameter to
setlinestyle is not USERBIT_LINE (in other words, if it is not equal to 4), you must still provide
the upattern parameter, but it will be ignored.

Difference between DDA &Bresenhams:

Digital Differential Analyzer Bresenhams Line Drawing Algorithm


Line Drawing Algorithm
Arithmetic DDA algorithm uses floating Bresenhams algorithm uses fixed
points i.e. Real Arithmetic. points i.e. Integer Arithmetic.
Operations DDA algorithm Bresenhams algorithm uses
uses multiplication and division in its only subtraction and addition in its operations.
operations.
Speed DDA algorithm is rather slowly than Bresenhams algorithm is faster than DDA
Bresenhams algorithm in line drawing algorithm in line drawing because it performs
because it uses real arithmetic only addition and subtraction in its calculation
(floating-point operations). and uses only integer arithmetic so it runs
significantly faster.
Accuracy & DDA algorithm is not as accurate and Bresenhams algorithm is more efficient and much
Efficiency efficient as Bresenham algorithm. accurate than DDA algorithm.
Drawing DDA algorithm can draw circles and Bresenhams algorithm can draw circles and
curves but that are not as accurate as curves with much more accuracy than DDA
Bresenhams algorithm. algorithm.
Round Off DDA algorithm round off the Bresenhams algorithm does not round off but
coordinates to integer that is nearest to takes the incremental value in its operation.
the line.
Expensive DDA algorithm uses an enormous Bresenhams algorithm is less expensive than
number of floating-point DDA algorithm as it uses only addition and
multiplications so it is expensive. subtraction.

1.6.5 Consider the Line (0,0) to (4,6) use the Simple DDA Algorithm to Rasterize this Line
Solution:
x1=0 y1=0 x2=4 y2=6

Length = [𝑦2− 𝑦1] =6


Δ𝑥 = (𝑥2 − 𝑥1)/𝑙𝑒𝑛𝑔𝑡ℎ
= 4/6
and Δ𝑦 = (𝑦2 − 𝑦1)/𝑙𝑒𝑛𝑔𝑡ℎ
=6/6 = 1
Initial value for
𝑥 = 0 + 0.5 (4/6) =0.5
𝑌 = 0 + 0.5 (1)= 0.5
1.7 Mathematical Model:

S={FC,SC,Dx,Dy}
where,
S=system
FC=First Co-ordinate
SC=Second Co-ordinate
Dx=(x2-x1) &Dy=(y2-y1)

Input:x1, y1, x2, y2 is a input co-ordinates

Output: It Display the Line on the Screen.

Venn diagram:
Where,
FC=(x1, y1) :-First Co-ordinate
SC=(x2, y2) :-Second Co-ordinate
DX & DY is a Magnitude Parameter

1.8 Conclusion:

DDA and Bresenham’s Line Drawing Algorithm are understood and executed
successfully.
Oral Question Bank:
1. Explain DDA Line Drawing Algorithm.
2. Explain Bresenham’s Line Drawing algorithm
3. Explain Ceil & floor.
4. Explain line styles.
5. Explain difference between the DDA &Bresenham’s line drawing algorithms.

Das könnte Ihnen auch gefallen