Sunteți pe pagina 1din 23

Unit 1 Chapter 2 Scan-conversion aalgorithms of computer graphics primitives

By: Ms. Neha Mukesh Srivastava

Syllabus
Scan-Conversion of a Lines
Digital Differential Analyzer Algorithm Bresenhams Line-Drawing Algorithm

Scan-Conversion of Circle and Ellipse


Bresenhams Method of Circle Drawing Midpoint Circle Algorithm

Drawing Ellipses and Other Conics.


2 Computer Graphics - Prof. Neha M. Srivastava

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

Scan conversion of lines


Lines, or more accurately segments, are the most basic of computer graphics objects. The display system of a computer is a two-dimensional array consisting of rows and columns. The primary objective of scan-conversion is to determine the intersection of rows and column to find the area, called pixels, to paint any object. A line can be described as an infinitely thin, infinitely long, and perfectly straight, containing an infinite number of points. In Euclidean geometry, exactly one line can be found that passes through any two points. The line provides the shortest connection between the points. Each line should appear to have an even visual thickness, that is, it should have as constant a density as possible, and this thickness should be independent of its length and slope.
4 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

Digital differential analyzer algorithm DDA


Digital differential analyzer (DDA) is an incremental scan-conversion method to determine points on a line. It requires calculating the difference form the previous step calculations. The differences are x and y in x and y directions respectively. These determine the direction of the movement and convergence.
6 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.

Computer Graphics - Prof. Neha M. Srivastava

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

Computer Graphics - Prof. Neha M. Srivastava

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

Computer Graphics - Prof. Neha M. Srivastava

Bresenhams line drawing algorithm


Although the DDA algorithm for drawing straight lines is simple, they involve real arithmetic. Some simple modifications result in an algorithm that does only integer arithmetic, and only involves additions. The Bresenham's line algorithm is an algorithm that determines which points in an n-dimensional raster should he plotted 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. This algorithm provides the means for the fast and efficient way to represent continuous abstract lines onto a discrete plane of computer display. The algorithm basically approximates a real valued line by calculating what pixels to illuminate and to provide illusion of the line. Since the pixels are sufficiently small, the approximation is good enough to trick the human eyes and get illusion of a real line. To proceed with the algorithm, it always increments in either x or y by one unit based on the slope of the line. The increment factor in the other variable is deter mined by calculating the error distance between the actual line and the nearest pixel plot.
12 Computer Graphics - Prof. Neha M. Srivastava

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

Computer Graphics - Prof. Neha M. Srivastava

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

Step 3: [Initialize the start and error term]


e = ( y / x) (1 / 2) = (4 / 8) (1 / 2) = 0 and x = x1 = 0 and y = y1 = 0

Step 4: [Determine the new pixels on the line]


15 Computer Graphics - Prof. Neha M. Srivastava

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 -1 -0.5 -0.5 -0.5 0 0 -1 -0.5 -0.5 -0.5 0 0 -1 -0.5

(0,0) (1,1) (2,1) (3,2) (4,2) -

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

-0.5 -0.5 0 0 -1 -0.5 -0.5 -0.5 0 0 -1 -0.5

(5,3) (6,3) (7,4) (8,4) -

5 5 6 6 6 7 7 7 8 8 8 9

3 3 3 3 4 4 4 4 4 4 5 5

Computer Graphics - Prof. Neha M. Srivastava

Scan-conversion of circle and ellipse


Euclidean geometry defines a circle as a set of all points in a plane at a fixed distance (called a radius) from a given point (called the center). Circles are simple closed curves, which divide the plane into an interior and exterior part. A circle is a symmetric figure. This property of symmetry of the circle could be utilized to scan-convert the circle by plotting eight points for each point on the circle for eight octants in a coordinate system. The entire circle could be produced by just computing first octant and then repeated reflection of the point about each 45o axis. It produces seven more points on the circumference of the circle. Hence it is sufficient to calculate only one octant. Before we proceed with the circle drawing algorithms, we will describe the mathematical structure of the circle. A circle centered at the origin can be mathematically defined by either polynomial or a trigonometric method . Firstly, any circle centered at the origin can be described by a send-order polynomial equation given by r2 = x2 + y2 Where x and y are the x and y coordinates and r is the radius of the circle.
Computer Graphics - Prof. Neha M. Srivastava

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

Bresenhams method for circle drawing


Method of Bresenham uses scan conversion with only integer operation like addition, subtraction, and multiplication, rather than trigonometric functions. Therefore, the method proves to be faster and efficient for real-time implementation. Bresenham considered the eight-way symmetry of the circle and generates the points to plot only 1/8th of the circle between 90 and 45, which increments x in positive and y in negative direction respectively. Let P(xi, yi)be a point on the circumference of the circle just now generated. As the direction of the circle generation is from 90o to 45, the next point on the circumference can b R(xi+1 , yi) or S(xi+1 , yi-1). A pixel R or S chosen next will be increment in +x by one unit, or it will be increment in +x and y simultaneously. To choose among these two, the best approximation is to find out the pixel or the point that falls nearest to the true circle.

19

Computer Graphics - Prof. Neha M. Srivastava

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

Computer Graphics - Prof. Neha M. Srivastava

Midpoint circle algorithm


The midpoint subdivision circle algorithm also utilizes the eight-point symmetry about the origin in the respective octants. It is also similar to the Bresenham's circle algorithm but rather chooses the pixel approximation based on the spatial relationship between the arbitrary point (x, y) and the radius of the circle about the origin. As we draw the circle again from 90o to 45o, the circle moves positive in x and negative in y directions.

21

Computer Graphics - Prof. Neha M. Srivastava

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 2: Initialize the start pixel on tht circle


x = 0 and y = r

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

Computer Graphics - Prof. Neha M. Srivastava

The End

23

Computer Graphics - Prof. Neha M. Srivastava

S-ar putea să vă placă și