You are on page 1of 16

HEMA.V Sikkim Manipal University DE Reg:No 511119346 Subject : MC0072 - Computer Graphics.

Book ID: B0810 Assignment 1 & 2

Book ID: B0810 1. Write a short note on: a) Replicating pixels b) Moving pen c) Filling area between boundaries d) Approximation by thick polyline e) Line style ad pen style.

A ) Replicating pixels : A quick extension to the scan-conversation ineer loop to write multiple pixelat each computed pixel works resonably well fo lines: Here , pixel are duplicated in columns for lines with 1 < slope < 1 and in rows for all other lines . The effect, however , is that the line endsare always vertical or horizontal , which is not pleasing for rather thick lines as shown in figure

Furthermore , lines that are horizontal and vertical have different thickness for lines at an angle ,where the thickness of the primitive is defined as the distence between the primitives boundaries parpendicular to its tangent. Thus if the thickness paramater is t , a horizontal or horizontal or verticalline has thickness t , whereas one drawn at 45o has an average thickness of This is another result of having fewer pixels in the line at an angle, as first noted in Section ; itdescreses the brightness contrast with horizontal and vertical lines of the same thickness. Stillanother problem with pixel replication is the generic problem of even-numbered width: We cannot center the duplicated column or row about the selected pixel, so we must choose a side of the primitive to have an extra pixel. Altogether, pixel replication is an efficient but crude approximation that works best for primitives that are not very thick.

b) Moving pen : Choosing a rectangular pen whose center or corner travels along the single-pixeloutline of the primitive works reasonably ell for lines; it produces the line shown in figure below Notice that this line ios similar to that produced by pixel replication but is thicker at the endpoints.As with pixel replication, because the pen stays vertically aligned, the perceived thickness of the primitive varies as a function of the primitives angle, but in the opposite way.

C) Filling area between boundaries. The width is thinnest for horizontal segments and thickest for segments with slope of . An ellipse are, for example , varies in thickness alone its entire trajectory, being of the specified thickness when the tangent is nearly horizontal or vertical, and thickened by a factor of around . this problem would be eliminated if the square turned to follow the path, but it is much better to use a circular cross-section so that the thickness is angle-independent.Now lets look at how to implement the moving-pen algorithm for the simple case of an upright rectangular or circular crosssection ( also called footprint ) so that its center or corner is at the chosen pixel; for a circular footprint and a pattern drawn in opaque mode, we must in ddition mask off the bits outside the circular region, which is not as easy task unless our low-level copy pixel has a write mask for the destination region. The bure-force copy solution writes pixels more than once ,since the pens footprints overlap at adjacent pixels. A better technique that also handle the circularcross-section problem is to use the snaps of the footprint to compute spans for successive footprints at adjacent pixels. As in the filling

d) Approximation by thick polyline We can do piecewise-linear approximation of any primitive by computing points on the boundary ( with floating-point coordinates ) , then connecting these points with line segments to from a polyline. The advantage of this approach is that the algorithms for both line clipping and line scan conversion ( for thin primitives ) , and for polygon clipping and polygon scan conversion ( for thick primitives ) , are efficient. Naturall, the segnates must be quite short in places where the primitive changes direction rapidly. Ellipse ares cane be represented as ratios of parametric polynomials,which lend themselves readily to such piecewiselinear approximation. The individual line segments are then drawn as rectangles with the specified thickness. To make the thick approximation look nice, however, we must solve the problem of making thick lines join smoothly . e) Line style ad pen style. SRGPs line-style atribute can affect any outline primitive. In general, we must use conditional logic to test whether or not to write a pixel, writing only for 1s. we store the pattern write mask as a string of 16 booleans (e.g., a 16-bit integer ); it should therefore repeat every 16 pixels. We modify the unconditional WritePixel statemet in the line scan-conversion algorithm to handle this.There is a drawback to this technique, however. Since each bit in the mask corresponds to an iteration of the loop, and not to a unit distance along the line . the length of dashes varies with the angle of the line; a dash at an angle is longer than is a horizontal or

vertical dash. For engineering drawings, this variation is unacceptable, and the dashes must be calculated and scan coverted as individual line segments of length invariant with angle. Thick line are created as sequences of alternating solid and transparent rectangles whose vertices are calculated exactly as a function of the line style selected. The rectangles are then scan-converted individually; for horizontal and vertical line , the program may be able to copypixel the rectangle. Line style and pen style interact in thick outline primitive. The line style is used to calculate the rectangle for each dash, and each rectangle is filled with the selected pen pattern.

2. What is DDA line drawing algorithm explain it with the suitable example? discuss the merit and demerit of the algorithm.
DNA Line Algorithm: 1: Read the line end points (X1,Y1) and (X2,Y2) such that they are equal . [ If equal then plot that point and end ]. 2: x = and y = 3. If then Length= Else Length= end if

