2D Transformation
2D Transformation
Changes in orientations, size and shape are accomplished with geometric transformations
that alter the coordinate description of objects.
Basic transformation
Translation
T(tx, ty)
Translation distances
Scale
S(sx,sy)
Scale factors
Rotation
R()
Rotation angle
Translation
x’ = x + tx, y’ = y + ty
40
The translation distance point (tx,ty) is called translation vector or shift vector.
Translation equation can be expressed as single matrix equation by using column vectors
to represent the coordinate position and the translation vector as
P ( x, y )
T (t x , t y )
x' x t x
y' y t y
x ' x t x
y ' y t
y
P' P T
Moving a polygon from one position to another position with the translation
vector (-5.5, 3.75)
Rotations:
Positive values for the rotation angle define counter clock wise rotation about
pivot point. Negative value of angle rotate objects in clock wise direction. The
transformation can also be described as a rotation about a rotation axis perpendicular to
xy plane and passes through pivot point
41
Rotation of a point from position (x,y) to position (x’,y’) through angle θ relative to
coordinate origin
The transformation equations for rotation of a point position P when the pivot point is at
coordinate origin. In figure r is constant distance of the point positions Ф is the original
angular of the point from horizontal and θ is the rotation angle.
x = rcosФ, y = rsinФ
the transformation equation for rotating a point at position (x,y) through an angle θ about
origin
x’ = xcosθ – ysinθ
y’ = xsinθ + ycosθ
Rotation equation
P’= R . P
Rotation Matrix
cos sin
R=
sin cos
42
x' cos sin x
y ' sin cos y
Note : Positive values for the rotation angle define counterclockwise rotations about the
rotation point and negative values rotate objects in the clockwise.
Scaling
A scaling transformation alters the size of an object. This operation can be carried out for
polygons by multiplying the coordinate values (x,y) to each vertex by scaling factor Sx &
Sy to produce the transformed coordinates (x’,y’)
x' s x 0 x
y ' 0 s y y
or
P’ = S. P
Turning a square (a) Into a rectangle (b) with scaling factors sx = 2 and sy= 1.
Any positive numeric values are valid for scaling factors sx and sy. Values less than 1
reduce the size of the objects and values greater than 1 produce an enlarged object.
43
There are two types of Scaling. They are
Uniform scaling
Non Uniform Scaling
To get uniform scaling it is necessary to assign same value for sx and sy. Unequal values
for sx and sy result in a non uniform scaling.
x' 1 0 t x x
y ' 0 1 t y
y
1 0 0 1 1
P' T t x , t y P
For Scaling
x' s x 0 0 x
y ' 0 sy 0 y
1 0 0 1 1
P ' S s x , s y P
44
For rotation
Composite Transformations
Translation
If two successive translation vectors (tx1,ty1) and (tx2,ty2) are applied to a coordinate
position P, the final transformed location P’ is calculated as
P’=T(tx2,ty2).{T(tx1,ty1).P}
={T(tx2,ty2).T(tx1,ty1)}.P
Or
T(tx2,ty2).T(tx1,ty1) = T(tx1+tx2,ty1+ty2)
Rotations
P’=R(θ2).{R(θ1).P}={R(θ2).R(θ1)}.P
45
By multiplying the two rotation matrices, we can verify that two successive rotation are
additive
So that the final rotated coordinates can be calculated with the composite rotation matrix
as
P’ = R(θ1+ θ2).P
Scaling
46
The composite transformation matrix for this sequence is obtain with the concatenation
Translate object so that the fixed point coincides with the coordinate origin
Scale the object with respect to the coordinate origin
Use the inverse translation of step 1 to return the object to its original position
47
Concatenating the matrices for these three operations produces the required scaling matix
#include <math.h>
#include <graphics.h>
typedef float Matrix3x3 [3][3];
Matrix3x3 thematrix;
48
/ * Multiplies matrix a times b, putting result in b */
void matrix3x3PreMultiply (Matrix3x3 a. Matrix3x3 b)
{
int r,c:
Matrix3x3 tmp:
for (r = 0; r < 3: r++)
for (c = 0; c < 3; c++)
tmp[r][c] =a[r][0]*b[0][c]+ a[r][1]*b[l][c] + a[r][2]*b[2][c]:
for (r = 0: r < 3: r++)
for Ic = 0; c < 3: c++)
b[r][c]=- tmp[r][c]:
}
49
m[l] [2] = refPt.y * (1 - cosf (a) - refPt.x * sinf ( a ) ;
matrix3x3PreMultiply (m, theMatrix);
}
Other Transformations
1. Reflection
2. Shear
Reflection
1 0 0
0 1 0
0 0 1
Reflection of an object about the y axis
1 0 0
0 1 0
0 0 1
51
Reflection about origin is accomplished with the transformation matrix
1 0 0
0 1 0
0 0 1
To obtain transformation matrix for reflection about diagonal y=x the transformation
sequence is
1. Clock wise rotation by 450
2. Reflection about x axis
3. counter clock wise by 450
52
Reflection about the diagonal line y=x is accomplished with the transformation
matrix
0 1 0
1 0 0
0 0 1
To obtain transformation matrix for reflection about diagonal y=-x the transformation
sequence is
1. Clock wise rotation by 450
2. Reflection about y axis
3. counter clock wise by 450
Reflection about the diagonal line y=-x is accomplished with the transformation
matrix
0 1 0
1 0 0
0 0 1
Shear
A Transformation that slants the shape of an object is called the shear transformation.
Two common shearing transformations are used. One shifts x coordinate values and other
shift y coordinate values. However in both the cases only one coordinate (x or y)
changes its coordinates and other preserves its values.
53
X- Shear
The x shear preserves the y coordinates, but changes the x values which cause vertical
lines to tilt right or left as shown in figure
1 shx 0
0 1 0
0 0 1
x’ =x+ shx .y
y’ = y
Y Shear
The y shear preserves the x coordinates, but changes the y values which cause horizontal
lines which slope up or down
1 0 0
shy 1 0
0 0 1
x’ =x
y’ = y+ shy .x
54
XY-Shear
x' 1 shx 0 x
y ' sh 1 0 y
y
1 0 0 1 1
x’ =x+ shx .y
y’ = y+ shy .x
We can apply x shear and y shear transformations relative to other reference lines. In x
shear transformations we can use y reference line and in y shear we can use x reference
line.
We can generate x-direction shears relative to other reference lines with the
transformation matrix
y’ = y
Example
55
Y shear with x reference line
We can generate y-direction shears relative to other reference lines with the
transformation matrix
x’ =x
Example
56