Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi

Lecture 11: 2D Transformations CS 433: Computer Graphics


2D Transformations
Dr. Zahid Halim
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 11: 2D Transformations CS 433: Computer Graphics
Definitions Definitions
A matrix is a rectangular array of elements (usually
numbers) arranged in rows and columns enclosed in
brackets.
|
|
|
|
|

\
|
=
|
|
|

\
|

=
|
|
|

\
|
=
d
c
b
a
C B A
2 1 3
5 3 2
0 4 2

2 0
4 1
2 1
Order of the matrix is the number of rows and columns. A is a 32
matrix, B is a 33 matrix C is a 41 matrix .
Matrices are denoted by capital letters while their elements are written
as lower case letters as in example C above.
We refer to a particular element by using notation that refers to the row
and column containing the element, i.e. a
21
=-1, b
32
= 1, c
21
= b.
Two matrices A and B are said to be equal, written A = B, if they have
the same order and a
ij
= b
ij
for every i and j.
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 11: 2D Transformations CS 433: Computer Graphics
Type of Matrices
Type of Matrices
Square matrix a matrix with an equal number of
rows and columns.
Diagonal matrix a square matrix with zeros
everywhere except down the leading diagonal.
Unit matrix (identity matrix) a diagonal matrix with
ones down the leading diagonal. The identity matrix
is denoted with the letter I.
matrix unit 3 3 a is
1 0 0
0 1 0
0 0 1
3

|
|

\
|
= I
matrix diagonal 3 3 a is
9 0 0
0 5 0
0 0 1

|
|

\
|
= A
matrix square 3 3 a is
9 8 7
6 5 4
3 2 1

|
|

\
|
= A
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 11: 2D Transformations CS 433: Computer Graphics
Type of Matrices (continue)
Type of Matrices (continue)
Zero matrix a matrix with zeros everywhere,
denoted by O.
Symmetric matrix a square matrix whose (ij)
th
element is the same as the (ji)
th
element for all i and j.
Row matrix (row vector) a matrix with only one row,
i.e. its order is 1m.
Column matrix (column vector) a matrix with only
one column, i.e. an n1 matrix.
matrix symmetric a is
3 7 4
7 2 5
4 5 1
|
|

\
|
= A
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 11: 2D Transformations CS 433: Computer Graphics
Matrix Addition and Subtraction
Matrix Addition and Subtraction
If A and B are two nm matrices then
their sum C = A + B is the nm matrix with
c
ij
= a
ij
+ b
ij
their difference C = A - B is the nm matrix with
c
ij
= a
ij
- b
ij
|
|

\
|


=
|
|

\
|


=
|
|

\
|

|
|

\
|

