Matrix Notation for Graphical Transformations
Total Page:16
File Type:pdf, Size:1020Kb
Appendix Matrix notation for A graphical transformations This appendix is not intended as an introduction to matrix notation but serves to supplement the principles given in Chapters 3 and 4 concerning transformations by using the shorthand of matrix notations. A.I TWO-DIMENSIONAL TRANSFORMATIONS A generalized point in 2-D can be represented by its x and Y co-ordinates as p = [xv YI], where [xv yd is a 1 x 2 matrix. Similarly a line in 2-D can be represented by its end points as a 2 x 2 matrix: L = [:~ ~:]. The general 2-D point (x, y) can be transferred into the point (xv YI) as Xl = ax + by (A. 1) YI = ex + dy, where the transform T can be expressed in matrix form as: Equation (A.l) can now be expressed in matrix form as (XII YI) = (X, y) [~ ~] I (A.2) i.e. Xl can be found by multiplying the row vector (x, y) by the first column [~ :] and YI by multiplying (x, y) by the second column [: ~]. Translation I ~ Similarly a pair of matrix transformations can be multiplied together to give one combined (or concatenated) form. Thus if and then the concatenated transform or T = [(ae + ef) (ag + eh)] (be + df) (bg + dh) T = [~ ~l where j = ae + ej, i.e. j is formed by taking the sum of the products of the first row of TI with the first column of T2; k = be + dj, i.e. k is formed by taking the sum of the products of the second row of TI with the first column of T2; 1 = ag + eh, i.e. 1is formed by taking the sum of the products of the first row of TI with the second row of T2; m = bg + dh, i.e. m is formed by taking the sum of the products of the second row of TI with the second column of T2• A.2 TRANSLATION The expressions given in Chapter 3 for translation of a point are Xl = X + ~x, YI = Y + ~y, where Xl and YI are co-ordinates of the translated point, i.e. (Xl, YI) = (X, Y) + T where T = [~X, ~y] Similarly a line L can be translated as I 294 II Appendix A (A. 3) Thus in Fig. 3.17, the line L = [~ ~] which is translated by two units in x and one unit in y as T = [~ ~]. Hence from eqn (A.3), the transform is L+T=[~ ~]+[~ ~]=[!!J i.e. (Xl, YI) = (3, 2) (xz, yz) = (4, 4) as in Fig. 3.17. A.3 SCALING Scaling involves enlarging or reducing size, e.g. the point (x', y') = (x, y)S where 5= [AX 0] o Ay the scaling matrix. This scales by Ax in the x direction and by Ay in the y direction. An enlargement (Ax, Ay> 1) also translates further from the origin, while a reduction (Ax, Ay < 1) translates nearer the origin. In the example Fig. 3.18, the line L = [~ ~] is doubled in size, i.e. 5 = [~ ~] and LS = [(1 x 2 + 1 x 0) (1 x 0 + 1 x 2)] (2 x 2 + 3 x 0) (2 x 0 + 3 x 2) = [! ~] Homogeneous co-ordinates I ~ i.e. (Xl, YI) = (2, 2) (X2' Y2) = (4, 6) as in Fig. 3.18. A.4 ROTATION In a 2-D transformation, points are rotated about Oz in an anti-clockwise direc tion for positive values of e, using a right-hand co-ordinate system, e.g. for a point (Xl! YI) X I = X cos e + Y sin e YI = Y cose + X sine, where Xl and YI are the transformed co-ordinates of the point (x, Y), or (Xl' YI) = (X, y)R where R = [COSO sinO] -sinO cosO the rotation matrix. In the example given in Fig. 3.20 the line L = [~ !] is rotated by 0 = 60° about the origin, i.e. R = [cos 60° sin 600] -sin 60° cos 60° and LR = [-0.366 1.366] -1.598 3.232 . A.S HOMOGENEOUS CO-ORDINATES To achieve a pure rotation about the end (1, 1) of a line, it would be necessary to translate the end (I, 1) to the origin, perform the rotation and then retranslate the end of the line back to the position (I, 1). The advantage and strength of matrix multiplication is that the successive processes of translation, rotation and translation can be combined into a single transformation matrix. However, the desire to combine the above 2x2 matrices into a single multiplication matrix gives rise to difficulties because the translation transform is an additive process, while rotation and scaling are multiplicative. To achieve a similar multiplicative form with translation, the concept of homogeneous co-ordinate transforms is introduced. To treat all 2-D processes as multiplicative, the translation process must be turned into a dot product form. The transformations can then all be treated simi larly (or homogeneously). This is carried out by considering the xy plane con- ~ I Appendix A y x Points P lie --......"..... along this line as scale factor F increases z Fig. A.1 Homogeneous co-ordinate representation of a point by use ofa scale factor F. taining the line L to be scaled in the z direction. The xy plane is then only one of an infinite number of planes in the z direction. The z axis then represents a scaling factor F which ranges from 0 to 00 as shown in Fig. A.I. A point P(XI' YI) is now represented as P(F·XI, F·YI, F) where scale factor F=f=O giving the homo geneous co-ordinate representation for a point as P(XYF). The 2-D Cartesian co-ordinates are given by x=XIF, y=YIF. In practice F is normally chosen as 1 and thus division by F is not required. For 2-D representation in homogeneous form, a point now becomes a three element row vector [x, y, 1] and a translation becomes the dot product [x'y'l] = [xy1]· T(tlXL1y), where [x' y' 1] is the transformed point at F = 1 and [x y 1] is the original point at F=l and T(L1x L1y) = [ ~ ~ ~ 1 L1x L1y 1 A series of translations one after the other, can be accomplished by first finding the dot products of all the translation matrices to give a concatenated form as a single translation matrix whose elements will be the sum of the individual elements. A scaling is now Homogeneous co-ordinates I ~ ~x [x'y'l] = [xyl]' [ ~ The concatenated form of successive scalings one after the other will be the product of the individual elements. A rotation is now cosEl sinEl 0] [ [x'y'l] = [xyl]· -s~nEl cosEl 0 . o 1 Concatenation of successive rotations will be the sum of individual elements. Example A.I Rotate a square about a corner P through an angle El. Because transformations are with respect to the origin, this process has three parts: y Final position ,.. ,..,..",,/ y ( \ ~ \ Initial ~ (/) position \ .) 1+---- \ ,..'" P(l, 2) OL----------------- O~------~----~x- x Stage 1 Fig. A.2 Stages in the rotation ofa square. y OL----S-ta-g-e-3---X- Stage 2 x ~ I Appendix A 1. translate P to the origin; 2. rotate through an angle e; 3. re-translate back to the original position of P as shown in Fig. A.2. The net transformation matrix is: 0 sine 0 0] [COS9 1 ~ . -s~ne cose 1 L~, -YI 0 l[~ YI ~] [ cos9 sine = -sine cose XI(1 - cos e) + YI sin e YI(1 - cos e) - Xl sin e n This single transformation matrix can then be applied to all four corners of the square to give their final co-ordinates. The order of the above steps is clearly vital. If a rotation about P were followed by two translations, the result would be quite different and thus matrices are not, in general, commutative. The only transforms in which order is unimportant are: 1. a series of translations; 2. successive sea lings; 3. successive rotations; 4. when a scaling (with the same X and Y scale factor) is followed by a rotation. A. 5.1 COMPUTATIONAL EFFICIENCY Although the individual matrices need to be kept as a 3x3 matrix, the final transformation matrix can have the last column ignored. Thus the transfor mation of a 2-D point uses four multiplications and four additions, instead of nine multiplications and six additions. The resulting saving of computation time can be worth while for the transformation of complex items or where speed is important, as in dynamic displays. A.6 THREE-DIMENSIONAL TRANSFORMATIONS 3-D transformations in homogeneous co-ordinates involve a 4x4 matrix. A 3-D translation becomes 0 0 1 1 T(Ax, I>y, 1>2) [ ~ ~ 0 1 dx dy Az ~l A 3-D scaling becomes 0 0 dy 0 S(Ax, I>y, 1>2) [Ax ~ ~ 0 Az 0 0 ~l Three-dimensional transformations I ~ A 3-D rotation becomes: rotation about the x axis 0 0 cose sine R = x -sine cose [ 0 0 ~J rotation about the y axis [COSO 0 -sine 1 0 R = 0 y si~e 0 cose 0 0 ~l rotation about the Z axis [ cosO sine 0 R = -sine cose 0 Z 0 0 1 0 0 0 ~J The example given in Fig. 4.19, which involves a rotation of a triangle ABC, first about'Oz by +900 and then by -600 about the Ox axis, is now con sidered. Since corner A is placed at the origin, and always remains there, it can be ignored.