computer graphics

20
HEMA.V Sikkim Manipal University – DE Reg:No 511119346 Subject : MC0072 - Computer Graphics. Book ID: B0810

Upload: hemasudarshan

Post on 03-Dec-2014

273 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: Computer Graphics

HEMA.VSikkim Manipal University – DE

Reg:No 511119346Subject : MC0072 - Computer Graphics.

Book ID: B0810Assignment 1 & 2

Page 2: Computer Graphics

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 primitive’s 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.

Page 3: Computer Graphics

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 primitive’s 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 cross-section ( also called footprint ) so that its center or corner is at thechosen 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 pen’s 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 footprintsat adjacent pixels. As in the filling

Page 4: Computer Graphics

d) Approximation by thick polylineWe 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 primitivechanges direction rapidly. Ellipse ares cane be represented as ratios of parametric polynomials,which lend themselves readily to such piecewise-linear 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.SRGP’s 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. Wemodify 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

Page 5: Computer Graphics

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 thenLength=ElseLength=end if

Page 6: Computer Graphics

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 onwhether its argument is <0, =0, >0 respectively. The factor 0.5 makes it possible to round thevalues 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+¨xy= y+¨yi=i+1}

8. stopLet 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 havex1=0, x2= 4, y1=0, y2=6Page | 5MC0072- Computer Graphicstherefor Length = =6¨x = / Length = 4/6¨y= /Length = 6/6-1Initial values forx= 0+0.5*sign ( 4/6 ) = 0.5y= 0+ 0.5 * sign(1)=0.5

Tabulating the results of each iteration in the step 6 we get,

Page 7: Computer Graphics

The results are plotted as shown in the It shows that the rasterized line lies to both sides of theactual 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=-6Therefore Lenth = | X2 -- X1 | = | Y2-Y1| = 6Therefore Ax = Ay = -1Initial values forx= 0+0.5*sign (-1) = -0.5y= 0+ 0.5 * sign(-1) = -0.5Tabulating the results of each iteration in the step 6 we get,

Page 8: Computer Graphics

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 Algorithm1. 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. Iteliminates the multiplication in the equation by making use of raster characteristics, so thatappropriate increments are applied in the x or y direction to find the pixel positions along the linepath

Disadvantages of DDA Algorithm1. 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) ReflectionB) SheerC) Rotation about an arbitrary axisAns:A ) ReflectionA 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.

Page 9: Computer Graphics

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

Page 10: Computer Graphics

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 theThe 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 axis2. Perform the specified rotation about that axis.3. Translate the object so that the rotation axis is moved back to its original positionWhen 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

Page 11: Computer Graphics

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 axis4. 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

Page 12: Computer Graphics

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

A) Basic Concepts in Line DrawingBefore discussing specific line drawing algorithms it is useful to note thegeneral requirements for such algorithms. These requirements specifythe desired characteristics of line.・ The line should appear as a straight line and it should start and endaccurately.・ The line should be displayed with constant brightness along its lengthindependent 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

Page 13: Computer Graphics

c) Lines with other orientationAs shown in Figure a horizontal and vertical lines are straight andhave same width. The 450 line is straight but its width is not constant. Onthe other hand, the line with any other orientation is neither straight norhas same width. Such cases are due to the finite resolution of displayand we have to accept approximate pixels in such situations, shown inFig.3.5 (c).The brightness of the line is dependent on the orientation of theline. We can observe that the effective spacing between pixels for the450 line is greater than for the vertical and horizontal lines. This will makethe vertical and horizontal lines appear brighter than the 450 line.Complex calculations are required to provide equal brightness alonglines of varying length and orientation. Therefore, to draw line rapidlysome 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 Algorithm1. Read the line end points (x1,y1 ) and (x2,y2) such that they are notequal.[if equal then plot that point and exit]

2. Δx = and Δy =

3. If then Length= else

Page 14: Computer Graphics

Length=

end if4. = (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. Itreturns –1, 0,1 depending on whether its argument is <0, =0, >0respectively. The factor 0.5 makes it possible to round the values in theinteger 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+Δxy= y+Δyi=i+1}8. stop

C) Bressenham’s Line Drawing AlgorithmBresenham’s line algorithm uses only integer addition andsubtraction and multiplication by 2, and we know that the computer canperform the operations of integer addition and subtraction very rapidly.The computer is also time-efficient when performing integermultiplication by powers of 2. Therefore, it is an efficient method forscan-converting straight lines.

The basic principle of Bresenham’s 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..

Page 15: Computer Graphics

As shown in the Figure above the line does not pass through allraster points (pixels). It passes through raster point (0,0) andsubsequently crosses three pixels. It is seen that the intercept of linewith the line x=1 is closer to the line y=0,i.e. pixel(1,0) than to the liney=1 i.e. pixel (1,1). Hence, in this case, the raster point at (1,0) betterrepresents the path of the line that at (1,1). The intercept, of the linewith the line x=2 is close to the line y=1, i.e. pixel (2,1) than to the liney=0,i.e. pixel(2.0). Hence the raster point at (2,1) better represents thepath of the line, as shown in the Figure above.In mathematical term error or decision variable is defined ase=DB-DA Or DA-DB

Let us define e= DB-DA. Now if e>0, then it implies that DB>DA, i.e., thepixel above the line is closer to the true line. If DB < DA (i.e.<0) then wecan say that the pixel below the line is closer to the true line. Thus bychecking only the sign of error term it is possible to determine the betterpixel to represent the line path.The error term is initially set as e=2Δy-ΔxWhere Δy=y2-y1, and Δx=x2-x1

Then according to value of e following actions are taken.while (e≥0){y=y+1e=e-2*Δx}x=x+1e=e+2*ΔyWhen e≥0, error is initialized with e=e – 2Δx. This is continued till erroris negative. In each iteration y is incremented by 1. When e<0, error isinitialized to e=e + 2Δy. In both the cases x is incremented by 1. Let ussee the Bresenham’s line drawing algorithm.

Bresenham’s Line Algorithm1. Read the line end points (x1, y1) and (x2, y2) such that they are notequal.

Page 16: Computer Graphics

[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 zerointercepts]5. i = 1 [initialize counter]6. Plot (x, y)7. while (e >0){y = y+1e = e-2 * Δx}x = x+1e = e+2 * Δy8. i = i+19. if ( i Δx) then go to step 610. stop