Documente Academic
Documente Profesional
Documente Cultură
Syllabus
Scan-Conversion of a Lines
Digital Differential Analyzer Algorithm Bresenhams Line-Drawing Algorithm
Introduction
The process to determine which pixel provides the best approximation to shape the object is called rasterization, and when such procedure is combined with picture generation using scan line, it is called scanconversion. A point is a geometric element that is defined by it coordinates. Mathematically, a point P in space has coordinates x and y represented by P(x, y). It is important to note that a point coordinates have integral values in order to display it on the screen coordinates, as a display system requires no floating point coordinates. If one generates such coordinates then it must be converted to its nearest integer part. For example, a point with coordinates P(1.3, 4.2) has to be converted to P( 1,4) for display using suitable functions in any programming environment.
3 Computer Graphics - Prof. Neha M. Srivastava
The choice of pixel on the line or a curve is difficult, because the screen resolution may not be a perfect square in dimension - equal horizontal and vertical width. But this is not the case. A line along the diagonal is straight, but its width is not constant. This is because each pixel is visualized to be a square element in display. Hence, it requires a careful introspection in determining the next pixel in the sequence to be painted to give it a straight uniform look. A line is a graphic element that is defined by its two end points and be given by the equation y = mx + c, where m is the slope of the line and c is the intercept on the y-axis. This equation describes all the points that lie on this line. Therefore, a line drawing requires determining such points that lie on it. This process is incremental in nature. Most (straight) lines and curves drawn in computer graphics satisfy equating a function of x and y with zero. For example, we can re-express the line equation y = mx + c as 0 = mx + c y.
5 Computer Graphics - Prof. Neha M. Srivastava
Step 1:
Input the coordinates of the two end points A(x1,y1) and B(x2, y2) for the line AB respectively. Note that points A and B are not equal. (If they are equal, then it is a point. Plot the point and return.)
Step 2:
[Calculate dx and dy] dx = (x2 x1) and dy = (y2 y1)
Step 3:
[Calculate the length L] If abs (x2 x1) >= abs (y2 y1) then L = abs (x2 x1) Else L = abs (y2 y1)
Step 4:
[Calculate the increment factor] x = (x2 x1)/L and y = (y2 y1)/L This step makes either x or y equal to 1, because L is either |x2 x1| or |y2 y1|. Therefore, a step increment in x or y direction is equal to 1.
Step 5:
[Initialize the initial point on the line and plot] xnew = x1 + 0.5 and ynew = y1 + 0.5 Plot (Integer (xnew), Integer (ynew)) The values are rounded using the factor of 0.5 rather than truncating, so that the central pixel addressing is handled correctly. Moreover, the sign function given makes the algorithm work in all the quadrants.
Step 6: [Obtain the new pixel on the line and plot the same]
Initialize i to 1 While (i <= L) { xnew = xnew + x ynew = ynew + y Plot (Integer (xnew), Integer (ynew)) i=i+l }
Step 7:
Finish
Procedure lineDDA(xa, ya, xb, yb : integer); Var dx, dy, steps, k : integer; xIncrement, yIncrement, x, y : real; Begin dx = xb xa; dy = yb ya; if abs(dx) >= abs(dy) then steps = abs(dx) else steps = abs(dy) xIncrement = dx / steps; yIncrement = dy / steps; x = xa; y = ya; setpixel(round(x), round(y), color); for k = 1 to steps do begin x = x + xIncrement y = y + yIncrement setpixel(round(x), round(y), color); end End;(lineDDA)
9 Computer Graphics - Prof. Neha M. Srivastava
Example 1
Consider a line AB with A = (0,0) and B = (8, 4). Apply a simple DDA algorithm and calculate the pixels on this line. Solution: Trace for the algorithm for line AB. Step 1: [Initialize the inputs] x1 = 0 and y1 = 0 and x2 = 8 and y2 = 4 Step 2: [Calculate dx and dy] dx = x2 x1 = 8 - 0 = 8 and dy = y2 y1 = 4 0 = 4 Step 3: [Calculate length estimate] L = |dy| = 8 as dx > dy Step 4: [Calculate increment factors in x and y directions] xIncrement = dx/L = 8/8 = 1 and yIncrement = dy/L = 4/8 = 0.5 Step 5: [Initialize the points] xnew = xl + 0.5 = 0 + 0.5 = 0.5 and ynew = y1 + 0.5 = 0 + 0.5 = 0.5. Plot (0, 0)
10 Computer Graphics - Prof. Neha M. Srivastava
Step 6: [Obtain the new pixel on the line and plot the same] Initialize i to 1 While (i <= L) { xnew = xnew + xIncrement ynew = ynew + yIncrement Plot (Integer (xnew), Integer (ynew)) i=i+l } Step 7: Finish Fig. shows a screen plot of the line AB.
i 1 2 3 4 5 6 7 8 9
Plot (0,0) (1,1) (2,1) (3,2) (4,2) (5,3) (6,3) (7,4) (8,4)
Xnew 0.5 1.5 2.5 3.5 4.5 505 6.5 7.5 8.5
Ynew 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5
11
The algorithm is based on whether the predefined error term is positive or negative. There fore, the algorithm only examines the error terms to determine the direction of the movement of the line. The line segment will lie in one of the eight octets. This will define the direction of movement of the line segment, which can be towards positive x-axis, positive y-axis, negative x-axis, or negative y-axis.
The algorithm plots the points, which approximate the line in direction that is perpendicular to the axis towards which the line is sloping. Thus for the line in a first octet, the algorithm plots the points and moves in the positive y axis. This is done as long as the error term is non-negative. When the error term is negative, the algorithm plots the points in the direction of line and updates the error term until it becomes non-negative again. The algorithm assumes the two end points to be non-equal and variable to be integer.
13
Algorithm
Step 1:
Initialize the end points of line, say, AB with A(x1, y1) and B(x2, y2). The two end points are assumed to be distinct.
Step 2:
[Calculate x and y] x = x2 x1 and y = y2 y1
Step 3:
[Initialize the point and error term to compensate for nonzero intercepts] e = ( y / x) (1 / 2) and x = x1, and y = y1,
Step 4:
[Determine the next pixel on the line and update the error term]
For i = 0 to x Plot (int (x), int (y)) while (e >= 0) y=y+1 e=e-1 End while loop x=x+1 e= y/ x+ e next i End for loop
Step 5:
Finish
14 Computer Graphics - Prof. Neha M. Srivastava
Example2
Consider the line coordinates (0, 0) and (8, 4). Rasterize the line segment using Bresenhams algorithm. Solution Step 1: [Initialize]
x1 = 0 and y1 = 0 and x2 = 8 and y2 = 4
Step 2: [Calculate
x and
y]
y = y2 y1 = 4 0 = 4
x = x2 x1 = 8 0 = 8 and
Counter i
Error term e
Plot (x, y)
Y5
Counter i
Error term e
Plot (x, y)
0 0 0 1 1 1 2 2 2 3 3 3 4 4 4
16
0 0 1 1 1 2 2 2 3 3 3 4 4 4 5
0 1 1 1 1 1 1 2 2 2 2 2 2 3 3
5 5 5 6 6 6 7 7 7 8 8 8
5 5 6 6 6 7 7 7 8 8 8 9
3 3 3 3 4 4 4 4 4 4 5 5
17
The polynomial relation between x y, and r could be used to find y coordinates from noted x coordinates. From Figure 2.12, it is evident that the sector between 90o to 45o generates x by stepping x by 0 tor 2 and y by calculating (r2 x2)for each x. The method works well, but it is a little economical since for each point squares of r and x must be found and the square root of the difference is also a must . The second approach describes the circle using trigonometric relations, where x and y coordinate of the circle are given by x = r cos and y = r sin , where is the current angle, r is the radius of the circle, x is the x coordinate, and y is the y coordinated. The method increments from 0 to 4 and calculates x and y each time. The method is again inefficient, because computation of sin and cos takes much time.
18 Computer Graphics - Prof. Neha M. Srivastava
19
Algorithm
Step 1:
Obtain the radius of the circle r.
Step 2:
Set the base decision factor =
1
= 3 2r
Step 3:
Set the base values of the coordinates x = 0 and y = r
Step 4:
[Compute the next pixels on the circle and update the decision factor] While (x <= y) Plot (x, y) if ( < 0) then = + 4x + 6 Else = + 4(x y) + 10 y = y-l End if x=x+1 End while
Step 5:
Finish
20
21
Algorithm
The following algorithm generates pixels on a circle using the midpoint method for the 90o to 45o octant. The remaining pixels for the circle may be generated by successive reflection around the eight axes. Step 1:
Input the radius of the circle. Note that radius is the distance from the origin.
Step 3:
[Compute the initial decision factor] Mi = M = (5 / 4) r
Step 4: [Compute the decision factor and determine the next pixel coordinates on the circle]
While (x <= y) Plot (x, y) if (M < 0) then M = M + 2x + 3 Else M = M + 2(x y) + 5 y=y1 End if x=x+1 End while
Step 5:
Finish
22
The End
23