4. = (x2-x1)/length = (y2-y1)/length This makes either or equal to 1 because the length is either| x2-x1| or | y2-y1|, the incremental value for either x or y is 1. 5. x = x1+0.5 * sign ( ) y = y1+0.5*sign( ) [Here the sign function makes the algorithm worl in all quadrant. It returns 1, 0,1 depending on whether its argument is <0, =0, >0 respectively. The factor 0.5 makes it possible to round the values in the integer function rather than truncating them] 6. i=1 [begins the loop, in this loop points are plotted] 7. while(i length) { Plot (Integer(x), Integer(y)) x= x+x y= y+y i=i+1 } 8. stop Let us see few examples to illustrate this algorithm. Ex.1: Consider the line from (0,0) to (4,6). Use the simple DDA algorithm to rasterize this line. Sol: Evaluating steps 1 to 5 in the DDA algorithm we have x1=0, x2= 4, y1=0, y2=6
Page | 5 MC0072- Computer Graphics

therefor Length = =6 x = / Length = 4/6 y= /Length = 6/6-1 Initial values for x= 0+0.5*sign ( 4/6 ) = 0.5 y= 0+ 0.5 * sign(1)=0.5 Tabulating the results of each iteration in the step 6 we get,

The results are plotted as shown in the It shows that the rasterized line lies to both sides of the actual line, i.e. the algorithm is orientation dependent

Result for s simple DDA Ex. 2 : Consider the line from (0,0) to (-6,-6). Use the simple DDA algorithm to rasterize this line. Sol : x1=0, x2= -6,y1=0, y2=-6 Therefore Lenth = | X2 -- X1 | = | Y2-Y1| = 6 Therefore Ax = Ay = -1 Initial values for x= 0+0.5*sign (-1) = -0.5 y= 0+ 0.5 * sign(-1) = -0.5 Tabulating the results of each iteration in the step 6 we get,

The results are plotted as shown in the It shows that the rasterized line lies on the actual line and it is 450 line. 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. Hence end point accuracy is poor. 3. Write a short note on: A) Reflection B) Sheer C) Rotation about an arbitrary axis Ans: A ) Reflection A reflection is a transformation that produces a mirror image of an object relative to an axis of reflection. We can choose an axis of reflection in the xy plane or perpendicular to the xy plane. The table below gives examples of some common reflection.

Reflection about y axis

B) Shear : A transformation that slants the shape of objects is called the shear transformation. Two common shearing transformations are used. One shifts x coordinate values and other shifts y coordinate values. However, in both the cases only one coordinate (x or y) changes its coordinates and other preserves its values. 1 X shear : The x shear preserves they coordinates, but changes the x values which causes vertical lines to tilt right or left as shown in the fig. 6.7. The transformation matrix for x shear is given as

2 Y Shear: The y shear preserves the x coordinates, but changes the y values which causes horizontal lines to transform into lines which slope up or down, as shown in the The transformation matrix for y shear is given as

C) Rotation about an arbitrary axis: A rotation matrix for any axis that does not coincide with a coordinate axis can be set up as a composite transformation involving combinations of translation and the coordinate-axes rotations. In a special case where an object is to be rotated about an axis that is parallel to one of the coordinate axes we can obtain the resultant coordinates with the following transformation sequence. 1. Translate the object so that the rotation axis coincides with the parallel coordinate axis 2. Perform the specified rotation about that axis. 3. Translate the object so that the rotation axis is moved back to its original position When an object is to be rotated about an axis that is not parallel to one of the coordinate axes, we have to perform some additional

transformations. The sequence of these transformations is given below. 1. Translate the object so that rotation axis specified by unit vector u passes through the coordinate origin. 2. Rotate the object so that the axis of rotation coincides with one of the coordinate axes. Usually the z axis is preferred. To coincide the axis of rotation to z axis we have to first perform rotation of unit vector u about x axis to bring it into xz plane and then perform rotation about y axis to coincide it with z axis. 3. Perform the desired rotation about the z axis 4. Apply the inverse rotation about y axis and then about x axis to bring the rotation axis back to its original orientation. 5. Apply the inverse translation to move the rotation axis back to its original position. As shown in the Fig. the rotation axis is defined with two coordinate points P1 and P2 and unit vector u is defined along the rotation of axis as Where V is the axis vector defined by two points P1 and P2 as V = P2 P1= (x2 x1, y2 y1, z2 z1) The components a, b and c of unit vector us are the direction cosines for the rotation axis and they can be defined as

