# February 2010 Master of Computer Application (MCA) – Semester 3 MC0072 – Computer Graphics

Assignment Set – 2 1. Describe the following with respect to Line Clipping Algorithms: A) Sutherland and Cohen Subdivision B) Generalized Clipping with Cyrus-beck C) Liang-Barsky Line Clipping Algorithm
Ans –

A) Sutherland and Cohen Subdivision Line Clipping Algorithm This is one of the oldest and most popular line clipping algorithm developed by Dan Cohen and Ivan Sutherland. To speed up the processing this algorithm performs initial tests that reduce the number of intersections that must be calculated. This algorithm uses a four digit (bit) code to indicate which of nine regions contain the end point of line. The four bit codes are called region codes or outcodes. These codes identify the location of the point relative to the boundaries of the clipping rectangle as shown in the figure below.

Four-bit codes for nine regions Each bit position in the region code is used to indicate one of the four relative coordinate positions of the point with respect to the clipping

window: to the left, right, top or bottom. The rightmost bit is the first bit and the bits are set to 1 based on the following scheme: Set Bit 1 – if the end point is to the left of the window Set Bit 2 – if the end point is to the right of the window Set Bit 3 – if the end point is below the window Set Bit 4 – if the end point is above the window Otherwise, the bit is set to zero. Once we have established region codes for all the line endpoints, we can determine which lines are completely inside the clipping window and which are clearly outside. Any lines that are completely inside the window boundaries have a region code of 0000 for both endpoints and we trivially accept these lines. Any lines that have a 1 in the same bit position in the region codes for each endpoint are completely outside the clipping rectangle, and we trivially reject these lines. A method used to test lines for total clipping is equivalent to the logical AND operator. If the result of the logical AND operation with two end point codes is not 0000, the line is completely outside the clipping region. The lines that cannot be identified as completely inside or completely outside a clipping window by these tests are checked for intersection with the window boundaries. Ex: Consider the clipping window and the lines shown in Figure below. Find the region codes for each end point and identify whether the line is completely visible, partially visible or completely invisible.

The Figure below shows the clipping window and lines with region codes. These codes are tabulated and end point codes are logically ANDed to identify the visibility of the line in given in the table below. Line P1 p2 P3 P4 P5 P6 End Cod 0000 0001 0001 Point Logical Anding 0000 0000 0001 0001 0000 0000 Result Completely visb Completely inv Partially visible

P7 P8 P9 P10

0100 1000

0010 0000 0010 0000

Partially visible Partially visible

The Sutherland – Cohen algorithm begins the clipping process for a partially visible line by comparing an outside endpoint to a clipping boundary to determine how much of the line can be discarded. Then the remaining part of the line is checked against the other boundaries, and the process is continued until either the line is totally discarded or a section is found inside the window. This is illustrated in Figure below.

Sutherland-Cohen subdivision line clipping Sutherland and Cohen subdivision line clipping algorithm:

1. Read two end points of the line say P1 (x1, y1) and P2 (x2, y2). 2. Read two corners (left-top and right-bottom) of the window, say (Wx1, Wy1 and Wx2, Wy2).

3. Assign the region codes for two endpoints P1 and P2 using following steps : Initialize code with bits 0000 Set Bit 1 – if (x < Wx1) Set Bit 2 – if (x > Wx2) Set Bit 3 – if(y< Wy2), Set Bit 4 – if (y> Wy1) 4. Check for visibility of line P1 P2 a) If region codes for both endpoints P1 P2 are zero then the line is completely visible. Hence draw the line and go to step 9. b) If region codes for endpoints are not zero and the logical ANDing of them is also nonzero then the line is completely invisible, so reject the line and go to step 9. c) If region codes for two endpoints do not satisfy the conditions in 4a) and 4b) the line is partially visible 5. Determine the intersecting edge of the clipping window by inspecting the region codes of two endpoints. a) If region codes for both the end points are non-zero, find intersection points and with boundary edges of clipping window with respect to point P1 and point P2 respectively b) If region code for any one end point is non zero then find intersection point or respect to it. with the boundary edge of the clipping window with

6. Divide the line segments considering intersection points. 7. Reject the line segment if any one end point of it appears outsides the clipping window. 8. Draw the remaining line segments. 9. Stop

2. Describe the following with respect to methods of generating characters:

A) Stroke method method
Ans –

B) Starbust method

C) Bitmap

A) Stroke method

This method uses small line segments to generate a character. The small series of line segments are drawn like a stroke of pen to form a character as shown in the figure above. We can build our own stroke method character generator by calls to the line drawing algorithm. Here it is necessary to decide which line segments are needed for each character and then drawing these segments using line drawing algorithm. B) Starbust method In this method a fix pattern of line segments are used to generate characters. As shown in the fig. 5.20, there are 24 line segments. Out of these 24 line segments, segments required to display for particular character are highlighted. This method of character generation is called starbust method because of its characteristic appearance

Figure shows the starbust patterns for characters A and M. the patterns for particular characters are stored in the form of 24 bit code, each bit representing one line segment. The bit is set to one to highlight the line segment; otherwise it is set to zero. For example, 24-bit code for

