advertisement

University of Babylon College of Information Technology Department of Information Network Day: Monday Date: 25-11-2013 Stage: Second. Topic: Introduction to Computer Graphics Lecture6: TWO-DIMENSIONAL TRANSFORMATIONS. General Objective: The students will be able to do the variant two dimensional transformations on any object. Procedural Objective: 1. The students will be able to apply the translation to move any object from one position to another. 2. The students will be able apply the scaling to resize any object. 3. The students will be able to apply the rotation to rotate any object from its position to another depending on the given angle. Teaching aids: Lecture (presented by MS-power point program on the laptop), LCD monitor, discussion between lecturer and students, whiteboard, pens. 1 TWO-DIMENSIONAL TRANSFORMATIONS 6.1 Introduction With the procedures for displaying output primitives and their attributes, we can create a variety of picture and graph forms. In many applications, there is also a need for altering or manipulating displays. Sometimes we need to reduce the size of an object or graph to place it into a larger display. Or we might want to test the appearance of design patterns by rearranging the relative positions and sizes of the pattern parts. For animation applications, we need to produce continuous motion of displayed objects about the screen. These various manipulations are carried out by applying appropriate geometric transformations to the coordinate points in a display. The basic transformations are translation, scaling, and rotation. We first discuss methods for performing these transformations and then consider how transformation functions can be incorporated into a graphics package. 6.2 Basic Transformations Displayed objects are defined by sets of coordinate points. Geometric transformations are procedures for calculating new coordinate positions for these points, as required by a specified change in size and orientation for the object. Translation A translation is a straight-line movement of an object from one position to another. We translate a point from coordinate position (x, y) to a new position (x', y') by adding translation distances, Tx and Ty, to the original coordinates: x'=x+Tx, y'=y+Ty (6-1) The translation distance pair (Tx,Ty) is also called a translation vector or shift vector. Polygons are translated by adding the specified translation distances to the coordinates of each line endpoint in the object. Figure (6-1) illustrates movement of a polygon to a new position as determined by the translation vector ( -20, 50). FIGURE 6-1 Translation of an object from position (a) to position (b) with translation distances (-20, 50). 2 Objects drawn with curves are translated by changing the defining coordinates of the object. To change the position of a circle or ellipse, we translate the center coordinates and redraw the figure in the new location. Translation distances can be specified as any real numbers (positive, negative, or zero). If an object is translated beyond the display limits in device coordinates, the system might return an error message, clip off the parts of the object beyond the display limits, or present a distorted picture. Systems that contain no provision for handling coordinates beyond the display limits will distort shapes because coordinate values overflow the memory locations. This produces an effect known as wraparound, where points beyond the coordinate limits in one direction will be displayed on the other side of the display device (Fig. 6-2). FIGURE 6-2 possible effects of wraparound on the display of a polygon with one vertex (P) specified beyond the display coordinate limits. Point P is wrapped around to position P', with a corresponding distortion (solid lines) in the display of the figure. Scaling A transformation to alter the size of an object is called scaling. This operation can be carried out for polygons by multiplying the coordinate values (x, y) for each boundary vertex by scaling factors Sx and Sy to produce transformed coordinates (x',y'): x'=x.Sx, y'=y.Sy (6-2) Scaling factor Sx scales objects in the x direction, while Sy scales in the y direction. Any positive numeric values can be assigned to the scaling factors Sx and Sy. Values less than 1 reduce the size of objects; values greater than 1 produce an enlargement. Specifying a value of 1 for both Sx and Sy leaves the size of objects unchanged. When Sx and Sy are assigned the same value, a uniform scaling is produced, which 3 maintains relative proportions of the scaled object. Unequal values for Sx and Sy are often used in design applications, where pictures are constructed from a few basic shapes that can be modified by scaling transformations (Fig. 6-3). When an object is redrawn with scaling equations 6-2, the length of each line in the figure is scaled according to the values assigned to Sx and Sy. In addition, the distance from each vertex to the origin of the coordinate system is also scaled. Figure (6-4) illustrates scaling a line by setting Sx and Sy to 1/2 in Eqs. 6-2. Both the line length and the distance to the origin are reduced by a factor of 1/2. Enlarged objects are moved away from the coordinate origin. FIGURE 6-4 A line scaled with Eqs. (6-2) and Sx= Sy = 1/2 is reduced in size and moved closer to the coordinate origin. FIGURE 6-3 Turning a square (a) into a rectangle (b) by setting Sx = 2 and Sv = 1. Q3 We can control the location of a scaled object by choosing a position, called the fixed point, which is to remain unchanged after the scaling transformation. Coordinates for the fixed point, (Xf, Yf) can be chosen as one of the vertices, the center of the object, or any other position (Fig. 6-5). A polygon is then scaled relative to the fixed point by scaling the distance from each vertex to the fixed point. For a vertex with coordinates (x, y), the scaled coordinates (x', y') are calculated as: x'=xf+(x-xf)Sx, y'=yf+(y-yf)Sy (6-3) FIGURE 6-5 Scaling relative to a chosen fixed point (xf, yf). Distances from each polygon vertex to the fixed point are scaled by transformation equations (6-3). 4 We can rearrange the terms in these equations to obtain scaling transformations relative to a selected fixed point as: x'=x.Sx+(1-Sx)xf (6-4) y'=y.Sy+(1-Sy)yf where the terms (1-Sx)xf and (1-Sy)yf are constant for all points in the object. As in translation, a scaling operation might extend objects beyond display coordinate limits. Transformed lines beyond these limits could be either clipped or distorted, depending on the system in use. Scaling transformations 6-4 are applied to the vertices of a polygon. Other types of objects could be scaled with these equations by applying the calculations to each point along the defining boundary. For standard figures, such as circles and ellipses, these transformations can be carried out more efficiently by modifying distance parameters in the defining equations. We scale a circle by adjusting the radius and possibly repositioning the circle center. Rotation Transformation of object points along circular paths is called rotation. We specify this type of transformation with a rotation angle, which determines the amount of rotation for each vertex of a polygon. Figure 6-6 illustrates displacement of a point from position (x, y) to position (x', y'), as determined by a specified rotation angle Ɵ relative to the coordinate origin. In this figure, angle Ф is the original angular position of the point from the horizontal. We can determine the transformation equations for rotation of the point from the relationships between the sides of the right triangles shown and the associated angles. Using these triangles and standard trigonometric identities, we can write: x'=r cos(Ф+Ɵ)=r cosФ cosƟ – r sinФ sinƟ (6-5) y'=r sin(Ф+Ɵ)= r sinФ cosƟ + r cosФ sinƟ where r is the distance of the point from the origin. FIGURE 6-6 Rotation of a point from position (x, y) to position (x',y') 5 through a rotation angle Ɵ, specified relative to the coordinate origin. The original angular position of the point from the x axis is Ф. We also have: x = r cos(Ф), y = r sin(Ф) (6-6) so that Eqs. 6-5 can be restated in terms of x and y as: x'=x cosƟ- y sinƟ (6-7) y'=y cosƟ+x sinƟ Positive values for Ɵ in these equations indicate a counterclockwise rotation, and negative values for Ɵ rotate objects in a clockwise direction. Objects can be rotated about an arbitrary point by modifying Eqs. 6-7 to include the coordinates (xr, yr) for the selected rotation point (or pivot point). Rotation with respect to an arbitrary rotation point is shown in Fig. 6-7. The transformation equations for the rotated coordinates can be obtained from the trigonometric relationships in this figure as: x'=xr+(x-xr)cosƟ- (y-yr)sinƟ (6-8) y'=yr+(y-yr)cosƟ+(x-xr)sinƟ FIGURE 6-7 Rotation of a point from (x, y) to (x', y') through an angle Ɵ, specified relative to a pivot point at (xr, yr). The pivot point for the rotation transformation can be set anywhere inside or beyond the outer boundary of an object. When the pivot point is specified within the object boundary, the effect of the rotation is to spin the object about this internal point. With an external pivot point, all points of the object are displaced along circular paths about the pivot point. 6 6.3 Matrix Representations and Homogeneous Coordinates There are many applications that make use of the basic transformations in various combinations. A picture, built up from a set of defined shapes, typically requires each shape to be scaled, rotated, and translated to fit into the proper picture position. This sequence of transformations could be carried out one step at a time. First, the coordinates defining the object could be scaled, then these scaled coordinates could be rotated, and finally the rotated coordinates could be translated to the required location. A more efficient approach is to calculate the final coordinates directly from the initial coordinates using matrix methods, with each of the basic transformations expressed in matrix form. We can write transformation equations in a consistent matrix form by first expressing points as homogeneous coordinates. This means that we represent a two-dimensional coordinate position (x, y) as the triple [xh,yh,w], where: xh=x.w, yh=y.w (6-9) The parameter w is assigned a nonzero value in accordance with the class of transformations to be represented. For the two-dimensional transformations discussed in the last section, we can set w = 1. Each two-dimensional coordinate position then has the homogeneous coordinate form [x y 1]. Other values for w are useful with certain three-dimensional viewing transformations. With coordinate positions expressed in homogeneous form, the basic transformation equations can be represented as matrix multiplications employing 3 by 3 transformation matrices. Equations 6-1, for translation, become: (6-10) We also introduce the abbreviated notation T(Tx, Ty) for the 3 by 3 transformation matrix with translation distances Tx and Ty: (6-11) 7 Using this notation, we can write the matrix form for the translation equations more compactly as: P' = P . T(Tx,Ty) (6-12) where P' = [x' y' 1] and P = [x y 1] are 1 by 3 matrices (three-element row vectors) in the matrix calculations. Similarly, the scaling equations 6-2 are now written as: (6-13) or as: P' = P . S(Sx, Sy) (6-14) With: (6-15) as the 3 by 3 transformation matrix for scaling with parameters Sx and Sy. Equations 6-7, for rotation, are written in matrix form as: (6-16) or as: p' = p . R(Ɵ) (6-17) where: (6-18) is the 3 by 3 transformation matrix for rotation with parameter Ɵ. 8 Matrix representations are standard methods for implementing the basic transformations in graphics systems. In many systems, the scaling and rotation transformations are always stated relative to the coordinate origin, as in Eqs. 6-13 and 6-16. Rotations and scalings relative to other points are handled as a sequence of transformations. An alternate approach is to state the transformation matrix for scaling in terms of the fixed-point coordinates and to specify the transformation matrix for rotation in terms of the pivot-point coordinates. 6.4 Composite Transformations Any sequence of transformations can be represented as a composite transformation matrix by calculating the product of the individual transformation matrices. Forming products of transformation matrices is usually referred to as a concatenation, or composition, of matrices. Translations Two successive translations of an object can be carried out by first concatenating the translation matrices, then applying the composite matrix to the coordinate points. Specifying the two successive translation distances as (Tx1, Ty1) and (Tx2, Ty2), we calculate the composite matrix as: (6-19) which demonstrates that two successive translations are additive. Equation 6-19 can be written as: T(Tx1, Ty1) • T(Tx2, Ty2) = T(Tx1 + Tx2, Ty1 + Ty2) (6-20) The transformation of coordinate points for a composite translation is then expressed in matrix form as: P' = P . T(Txl + Tx2, Tyl + Ty2) (6-21) Scalings Concatenating transformation matrices for two successive scaling operations produces the following composite scaling matrix: S(Sx1, Sy1) . S(Sx2, Sy2) = S(Sx1 . Sx2, Sy1 . Sy2) (6-22) 9 The resulting matrix in this case indicates that successive scaling operations are multiplicative. That is, if we were to triple the size of an object twice in succession, the final size would be nine times that of the original. Rotations The composite matrix for two successive rotations is calculated as: R(Ɵ1). R(Ɵ2)= R(Ɵ1+ Ɵ2) (6-23) As is the case with translations, successive rotations are additive. Scaling Relative to a Fixed Point Using the transformation matrices for translation (Eq. 6-11) and scaling (Eq.6-15), we can obtain the composite matrix for scaling with respect to a fixed point (xf, yf) by considering a sequence of three transformations. This transformation sequence is illustrated in Fig. 6-8. First, all coordinates are translated so that the fixed point is moved to the coordinate origin. Second, coordinates are scaled with respect to the origin. Third, the coordinates are translated so that the fixed point is returned to its original position. The matrix multiplications for this sequence yield: (6-24) FIGURE 6-8 Sequence of transformations necessary to scale an object with respect to a fixed point using transformation matrices (6-11) and (6-15). 11 Rotation About a Pivot Point Figure 6-9 illustrates a transformation sequence for obtaining the composite matrix for rotation about a specified pivot point (xr, yr). First, the object is translated so that the pivot point coincides with the coordinated origin. Second, the object is rotated about the origin. Third, the object is translated so that the pivot point returns to its original position. This sequence is represented by the matrix product: (6-25) FIGURE 6-9 Sequence of transformations necessary to rotate an object about a pivot point using transformation matrices (6-11) and (6-18). 11