Chapter 2 2d Transformation
Chapter 2 2d Transformation
CHAPTER 2
TWO-DIMENSIONAL TRANSFORMATION
2.1 Introduction
As stated earlier, Computer Aided Design consists of three components, namely, Design (Geometric Modeling), Analysis (FEA, etc), and Visualization (Computer Graphics). Geometric Modeling provides a mathematical description of a geometric object - point, line, conic section, surface, or a solid. Visualization deals with creation of visual effects, e.g., creation of pie charts, contour plots, shading, animation, etc. Computer graphics provides visual displays and manipulations of objects, e.g., transformation, editing, printing, etc. Fortran and visual C languages are used to effect these operations. Transformation is the backbone of computer graphics, enabling us to manipulate the shape, size, and location of the object. It can be used to effect the following changes in a geometric object: Change the location Change the Shape Change the size Rotate Copy Generate a surface from a line Generate a solid from a surface Animate the object
2-1
2-2
[P] =
x1 y1 0 x2 y2 0 x3 y3 0 xn yn 0
(2.1)
The size of this matrix depends on the geometry of the object, e.g., a point is defined by a single set of coordinates (x1, y1, z1), a line is defined by two sets of coordinates (x1, y1, z1) and (x2, y2, z2), etc. Thus a point matrix will have the size 1x3, line will be 2x3, etc. A transformation matrix is always written as a 4x4 matrix, with a basic shape shown below, 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1
[T]
(2.2)
Values of the elements in the matrix will change according to the type of transformation being used, as we will see shortly. The transformation matrix changes the size, position, and orientation of an object, by mathematically adding, or multiplying its coordinate values. We will now discuss the mathematical procedure for scaling, translation, and rotation transformations.
2-3
2.4 Scaling
In scaling transformation, the original coordinates of an object are multiplied by the given scale factor. There are two types of scaling transformations: uniform and non-uniform. In the uniform scaling, the coordinate values change uniformly along the x, y, and z coordinates, where as, in non-uniform scaling, the change is not necessarily the same in all the coordinate directions. 2.4.1 Uniform Scaling For uniform scaling, the scaling transformation matrix is given as s 0 0 0 0 s 0 0 0 0 0 0 s 0 0 1
[T]
(2.3)
[T] =
sx 0 0 0
0 sy 0 0
0 0 sz 0
0 0 0 1
(2.4)
where, sx, sx, sx are the scale factors for the x, y, and z coordinates of the object.
2-4
[P] =
x1 x2 x3 xn
y1 y2 y3 yn
z1 1 z2 1 z3 1 zn 1
(2.5)
Here, we have converted the Cartesian coordinates into homogeneous coordinates by adding a 4th column, with unit value in all rows. When a fourth column, with values of 1 in each row, is added in the points matrix, the matrix multiplication between the [P] and [T ] becomes compatible. The values (x1, y1, z1, 1) represent the coordinates of the point (x1, y1, z1), and the coordinates are called as homogeneous coordinates. In homogeneous coordinates, the points (2,3,1), (4,6,2), (6,9,3), (8,12,4), represent the same point (2,3,1), along the plane z = 1, z = 2, z = 3, and z = 4, respectively. In our subsequent discussion on transformation, we will use homogeneous coordinates. Example 1: If the triangle A(1,1), B(2,1), C(1,3) is scaled by a factor 2, find the new coordinates of the triangle. Solution: Writing the points matrix in homogeneous coordinates, we have
[P] =
1 1 0 1 2 1 0 1 1 3 0 1
2-5
2 0 0 0
0 2 0 0
0 0 2 0
0 0 0 1
The new points matrix can be evaluated by the equation [P*] = [P] [T], and by substitution of the P and T values, we get 1 1 0 1 2 1 0 1 1 3 0 1 2 0 0 0 0 2 0 0 0 0 2 0 0 0 0 1 2 2 0 1 4 2 0 1 2 6 0 1
P* =
y Transformed by scaling
Original
x Note that the new coordinates represent the original value times the scale factor. The old and the new positions of the triangle are shown in the figure.
2-6
[Tt]
1 0 0 x
0 1 0 y
0 0 1 0
0 0 0 1
(2.6)
where, x and y are the values of translation in the x and y direction, respectively. For translation transformation, the matrix equation is [P*] = [P] [Tt] where, [Tt] is the translation transformation matrix. Example 2: Translate the rectangle (2,2), (2,8), (10,8), (10,2) 2 units along x-axis and 3 units along y-axis. Solution: Using the matrix equation for translation, we have [P*] = [P] [Tt], substituting the numbers, we get (2.7)
2-7
[P*]
2 2 10 10
2 0 1 8 0 1 8 0 1 2 0 1
1 0 0 0 0 1 0 0 0 0 1 0 2 3 0 1
4 4 12 12
5 11 11 5
0 1 0 1 0 1 0 1
Note that the resultant coordinates are equal to the original x and y values plus the 2 and 3 units added to these values, respectively.
2-8
2.7 Rotation
We will first consider rotation about the z-axis, which passes through the origin (0,0,0), since it is the simplest transformation for understanding the rotation transformation. Rotation about an arbitrary axis, other than an axis passing through the origin, requires a combination of three or more transformations, as we will see later. When an object is rotated about the z-axis, all the points on the object rotate in a circular arc, and the center of the arc lies at the origin. Similarly, rotation of an object about an arbitrary axis has the same relationship with the axis, i.e., all the points on the object rotate in a circular arc, and the center of rotation lies at the given point through which the axis is passing.
x = r cos Original coordinates of point P. y = r sin x* = rcos( + ) y* = rsin( + ) The new coordinates.
where, is the angle between the line joining the initial position of the point and the x-axis, and is the angle between the original and the new position of the point.
2-9
Using the trigonometric relations, we get, x* = r (cos cos - sin sin) = x cos - y sin y* = r (cos sin + sin cos) = x sin + y cos In matrix form we can write these equations as [x* y*] = [ x y] cos sin - sin cos (2.8)
In general, the points matrix and the transformation matrix given in equation (2.8) are re-written as
[x*
y* 0 1] = [ x
1]
cos -sin 0 0
sin cos 0 0
0 0 0 0 1 0 0 1
(2.9)
Thus, a point or any object can be rotated about the z-axis (in 2-D) and the new coordinates of the object found by the product of the points matrix and the rotation matrix, derived here.
2-10
Note: When the fixed axis is translated, the object is also translated. The axis and the object go through all the transformations simultaneously. We will now illustrate the above procedure by the following example.
Example 3: Rotate the rectangle (0,0), (2,0), (2, 2), (0, 2) shown below, 300 ccw about its centroid and find the new coordinates of the rectangle.
(0,2)
(2,2)
(0,0)
(2,0)
Solution: Centroid of the rectangle is at point (1, 1). We will first translate the centroid to the origin, then rotate the rectangle, and finally, translate the rectangle so that the centroid is restored to its original position. 1. Translate the centroid to the origin: The matrix equation for this step is
[P]
0 2 2 0
0 0 2 2
0 0 0 0
1 1 1 1
and
[Tt]
1 0 0 -1
0 1 0 -1
0 0 1 0
0 0 0 1
2-11
2. Rotate the Rectangle 300 ccw About the z-axis: The matrix equation for this step is given as [P*]2 = [P*]1 [Tr], where, [P*]1 is the resultant points matrix obtained in step 1, and [Tr] is the rotation transformation, where = 300 ccw. The transformation matrix is,
[Tr] =
0 0 0 0 1 0 0 1
.866 -.5 0 0
.5 .866 0 0
0 0 1 0
0 0 0 1
3. Translate the Rectangle so that the Centroid Lies at its Original Position: The matrix equation for this step is [P*]3 = [P*]2 [T-t], where [T-t] is the reverse translation matrix, given as
[T-t]
1 0 0 1
0 1 0 1
0 0 1 0
0 0 0 1
Now we can write the entire matrix equation that combines all the three steps outlined above. The equation is, [P*] = [P] [Tt] [Tr] [T-t]
2-12
[P*] =
0 2 2 0
0 0 2 2
0 0 0 0
1 1 1 1
1 0 0 -1
0 1 0 -1
0 0 1 0
0 0 0 1
0 0 0 0 1 0 0 1
1 0 0 1
0 1 0 1
0 0 1 0
0 0 0 1
0 1 0 1 0 1 0 1
The first two columns represent the new coordinates of the rotated rectangle.
2-13
[P*] = [P] [Tt] [Ts] [T-t] Where, [Tt] is the translation transformation matrix, for translation of the fixed point to the origin, [Ts] is the scaling transformation matrix, and [T-t] is the reverse translation matrix, to restore the fixed point to its original position. Note: The order of matrix multiplication progresses from left to right and the order should not be changed. The three transformation matrices [Tt] [Ts] [T-t] can be concatenated to produce a single transformation matrix, which uniformly scales an object while keeping the pivot point fixed. Thus, the resultant, concatenated transformation matrix for scaling is, 1 0 0 -x 0 1 0 -y 0 0 0 0 1 0 0 1 s 0 0 0 0 s 0 0 0 0 s 0 0 0 0 1 1 0 0 0 1 0 0 0 1 x y 0 0 0 0 1
[Ts]R =
2-14
s 0 0 x-sx
0 s 0 y-sy
0 0 s 0
0 0 0 1
(2.10)
The concatenated equation can be used directly instead of the step-by-step matrix solution. This form is preferable when writing a CAD program. Example 4: Given the triangle, described by the homogeneous points matrix below, scale it by a factor 3/4, keeping the centroid in the same location. Use (a) separate matrix operation and (b) condensed matrix for transformation.
[P] =
2 2 0 1 2 5 0 1 5 5 0 1
Solution (a) The centroid of the triangle is at, x = (2+2+5)/3 = 3, and y = (2+5+5)/3 = 4 or the centroid is C(3,4).
We will first translate the centroid to the origin, then scale the triangle, and finally translate it back to the centroid. Translation of triangle to the origin will give,
2 2 0 1 2 5 0 1 5 5 0 1
1 0 0 -3
0 1 0 -4
0 0 0 0 = 1 0 0 1
-1 -2 0 1 -1 1 0 1 2 1 0 1
-1 -2 0 1 -1 1 0 1 2 1 0 1
.75 0 0 0
0 0 .75 0 0 .75 0 0
0 0 0 1
2-15
Translating the triangle so that the centroid is positioned at (3, 4), we get
[P ] = [P ]2 [T-t]
0 0 0
1 1 1
1 0 0 0 0 1 0 0 0 0 1 0 3 4 0 1
(b) The foregoing set of three operations can be reduced to a single operation using the condensed matrix with x = 3, and y = 4. See equation (2.10) on page 16.
2 2 0 1 2 5 0 1 5 5 0 1
0.75 0 0 3-0.75(3)
0 0.75 0 4-0.75(4)
0 0 0.75 0
0 0 0 1
1 1 1
2-16
[Tcond] =
1 0 0 -x
0 1 0 -y
0 0 0 0 1 0 0 1
cos -sin 0 0
sin cos 0 0
0 0 1 0
0 0 0 1
1 0 0 x
0 1 0 y
0 0 1 0
0 0 0 1
(2.11)
where, is the angle of rotation and the point (x, y) lies in the xy plane. Example 5: Rotate the rectangle formed by points A(1,1), B(2,1), C(2,3), and D(1,3) 300 ccw about the point (3,2). y D(1,3) C C (2,3) . A(1,1) . (3,2) x B(2,1)
Solution: We will first translate the point (3,2) to the origin, then rotate the rectangle about the origin, and finally, translate the rectangle back so that the original point is restores to its original position (3,2). The new coordinates of the rectangle are found as follows. [P*] = [P] [Tt] [Tr] [T-t]
1 1 0 1 2 1 0 1 2 3 0 1 1 3 0 1
1 0 0 -3
0 1 0 -2
0 0 0 0 1 0 0 1
.866 -.5 0 0
.5 .866 0 0
0 0 0 0 1 0 0 1
1 0 0 3
0 1 0 2
0 0 1 0
0 0 0 1
2-17
0 0 0 0
1 1 1 1
These are the new coordinates of the rectangle after the rotation.
2-18
2.9 Mirroring
In modeling operations, one frequently used operation is mirroring an object. Mirroring is a convenient method used for copying an object while preserving its features. The mirror transformation is a special case of a negative scaling, as will be explained below. Let us say, we want to mirror the point A(2,2) about the x-axis(i.e., xz-plane), as shown in the figure. The new location of the point, when reflected about the x-axis, will be at (2, -2). The point matrix [P*] = [2 -2] can be obtained with the matrix transformation given below.
The transformation matrix above is a special case of a non-uniform scaling with sx =1 and sy = -1. We can extend this concept to mirroring around the y, z, and any arbitrary axis, as will be explained in the following discussion.
2-19
A x Step 2: Next, rotate the line about the origin (or the z-axis) such that it coincides with x or y axes (we will use the x-axis). Step 3: Mirror the rectangle about the x-axis. Step 4: Rotate the line back to its original orientation. Step 5: Translate the line back to its original position.
2-20
The new points matrix, in terms of the original points matrix and the five transformation matrices is given as, [P*] = [P] [Tt] [Tr] [Tm] [T-r] [T-t] (Note: A negative sign is used in the subscripts to indicate a reverse transformation).
Where, the subscripts t, r, and m represent the translation, rotation, and mirror operations, respectively.
2-21