Character A is 0011 0000 0011 1100 1110 0001 and for character M is 0000 0011 0000 1100 1111 0011. This method of character generation has some disadvantages. They are 1. The 24-bits are required to represent a character. Hence more memory is required 2. Requires code conversion software to display character from its 24-bit code 3. Character quality is poor. It is worst for curve shaped characters. C) Bitmap method The third method for character generation is the bitmap method. It is also called dot matrix because in this method characters are represented by an array of dots in the matrix form. It is a two dimensional array having columns and rows. An 5 7 array is commonly used to represent characters as shown in the fig 5.21. However 7 9 and 9 13 arrays are also used. Higher resolution devices such as inkjet printer or laser printer may use character arrays that are over 100 100.

character A in 5

7 dot matrix format

Each dot in the matrix is a pixel. The character is placed on the screen by copying pixel values from the character array into some portion of the screen’s frame buffer. The value of the pixel controls the intensity of the pixel.

3. Describe the following with respect to Homogeneous Coordinates: A) for Translation Scaling B) for Rotation C) for

Ans –

Homogeneous Coordinates for Translation The homogeneous coordinates for translation are given as

…. (6.13) Therefore, we have

= [x + tx y + ty 1] …. (6.14)

6.3.2 Homogeneous Coordinates for Rotation The homogeneous coordinates for rotation are given as

…. (6.15) Therefore, we have

= (6.16) 6.3.3 Homogeneous Coordinates for Scaling

….

The homogeneous coordinate for scaling are given as

=

…. (6.17)

Note: In this book, the object matrix is written first and it is then multipled by the required transformation matrix. If we wish to write the transformation matrix first and then the object matrix we have to take the transpose of both the matrices and post-multiply the object matrix i.e.,

Ex. Give a 3 × 3 homogeneous coordinate transformation matrix for each of the following translations a) b) c) d) Shift the image to the right 3 – units Shift the image up 2 units Move the image down Move the image down unit and right 1 unit unit and left 4 units

Sol.: We know that homogenous coordinates for translation are

a)

Here, tx = 3 and ty = 0

b)

Here, tx = 0 and ty = 2

c)

Here, tx = 1 and ty = –0.5

d)

Here, tx = –0.66

4. Describe the following with respect to Projection: A) Parallel Projections
Ans –

B) Perspective

C) Types of Parallel

A) Parallel Projection In parallel projection, z coordinate is discarded and parallel lined from each vertex on the object are extended until they intersect the view plane. The point of intersection is the projection of the vertex. We connect the projected vertices by line segments which correspond to connections on the original object.

parallel projection of an object to the view plane As shown in the Figure above, a parallel projection preserves relative proportions of objects but does not produce the realistic views. B) Perspective Projection The perspective projection, on the other hand, produces realistic views but does not preserve relative proportions. In perspective projection, the lines of projection are not parallel. Instead, they all coverage at a single point called the center of projection or projection reference point. The object positions are transformed to the view plane along these converged projection lines and the projected view of an object is determines by calculating the intersection of the converged projection lines with the view plane, as shown in the Fig. 7.9

Perspective projection of an object to the view plane C) Types of Parallel Projections

Parallel projections are basically categorized into two types, depending on the relation between the direction of projection and the normal to the view plane. When the direction of the projection is normal (perpendicular) to the view plane, we have an orthographic parallel

projection. Otherwise, we have an oblique parallel projection. Figure above illustrates the two types of parallel projection. Orthographic Projection The orthographic projection can display more than one face of an object. Such as orthographic projection is called axonometric orthographic projection. It uses projection planes (view planes) that are not normal to a principle axis. They resemble the perspective projection in this way, but differ in that the foreshortening is uniform rather than being related to the distance from the center of projection. Parallelism of lines is preserved but angles are not. The most commonly used axonometric orthographic projection is the isometric projection. The isometric projection can be generated by aligning the view plane so that it intersects each coordinate axis in which the object is defined at the same distance from the origin. As shown in the Fig. 7.11, the isometric projection is obtained by aligning the projection vector with the cube diagonal. It uses an useful property that all three principle axes are equally foreshortened, allowing measurements along the axes to be made to the same scale (hence the name: iso for equal, metric for measure).

Isometric projection of an object onto a viewing plane Oblique Projection An oblique projection is obtained by projecting points along parallel lines that are not perpendicular to the projection plane. Notice that the view plane normal and the direction of projection are not the same. The oblique projections are further classified as the cavalier and cabinet projections. For the cavalier projection, the direction of projection makes a 450 angle with the view plane. As a result, the projection of a line perpendicular to the view plane has the same length as the line itself; that is, there is no foreshortening. Cavalier Projections of the unit cube When the direction of projection makes an angle of arctan (2)=63.40 with the view plane, the resulting view is called a cabinet projection. For this angle, lines perpendicular to the viewing surface are

projected at one-half their actual length. Cabinet projections appear more realistic than cavalier projections because of this reduction in the length of perpendiculars. Figure below shows the examples of cabinet projections for a cube.

Cabinet projections of the Unit Cube