Computer Graphics 3: 2D Transformations: Downloaded From
Computer Graphics 3: 2D Transformations: Downloaded From
ie/bmacnamee
Computer Graphics 3:
2D Transformations
2
of
45
Contents
In todays lecture well cover the following:
Why transformations
Transformations
Translation
Scaling
Rotation
Homogeneous coordinates
Matrix multiplications
Combining transformations
3
of
45
Why Transformations?
In graphics, once we have an object
described, transformations are used to move
that object, scale it and rotate it
I
m
a
g
e
s
t
a
k
e
n
f
r
o
m
H
e
a
r
n
&
B
a
k
e
r
,
C
o
m
p
u
t
e
r
G
r
a
p
h
i
c
s
w
i
t
h
O
p
e
n
G
L
(
2
0
0
4
)
6
of
45
Transformations
Whole collections of points may be transformed
by the same transformation T, e.g. lines or circles
The image of a line, L, under T, is the set of all
images of the individual points of L.
For most mappings of interest, this image is still a
connected curve of some shape
For some mappings, the result of transforming a
line may no longer be a line
Affine Transformations, however, do preserve
lines, and are the most commonly-used
transformations in computer graphics
P1 {0, 2}
P3 {2, 0}
P2 {2, 2}
Q1 {0, 4}
P0 {0, 0}
Q2 {4, 4}
Q0 {0, 0}
Q3 {4, 0}
7
of
45
Useful Properties of Affine Transformations
Preservation of lines:
Preservation of parallelism
Preservation of proportional distances
8
of
45
Why are they useful?
They preserve lines, so the image of a straight line is another straight line.
This vastly simplifies drawing transformed line segments.
We need only compute the image of the two endpoints of the original line and
then draw a straight line between them
Preservation of co-linearity guarantees that polygons will transform into
polygons
Preservation of parallelism guarantees that parallelograms will transform into
parallelograms
Preservation of proportional distances means that mid-points of lines remain
mid-points
9
of
45
Elementary Transformations
Affine transformations are usually combinations of four elementary
transformations:
1: Translation
2: Scaling
3: Rotation
4: Shearing
10
of
45
Translation
Simply moves an object from one position to
another
x
new
= x
old
+ tx y
new
= y
old
+ ty
Note: House shifts position relative to origin
y
x
0
1
1
2
2
3 4 5 6 7 8 9 10
3
4
5
6
(tx,ty) is the translation
vector or shift vector
11
of
45
Translation
A translation moves an object into a different position in a scene
This is achieved by adding an offset/translation vector
In Vector notation:
y
x
y
x
y
x
t
t
P
P
Q
Q
=
y
x
t
t
t
Original points Transformed points
Translate by t
t
12
of
45
Translation
=
y
x
t
t
T
=
y
x
P
=
/
/
/
y
x
P
P
/
= P + T
13
of
45
Scaling
Scaling multiplies all coordinates
Alters the size of an object
WATCH OUT: Objects grow and move!
x
new
= Sx x
old
y
new
= Sy y
old
(S
x
, S
y
) are
the scaling factors
Note: House shifts position relative to origin
y
x
0
1
1
2
2
3 4 5 6 7 8 9 10
3
4
5
6
1
2
1
3
3
6
3
9
14
of
45
Translation
=
y
x
s
s
S
0
0
=
y
x
P
=
/
/
/
y
x
P
P
/
= S.P
S
x
= S
y
Uniform scaling
S
x
= S
y
Differential scaling
15
of
45
Scaling
A scaling changes the size of an object with
two scale factors, Sx and Sy (if Sx != Sy,
then we have a non-uniform scale)
y y
x x
y
x
S P
S P
Q
Q
Original points Transformed points
Scale by
Sx, Sy
Sx, Sy
16
of
45
Fixed Point Scaling
The location of a scaled object can be controlled by choosing a
position, called the fixed point,(x
f
, y
f
) i.e., to remain unchanged after the
scaling transformation.
The fixed point can be chosen as the objects centroid or any other
position
For a vertex with coordinates (x, y), the scaled coordinates (x
/,
y
/
) are
calculated as,
x
/
= x
f
+ (x x
f
)s
x
y
/
= y
f
+ (y y
f
)s
y
x
/
= x. s
x
+ x
f
(1 s
x
) and y
/
= y. s
y
+ y
f
(1 s
y
)
Where x
f
(1 s
x
) and y
f
(1 s
y
) are constants for all points in the object.
17
of
45
Rotation
To generate a rotation, a rotation angle and the position (x
r
, y
r
) of the rotation
point is needed
x
new
= r cos( + ) = r cos cos r sin sin
y
new
= r sin( + ) = r cos sin + r sin cos
Original polar coordinates
x
old
= r cos and y
old
= r sin
u
y
x
0
1
1
2
2
3 4 5 6 7 8 9 10
3
4
5
6
18
of
45
Rotation
Rotates all coordinates by a specified angle
x
new
= x
old
cos y
old
sin
y
new
= x
old
sin + y
old
cos
Points are always rotated about the origin
For rotation about any specified point (x
r
, y
r
):
x
new
= x
r
+ (x
old
- x
r
) cos (y
old
- y
r
) sin
y
new
= y
r
+ (x
old
- x
r
) sin + (y
old
- y
r
) cos
6
t
u =
y
x
0
1
1
2
2
3 4 5 6 7 8 9 10
3
4
5
6
19
of
45
Rotation equations can be written in the matrix form
as
P
/
= R. P
Where rotation matrix
When coordinate positions are represented as row vectors
instead of column vectors, the matrix product in rotation eqn (i)
is transposed so that the trnsformed row coordinate [x y] is
calculated as
P
/ T
= (R. P)
T
= P
T
.R
T
Where P
T
= [ x y ], and the transpose matrix R
T
of matrix R is
obtained by interchanging rows & columns.
=
u u
u u
cos sin
sin cos
R
20
of
45
Rotation
Using the trigonometric relations, a point rotated by an angle q about the origin
is given by the following equations:
So
P P Q
y x x
sin cos =
P P Q
y x y
cos sin + =
u u
u u
cos sin
sin cos
y x
y x
y
x
P P
P P
Q
Q
Original points
Transformed points
Rotate by u
u
21
of
45
Rotation - derivation
cos ( ) cos cos sin sin u o u o u o + = ( ) ( ) ( ) ( )
sin ( ) sin cos cos sin u o u o u o + = + ( ) ( ) ( ) ( )
P
Q
R
P
X
P
Y
o
u
[1]
[2]
[3]
[4]
[1] ) ( ) ( ) ( ) ( o u o u sin sin cos cos R R
x
Q =
Substituting from [3] and [4]
) ( ) ( u u sin cos
y
P
x
P
x
Q =
Similarly from [2]
) ( ) ( u u sin cos
x
P
y
P
y
Q + =
) Rcos( Q
X
o u + =
) Rsin( Q
y
o u + =
) ( cos R P
X
o =
) ( sin R P
y
o =
Background info
22
of
45
Shearing
A shearing affects an object in a particular direction (in 2D, its either in
the x or in the y direction)
A shear in the x direction would be as follows:
The quantity h specifies what fraction of the y-coordinate should be
added to the x-coordinate, and may be positive or negative
More generally: a simultaneous shear in both the x and y directions
would be
+
+
=
x y
y x
y
x
gP P
hP P
Q
Q
+
=
y
y x
y
x
P
hP P
Q
Q
23
of
45
Shearing
Original points Transformed points
Shear by {g,h}
g =0, h=0.5
Original points Transformed points
Shear by {g,h}
g=0.5, h=0.5
24
of
45
Matrix Representation
All affine transformations in 2D can be
generically described in terms of a generic
equation as follows:
i.e.
y
x
y
x
y
x
t
t
P
P
d c
b a
Q
Q
t MP Q + =
25
of
45
1: Translation
2: Scaling
3: Rotation
4: Shearing
Transformations Summary
y
x
y
x
y
x
t
t
P
P
Q
Q
1 0
0 1
0
0
0
0
y
x
y
x
y
x
P
P
S
S
Q
Q
0
0
cos sin
sin cos
y
x
y
x
P
P
Q
Q
T
T
T
T
0
0
1
1
y
x
y
x
P
P
g
h
Q
Q
26
of
45
Problem
An affine transformation is composed of a
linear combination followed by a translation
Unfortunately, the translation portion is not a
matrix multiplication but must instead be
added as an extra term, or vector this is
inconvenient
27
of
45
Homogeneous Coordinates
A point (x, y) can be re-written in homogeneous
coordinates as (x
h
, y
h
, h)
The homogeneous parameter h is a non-
zero value such that:
We can then write any point (x, y) as (h.x, h.y, h)
We can conveniently choose h = 1 so that
(x, y) becomes (x, y, 1)
h
x
x
h
=
h
y
y
h
=
28
of
45
Why Homogeneous Coordinates?
Mathematicians commonly use homogeneous
coordinates as they allow scaling factors to be
removed from equations
We will see in a moment that all of the
transformations we discussed previously can
be represented as 3*3 matrices
Using homogeneous coordinates allows us use
matrix multiplication to calculate
transformations extremely efficient!
29
of
45
Homogeneous Coordinates
The trick we use is to add an additional component 1 to both P and Q,
and also a third row and column to M, consisting of zeros and a 1
i.e.
1 1 0 0 1
y
x
y
x
y
x
P
P
t d c
t b a
Q
Q
=
1 0 0
1 0
0 1
y
x
t
t
M
=
1 0 0
0 0
0 0
y
x
S
S
M
=
1 0 0
0
0
cos sin
sin cos
u u
u u
M
=
1 0 0
0 1
0 1
g
h
M
Translation by {tx, ty}
Scale by Sx, Sy Shear by g, h:
Rotate by u:
30
of
45
Homogeneous Translation
The translation of a point by (dx, dy) can be
written in matrix form as:
Representing the point as a homogeneous
column vector we perform the calculation as:
1 0 0
1 0
0 1
dy
dx
+
+
=
+ +
+ +
+ +
=
1 1 * 1 * 0 * 0
1 * * 1 * 0
1 * * 0 * 1
1 1 0 0
1 0
0 1
dy y
dx x
y x
dy y x
dx y x
y
x
dy
dx
31
of
45
Remember Matrix Multiplication
Recall how matrix multiplication takes place:
+ +
+ +
+ +
=
z i y h x g
z f y e x d
z c y b x a
z
y
x
i h g
f e d
c b a
* * *
* * *
* * *
32
of
45
Homogenous Coordinates
To make operations easier, 2-D points are
written as homogenous coordinate column
vectors
v dy dx T v dy y
dx x
y
x
dy
dx
) , ( ' :
1 1 1 0 0
1 0
0 1
=
+
+
=
v s s S v y s
x s
y
x
s
s
y x y
x
y
x
) , ( ' :
1 1 1 0 0
0 0
0 0
=
Translation:
Scaling:
33
of
45
Homogenous Coordinates (cont)
v R v y x
y x
y
x
) ( ' :
1
cos sin
sin cos
1 1 0 0
0 cos sin
0 sin cos
u u u
u u
u u
u u
=
+
=
Rotation:
34
of
45
Inverse Transformations
Transformations can easily be reversed
using inverse transformations
1 0 0
1 0
0 1
1
dy
dx
T
1 0 0
0
1
0
0 0
1
1
y
x
s
s
S
1 0 0
0 cos sin
0 sin cos
1
u u
u u
R
35
of
45
Composite Transformations
Translations
If 2 successive translation vectors (dx
1
, dy
1
) and (dx
2
, dy
2
) are applied to a
coordinate position V, the final transformed location V
/
is calculated as
Where P and P
/
are represented as homogeneous coordinate column vectors.
Which demonstrates that 2 successive translations are additive
V dy dx T dy dx T
V dy dx T dy dx T
V
). , ( ). , (
} ). , ( ).{ , (
1 1 2 2
1 1 2 2 /
=
) , ( ) , ( . ) , (
1 0 0
1 0
0 1
1 0 0
1 0
0 1
1 0 0
1 0
0 1
2 2 2 1 1 1 2 2
2 1
2 1
1
1
2
2
dy dx dx dx T dy dx T dy dx T or
dy dy
dx dx
dy
dx
dy
dx
+ + =
+
+
=
36
of
45
Composite Transformations
Rotations
If 2 successive rotation vectors are applied to a coordinate position V, the final
rotated location V
/
is calculated as
By multiplying the 2 rotation matrices, we can verify that 2 scuccessive rotations
are additive:.
So that the final rotated coordinates can be calculated with the composite matrix as
V R R
V R R
V
)}. ( ). ( {
} ). ( ).{ (
1 2
1 2 /
u u
u u
=
) ( ) ( . ) (
2 1 1 2
u u u u + = R R R
V R V ). (
2 1
/
u u + =
37
of
45
Composite Transformations
Scalings
Concatenating transformation matrices for 2 successive scaling operations produces
the following composite scaling matrix:
Which demonstrates that 2 successive scalings are multiplicative.
Thus if we were to triple the size of an object twice in succession, the final size
would be nine times that of the original.
) . , . ( ) , ( . ) , (
1 0 0
0 . 0
0 0 .
1 0 0
0 0
0 0
1 0 0
0 0
0 0
2 1
2 1
1
1
2
2
2 1
2 1
1
1
2
2
y y x x y x y x
y y
x x
y
x
y
x
S S S S S S S S S S S or
S S
S S
S
S
S
S
=
38
of
45
Combining Transformations
A number of transformations can be
combined into one matrix to make things
easy
Allowed by the fact that we use homogenous
coordinates
Imagine rotating a polygon around a point
other than the origin
Transform to centre point to origin
Rotate around origin
Transform back to centre point
39
of
45
Combining Transformations (cont)
) (H House
H dy dx T ) , (
H dy dx T R ) , ( ) ( u H dy dx T R dy dx T ) , ( ) ( ) , ( u
1
2
3
4
40
of
45
Combining Transformations (cont)
The three transformation matrices are
combined as follows
1 1 0 0
1 0
0 1
1 0 0
0 cos sin
0 sin cos
1 0 0
1 0
0 1
y
x
dy
dx
dy
dx
u u
u u
REMEMBER: Matrix multiplication is not
commutative so order matters
v dy dx T R dy dx T v ) , ( ) ( ) , ( ' = u
41
of
45
Summary
In this lecture we have taken a look at:
2D Transformations
Translation
Scaling
Rotation
Homogeneous coordinates
Matrix multiplications
Combining transformations
Next time well start to look at how we take
these abstract shapes etc and get them on-
screen
42
of
45
Exercises 1
x
y
0
1
1
2
2
3 4 5 6 7 8 9 10
3
4
5
6
(2, 3)
(3, 2) (1, 2)
(2, 1)
Translate the shape below by (7, 2)
43
of
45
Exercises 2
x
y
0
1
1
2
2
3 4 5 6 7 8 9 10
3
4
5
6
(2, 3)
(3, 2) (1, 2)
(2, 1)
Scale the shape below by 3 in x and 2 in y
44
of
45
Exercises 3
Rotate the shape below by 30 about the origin
x
y
0
1
1
2
2
3 4 5 6 7 8 9 10
3
4
5
6
(7, 3)
(8, 2) (6, 2)
(7, 1)
45
of
45
Exercise 4
Write out the homogeneous matrices for the
previous three transformations
__ __ __
__ __ __
__ __ __
__ __ __
__ __ __
__ __ __
__ __ __
__ __ __
__ __ __
Translation Scaling Rotation
46
of
45
Exercises 5
Using matrix multiplication calculate the rotation
of the shape below by 45 about its centre (5, 3)
x
y
0
1
1
2
2
3 4 5 6 7 8 9 10
3
4
5
(5, 4)
(6, 3) (4, 3)
(5, 2)
47
of
45
Scratch
x
y
0
1
1
2
2
3 4 5 6 7 8 9 10
3
4
5
6
48
of
45
Equations
Translation:
x
new
= x
old
+ dx y
new
= y
old
+ dy
Scaling:
x
new
= Sx x
old
y
new
= Sy y
old
Rotation
x
new
= x
old
cos y
old
sin
y
new
= x
old
sin + y
old
cos