For most applications we only require three basic transformations, and these are demonstrated in Figure 1.
In fact the Window - Viewport transformation we discussed previously is made up of three basic transformations performed one after the other. We will see how to do this later.
x' = x + tx y' = y + tyAs you can see the calculation is minimal, and you might expect that this is the optimal method.
However, we will see later that this formulation is far from ideal in a real situation.
e.g. double the height and half the width. Figure 3 gives an example of the effect of scaling.
x' = x * sx y' = y * syThis calculation involves multiplication, but is very simple, and you might expect that this too is the optimal method. Again, this formulation is far from ideal in a real situation.
x' = x * cos(A) - y * sin(A) y' = x * sin(A) + y * cos(A)This transformation not only requires real arithmetic, but it also require trigonometric functions. There is no alternative to using trigonometric functions in this case, but it may be made more efficient by using function tables for sine and cosine.
The combined effect of the three transformations is the same as the formula given in the section above.
However, in general terms it may be necessary to perform an arbitrary set of transformations on an image. This would mean having to perform a whole sequence of calculations on each point.
This is not very efficient. A more efficient method would allow any number of transformations to be combined into a single composite transformation, which can then be applied to each point. This is possible if we specify the basic transformations in a different way.
The usual method is to use matrices to represent transformations and co-ordinates. We will discuss this in the next section.
A transformation can also be represented as a matrix, but this time with 3 rows and 3 columns:
The transformation is represented mathematically by the multiplication of the transformation matrix by the co-ordinate matrix. The result is a 3 rows by 1 column matrix which represents the transformed point. The resulting matrix is:
The three basic transformations are represented by the following transformation matrices:
At first it may seem that the effect of using transformation matrices is to complicate the calculation process for the benefit of making the calculation the same for each kind of transformation. However, matrix algebra is very similar to arithmetic algebra. e.g.
Using the matrix approach, if you need to apply transformation T1 to all your data co-ordinates, and then transformation T2 you can combine the two transformations into a single composite matrix and then apply the combined transformation to all the points in one pass. e.g.
The mathematical formulation for multiplying two transformations is:
giving:
As you can see, the calculation is straightforward, and easy to automate, but there are a lot of calculations. However, these calculations only need to be performed once and then the composite transformation matrix can be applied to all the data points. Figure 5 gives the code for handling matrix representation of co-ordinates and transformations.
TYPE TCoordinate = ARRAY [0 .. 2] OF Real; TTransformation = ARRAY [0 .. 2, 0 .. 2] OF Real; CONST Identity: TTransformation = ((1,0,0),(0,1,0),(0,0,1)); PROCEDURE TransformPoint (VAR P: TCoordinate; T: TTransformation); VAR I, J: Integer; Temp: TCoordinate; BEGIN FOR I := 0 TO 2 DO BEGIN Temp[I] := 0; FOR J := 0 TO 2 DO Temp[I] := Temp[I] + T[I,J] * P[J]; END; P := Temp END; PROCEDURE ComposeTransformation (VAR T3: TTransformation; T2, T1: TTransformation); VAR I, J, K: Integer; BEGIN FOR I := 0 TO 2 DO FOR J := 0 TO 2 DO BEGIN T3[I,J] := 0; FOR K := 0 TO 2 DO T3[I,J] := T3[I,J] + T2[I,K] * T1[K,J] END END; PROCEDURE Translate(TX, TY: Real; VAR T: TTransformation); BEGIN T := Identity; T[1,3] := TX; T[2,3] := TY END; PROCEDURE Scale(SX, SY: Real; VAR T: TTransformation); BEGIN T := Identity; T[1,1] := SX; T[2,2] := SY END; PROCEDURE Rotate(A: Real; VAR T: TTransformation); BEGIN T := Identity; T[1,1] := Cos(A); T[2,1] := Sin(A); T[1,2] := -T[2,1]; T[2,2] := T[1,1] END;
2. Rewrite the Window - Viewport Transformation program to make use of the transformation unit from exercise 1 above. You will need to express a Window - Viewport transformation as a sequence of basic transformations as described earlier in this section.
Copyright (C) 1995
JPS Graphics, ******************* All rights reserved Comments to author: *****************