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

UNIT3

The document discusses different algorithms for drawing lines on a computer screen, including the Digital Differential Analyzer (DDA) algorithm and Bresenham's line algorithm. It provides pseudocode and examples of how each algorithm works. The key differences between DDA and Bresenham's algorithm are that DDA uses floating point calculations while Bresenham's uses only integers, making it faster. Bresenham's algorithm is also more precise.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
240 views

UNIT3

The document discusses different algorithms for drawing lines on a computer screen, including the Digital Differential Analyzer (DDA) algorithm and Bresenham's line algorithm. It provides pseudocode and examples of how each algorithm works. The key differences between DDA and Bresenham's algorithm are that DDA uses floating point calculations while Bresenham's uses only integers, making it faster. Bresenham's algorithm is also more precise.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

FRAME BUFFER

A frame buffer is a large, contiguous piece of computer memory. At a minimum  there is


one memory bit for each pixel in the rater; this amount of memory is called a bit  plane. The
picture is built up in the frame buffer one bit at a time.

Line Drawing Algorithms


How does computer draw line?
• Screen made of pixels
• High-level language specifies line
• System must color pixels

Line Drawing Algorithms


1. Digital Differential Analyzer (DDA) Algorithm
2. Bresenham’s Line Algorithm
3. VECGEN Algorithm

Digital Differential Analyzer (DDA) Algortihm


The digital differential analyzer (DDA) is a scan-conversion line algorithm based on
calculation either ∆y or ∆x
The line at unit intervals in one coordinate and determine corresponding integer values
nearest the line path for the other coordinate.
Algorithm Description:
Step 1 : Accept Input as two endpoint pixel positions
Step 2: Horizontal and vertical differences between the endpoint positions are assigned to
parameters dx and dy (Calculate dx=xb-xa and dy=yb-ya).
Step 3: The difference with the greater magnitude determines the value of parameter
steps.
Step 4 : Starting with pixel position (xa, ya), determine the offset needed at each step to
generate the next pixel position along the line path.
Step 5: loop the following process for steps number of times
a. Use a unit of increment or decrement in the x and y direction
b. if xa is less than xb the values of increment in the x and y directions are 1 and m
c. if xa is greater than xb then the decrements -1 and – m are used.

DDA Pseudo-code
//assume that slope is gentle
DDA(float x0, float x1, float y0, float y1) {
float x, y;
float xinc, yinc;
int numsteps;
numsteps = Round(x1) – Round(x0);
xinc = (x1 – x0) / numsteps;
yinc = (y1 – y0) / numsteps;
x = x0;
y = y0;
ColorPixel(Round(x),Round(y));

for (int i=0; i<numsteps; i++) {


x += xinc;
y += yinc;
ColorPixel(Round(x),Round(y));
}
}
• Suppose we want to draw a line starting at pixel (2,3) and ending at pixel (12,8).
• What are the values of the variables x and y at each timestep?
• What are the pixels colored, according to the DDA algorithm?

numsteps = 12 – 2 = 10
xinc = 10/10 = 1.0
yinc = 5/10 = 0.5

t x y R(x) R(y)

0 2 3 2 3

1 3 3.5 3 4

2 4 4 4 4

3 5 4.5 5 5

4 6 5 6 5

5 7 5.5 7 6

6 8 6 8 6

7 9 6.5 9 7

8 10 7 10 7

9 11 7.5 11 8

10 12 8 12 8
Advantages of DDA Algorithm
1. It is the simplest algorithm
2. It is a is a faster method for calculating pixel positions

Disadvantages of DDA Algorithm


1. Floating point arithmetic in DDA algorithm is still time-consuming
2. End point accuracy is poor
Bresenham’s Algorithm
• Uses only integer calculations
• Uses distance between ideal y-coordinate and the upper and lower pixel (assuming
gentle slope)