4. Describe the following: A) Basic Concepts in Line Drawing B) Digital Differential Analyzer Algorithm C) Bresenhams Line Drawing Algorithm

A) Basic Concepts in Line Drawing Before discussing specific line drawing algorithms it is useful to note the general requirements for such algorithms. These requirements specify the desired characteristics of line. The line should appear as a straight line and it should start and end accurately. The line should be displayed with constant brightness along its length independent of its length and orientation. The line should be drawn rapidly. Let us see the different lines drawn in Fig.3.5.

a) Vertical and horizontal lines b) 450 line c) Lines

c) Lines with other orientation As shown in Figure a horizontal and vertical lines are straight and have same width. The 450 line is straight but its width is not constant. On the other hand, the line with any other orientation is neither straight nor has same width. Such cases are due to the finite resolution of display and we have to accept approximate pixels in such situations, shown in Fig.3.5 (c). The brightness of the line is dependent on the orientation of the line. We can observe that the effective spacing between pixels for the 450 line is greater than for the vertical and horizontal lines. This will make the vertical and horizontal lines appear brighter than the 450 line. Complex calculations are required to provide equal brightness along lines of varying length and orientation. Therefore, to draw line rapidly some compromises are made such as Calculate only an approximate line length. Reduce the calculations using simple integer arithmetic Implement the result in hardware or firmware B) DDA Line Algorithm 1. Read the line end points (x1,y1 ) and (x2,y2) such that they are not equal. [if equal then plot that point and exit] 2. x = 3. If Length= else and y = then

Length= end if 4. = (x2-x1)/length = (y2-y1)/length This makes either or equal to 1 because the length is either | x2-x1| or |y2-y1|, the incremental value for either x or y is 1. 5. x = x1+0.5 * sign( ) y = y1+0.5*sign( ) [Here the sign function makes the algorithm worl in all quadrant. It returns 1, 0,1 depending on whether its argument is <0, =0, >0 respectively. The factor 0.5 makes it possible to round the values in the integer function rather than truncating them] 6. i=1 [begins the loop, in this loop points are plotted] 7. while(i length) { Plot (Integer(x), Integer(y)) x= x+x y= y+y i=i+1 } 8. stop C) Bressenhams Line Drawing Algorithm Bresenhams line algorithm uses only integer addition and subtraction and multiplication by 2, and we know that the computer can perform the operations of integer addition and subtraction very rapidly. The computer is also time-efficient when performing integer multiplication by powers of 2. Therefore, it is an efficient method for scan-converting straight lines. The basic principle of Bresenhams line algorithm is to select the optimum raster locations to represent a straight line. To accomplish this, the algorithm always increments either x or y by one unit depending on the slope of line. The increment in the other variable is determined by examining the distance between the actual line location and the nearest pixel. This distance is called decision variable or the error. This is illustrated in the Figure below..

As shown in the Figure above the line does not pass through all raster points (pixels). It passes through raster point (0,0) and subsequently crosses three pixels. It is seen that the intercept of line with the line x=1 is closer to the line y=0,i.e. pixel(1,0) than to the line y=1 i.e. pixel (1,1). Hence, in this case, the raster point at (1,0) better represents the path of the line that at (1,1). The intercept, of the line with the line x=2 is close to the line y=1, i.e. pixel (2,1) than to the line y=0,i.e. pixel(2.0). Hence the raster point at (2,1) better represents the path of the line, as shown in the Figure above. In mathematical term error or decision variable is defined as e=DB-DA Or DA-DB Let us define e= DB-DA. Now if e>0, then it implies that DB>DA, i.e., the pixel above the line is closer to the true line. If DB < DA (i.e.<0) then we can say that the pixel below the line is closer to the true line. Thus by checking only the sign of error term it is possible to determine the better pixel to represent the line path. The error term is initially set as e=2y-x Where y=y2-y1, and x=x2-x1 Then according to value of e following actions are taken. while (e0) { y=y+1 e=e-2*x } x=x+1 e=e+2*y When e0, error is initialized with e=e 2x. This is continued till error is negative. In each iteration y is incremented by 1. When e<0, error is initialized to e=e + 2y. In both the cases x is incremented by 1. Let us see the Bresenhams line drawing algorithm. Bresenhams Line Algorithm 1. Read the line end points (x1, y1) and (x2, y2) such that they are not equal.

[if equal then plot that line and exit] 2. x = and y = 3. [initialize starting point] x = x1 y = y1 4. e = 2 * y x [ Initialize value of decision variable or error to compensate for non zero intercepts] 5. i = 1 [initialize counter] 6. Plot (x, y) 7. while (e >0) { y = y+1 e = e-2 * x } x = x+1 e = e+2 * y 8. i = i+1 9. if ( i x) then go to step 6 10. stop