UNIT3
UNIT3
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));
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
• 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 −
Multiplication and division are the Only addition and subtraction are
Method
only operations used. used in this process
It does not have a high degree of It's extremely accurate and precise.
Precision
accuracy or precision.
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
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
2. A round join is produced by capping the connection between the two segments with a
3. A bevel join is generated by displaying the line segment with but caps and filling in tri