10 2 4
1 7 2
7 ) 3 ( ) 3 ( ) 1 ( ) 2 ( 2
4 3 2 ) 5 ( 3 1
7 3 2
4 2 3

3 1 2
3 5 1
|
|

\
|

=
|
|

\
|
+ + +
+ + +
=
|
|

\
|

+
|
|

\
|

4 4 0
7 3 4
7 ) 3 ( ) 3 ( ) 1 ( ) 2 ( 2
4 3 2 ) 5 ( 3 1
7 3 2
4 2 3

3 1 2
3 5 1
Examples: Examples:
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 11: 2D Transformations CS 433: Computer Graphics
Scalar Multiplication of Matrices
Scalar Multiplication of Matrices
If A is an nm matrix and k is a real number,
then the scalar multiple C = kA is the nm
matrix where c
ij
= ka
ij
.
|
|

\
|

=
|
|

\
|


=
|
|

\
|
14 6 4
8 4 6
7 2 ) 3 ( 2 ) 2 ( 2
4 2 2 2 3 2
7 3 2
4 2 3
2
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 11: 2D Transformations CS 433: Computer Graphics
The Transpose of a Matrix
The Transpose of a Matrix
If A is a matrix then the transpose of A is the
matrix A
t
where a
t
ij
= a
ji
.
|
|
|
|

\
|

=
3 1 2
4 1 2
0 2 3
1 2 1
A
|
|
|

\
|


=
3 4 0 1
1 1 2 2
2 2 3 1
t
A
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 11: 2D Transformations CS 433: Computer Graphics
Matrix Multiplication
Matrix Multiplication
If A is an nm matrix and B is an mp matrix
then the product of A and B, C = AB, is an
np matrix
C
ij
= (row i of A) (column j of B)
= a
i1
b
1j
+ a
i2
b
2j
+ a
i3
b
3j
+ a
in
b
jp
2 3 2 3 3 3
4 5
2 14
8 3
0 2 ) 2 ( 0 4 1 ) 3 ( 2 3 0 1 1
0 1 ) 2 ( ) 3 ( 4 ) 2 ( ) 3 ( 1 3 ) 3 ( 1 ) 2 (
0 4 ) 2 ( 2 4 3 ) 3 ( 4 3 2 1 3
0 3
2 3
4 1
2 0 1
1 3 2
4 2 3

|
|
|

\
|

=
|
|
|

\
|
+ + + +
+ + + +
+ + + +
=
|
|
|

\
|

|
|
|

\
|

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 11: 2D Transformations CS 433: Computer Graphics
Properties
Properties
If a, b are scalars and A, B, C are matrices of
the same dimension (order) then
A + B = B + A
A + (B + C) = (A + B) + C
a(bA) = (ab)A
(a + b)A = aA + bA
a(A + B) = aA + aB
a(A - B) = aA - aB
A + OA = A, and aO = O
(A + B)
t
= A
t
+ B
t
(A - B)
t
= A
t
- B
t
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 11: 2D Transformations CS 433: Computer Graphics
Properties
Properties
If a, b are scalars and A, B, C are matrices
of the same dimension (order) then
assuming that the matrix dimensions are
such that the products in each of the
following are defined, then we have:
AB BA
A(BC) = (AB)C
(A + B)C = AC + BC
A(B + C) = AB + AC
OA = O, AO = O
(aA)(bB) = abAB
IA = AI = A
(AB)
T
= B
T
A
T
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 11: 2D Transformations CS 433: Computer Graphics
Inverse of a 2
Inverse of a 2

2 Matrix
2 Matrix
Not all Matrices have an inverse.
The Determinant determines if a matrix has an
inverse or not. If the determinant of a matrix A is zero
then the matrix has no inverse.
bc ad A A
d c
b a
A = =
|

\
|
= det
11 ) 1 ( 3 4 2 det
4 1
3 2
= = =
|

\
|

= A A A
Example: Example:
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 11: 2D Transformations CS 433: Computer Graphics
Inverse of a 2
Inverse of a 2

2 Matrix
2 Matrix
If A is a matrix then it is possible to find another
matrix, called A
-1
, such that AA
-1
= I or A
-1
A = I .
|

\
|

=
|

\
|
=

a c
b d
A
A
d c
b a
A
1

1
|

\
|
=
|

\
|

=
|

\
|

11
2
11
1
11
3
11
4
1
2 1
3 4
11
1

4 1
3 2
A A
Example: Example:
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 11: 2D Transformations CS 433: Computer Graphics
Solution of Systems of
Solution of Systems of
Simultaneous Equations
Simultaneous Equations
4
11 3 2
= +
= +
y x
y x
|

\
|
=
|

\
|
|

\
|
4
11
1 1
3 2
y
x
A
A x x = = b b
Ax = b A
-1
Ax = A
-1
b x = A
-1
b
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 11: 2D Transformations CS 433: Computer Graphics
|

\
|
=
|

\
|
|

\
|
4
11
1 1
3 2
y
x
A
A x x = = b b
3
1

3
1
3
1
4 ) 2 ( 11 1
4 3 11 ) 1 (
4
11
2 1
3 1
2 1
3 1
2 1
3 1

1 1
3 2
1
1
1
1
=
=

\
|
=
|

\
|
=
|

\
|
=
|

\
|
+
+
=
|

\
|
|

\
|

= =
|

\
|

=
|

\
|

=
|

\
|
=

y
x
y
x
x
b A x
A A
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 11: 2D Transformations CS 433: Computer Graphics
Inverse of a 3
Inverse of a 3

3 matrix
3 matrix
|
|
|

\
|
+ +
+
+ +
|
|
|

\
|

=
= = = = = =
= = = = = =
= = = = = =
+ + + + = =
|
|
|

\
|
=
|
|
|

\
|
=


1
: t determinan the From
) ( ) ( det

33 32 31
23 22 21
13 12 11
1
33 32 31
23 22 21
13 12 11
a a a
a a a
a a a
A
A
bd ae
e b
d a
a bg ah
h b
g a
a eg dh
h e
g d
a
cd af
f c
d a
a cg ai
i c
g a
a fg di
i f
g d
a
ce bf
f c
e b
a ch bi
i c
h b
a fh ei
i f
h e
a
A
afh bdi ceg cdh bfg aei A A
i f c
h e b
g d a
A
i h g
f e d
c b a
A
t
t
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 11: 2D Transformations CS 433: Computer Graphics
Inverse of a 33 matrix (example)
|
|
|

\
|
+ +
+
+ +
|
|
|

\
|

=
=

= = = =

=
=

= = = =

=
= = = = = =
= = + + + = =
|
|
|

\
|

=
|
|
|

\
|
=


4 4 4
4 3 3
4 3 7
4
1
4
3 1
1 1
4
4 1
0 1
4
4 3
0 1
4
4 0
1 1
3
3 0
0 1
3
3 4
0 1
4
4 0
3 1
3
3 0
4 1
7
3 4
4 3
: t determinan the From
4 13 9 ) 3 16 0 ( ) 0 0 9 ( det
3 4 0
4 3 1
0 1 1

3 4 0
4 3 1
0 1 1
1
33 32 31
23 22 21
13 12 11
A
a a a
a a a
a a a
A
A A
A A
t
t
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 11: 2D Transformations CS 433: Computer Graphics
Why Transformations?
In graphics, once we have an object described, transformations
are used to move that object, scale it and rotate it
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 11: 2D Transformations CS 433: Computer Graphics
Translation
Simply moves an object from one position to another
x
new
= x
old
+ dx y
new
= y
old
+ dy
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
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 11: 2D Transformations CS 433: Computer Graphics
Translation Example
y
x 0
1
1
2
2
3 4 5 6 7 8 9 10
3
4
5
6
(1, 1) (3, 1)
(2, 3)
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 11: 2D Transformations CS 433: Computer Graphics
Scaling
Scalar multiplies all coordinates
WATCH OUT: Objects grow and move!
x
new
= Sx x
old
y
new
= Sy y
old
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
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 11: 2D Transformations CS 433: Computer Graphics
Scaling Example
y
0
1
1
2
2
3 4 5 6 7 8 9 10
3
4
5
6
(1, 1) (3, 1)
(2, 3)
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 11: 2D Transformations CS 433: Computer Graphics
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
6

=
y
x
0
1
1
2
2
3 4 5 6 7 8 9 10
3
4
5
6
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 11: 2D Transformations CS 433: Computer Graphics
Rotation Example
y
0
1
1
2
2
3 4 5 6 7 8 9 10
3
4
5
6
(3, 1) (5, 1)
(4, 3)
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 11: 2D Transformations CS 433: Computer Graphics
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 (hx, hy, h)
We can conveniently choose h = 1 so that
(x, y) becomes (x, y, 1)
h
x
x
h
=
h
y
y
h
=
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 11: 2D Transformations CS 433: Computer Graphics
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!
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 11: 2D Transformations CS 433: Computer Graphics
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
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 11: 2D Transformations CS 433: Computer Graphics
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
* * *
* * *
* * *
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 11: 2D Transformations CS 433: Computer Graphics
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:
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 11: 2D Transformations CS 433: Computer Graphics
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




=
(
(
(

+

=
(
(
(

(
(
(


Rotation:
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 11: 2D Transformations CS 433: Computer Graphics
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


R
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 11: 2D Transformations CS 433: Computer Graphics
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
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 11: 2D Transformations CS 433: Computer Graphics
Combining Transformations (cont)
) (H House
H dy dx T ) , (
H dy dx T R ) , ( ) (
H dy dx T R dy dx T ) , ( ) ( ) , (
1
2
3
4
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 11: 2D Transformations CS 433: Computer Graphics
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


REMEMBER: Matrix multiplication is not
commutative so order matters
v dy dx T R dy dx T v ) , ( ) ( ) , ( ' =
Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi
Lecture 11: 2D Transformations CS 433: Computer Graphics
Reference
Book Code: A
Chapter: 5

You might also like