• Suppose that the line is gently sloping upwards from left to right.
• Start by coloring the left-most pixel.
• Then, for the next column (that is, for each x value), we have to figure out whether we
color the same y or y+1.
• How do we decide?
– When going from one column to the next, add an error value. If the error value
is more than 0.5, we should color y+1 and reset the error value. Otherwise,
color y and accumulate the error value.
• However, it seems like we’re still using floating point
– Solution, multiply both sides by 2 so that we use integer comparisons instead.
An accurate and efficient raster line generating algorithm developed by Bresenham, that uses
only incremental integer calculations.
In addition, Bresenham’s line algorithm can be adapted to display circles and other
curves.
To illustrate Bresenham's approach, we- first consider the scan-conversion process for
lines with positive slope less than 1.
Pixel positions along a line path are then determined by sampling at unit x intervals.
Starting from the left endpoint (x0,y0) of a given line, we step to each successive column
(x position) and plot the pixel whose scan-line y value is closest to the line path.

Bresenham’s Algorithm
1. Input the two line endpoints and store left endpoint as
(x0,y0)
2. Pre-calculate the values dx, dy, 2dy and 2dy - 2dx
3. Color pixel (x0,y0)
4. Let p0 = 2dy – dx
5. At each xk along the line, starting with k=0:
6. Repeat Step-4 dx times
Switch Point 0 and Point 1 if necessary
If negative slope, reflect
If steep slope, flip y and x
Bresenham’s Algorithm Example
• Suppose we want to draw a line starting at pixel (2,3) and ending at pixel (12,8).
• What are the values of p0, dx and dy?
• What are the values of the variable p at each timestep?
• What are the pixels colored, according to Bresenham’s algorithm?
dx = 12 – 2 = 10
dy = 8 – 3 = 5
2dy = 10
2dy – 2dx = -10
p0 = 2dy – dx = 15

t p P(x) P(y)

0 0 2 3

1 -10 3 4

2 0 4 4

3 -10 5 5

4 0 6 5
5 -10 7 6

6 0 8 6

7 -10 9 7

8 0 10 7

9 -10 11 8

10 0 12 8

Advantages
1. Algorithm is Fast
2. Uses only integer calculations
Disadvantages
1. It is meant only for basic line drawing.
Difference between DDA and Bresenham’s Algorithm
The following table highlights the major differences between DDA and Bresenham's Algorithm −

Basis of DDA Bresenham’s Algorithm


comparison

Multiplication and division are the Only addition and subtraction are
Method
only operations used. used in this process

In comparison to the Bresenham Despite the fact that it is more


Efficiency line algorithm, the DDA approach effective than the DDA algorithm.
is not as efficient.

The computation speed of the However, the computation speed of


Bresenham line method is the Bresenham line algorithm is
Speed
significantly higher than that of the significantly faster than that of the
DDA algorithm. DDA algorithm.

It does not have a high degree of It's extremely accurate and precise.
Precision
accuracy or precision.

In order to complete its tasks, it In order to do its tasks, it performs


Complexity
relies on complex calculations. basic calculations.

It comes at a high cost. It is on the lower end of the price


Price
range.

It prevents optimization from It enables optimization.


Optimization
happening.
Line Attributes

Basic attributes of a straight line segment are its type, its width, and its color. In some

graphics packages, lines can also be displayed using selected pen or brush options

 Line Type
 Line Width
 Pen and Brush Options
 Line Color

Line Cap

We can adjust the shape of the line ends to give them a better appearance by adding line

caps. There are three types of line cap. They are

1. Butt cap
2. Round cap
3. Projecting square cap

Butt cap obtained by adjusting the end positions of the component parallel lines so that

the thick line is displayed with square ends that are perpendicular to the line path.

Round cap obtained by adding a filled semicircle to each butt cap. The circular arcs are

centered on the line endpoints and have a diameter equal to the line thickness

Projecting square cap extend the line and add butt caps that are positioned one-half of the line
width beyond the specified endpoints. Three possible methods for smoothly joining two line
segments

1. Mitter Join
2. Round Join
3. Bevel Join
1. A miter join accomplished by extending the outer boundaries of each of the two lines

until they meet.

2. A round join is produced by capping the connection between the two segments with a

circular boundary whose diameter is equal to the width.

3. A bevel join is generated by displaying the line segment with but caps and filling in tri

angular gap where the segments meet

You might also like