You are on page 1of 33

204481 Foundation of Computer Graphics By Eko

1 Syamsudin & Anisya November 16, 2021


Transformations and Matrices
Transformations are functions
Matrices are functions representations
Matrices represent linear transformation
{2x2 Matrices}  {2D Linear Transformation}

204481 Foundation of Computer Graphics By Eko


2 Syamsudin & Anisya November 16, 2021
Transformations (1/3)
 What are they?
 Merubah sesuatu menjadi sesuatu yang lain menggunakan aturan
tertentu
 Matematik : pemetaan antara nilai-nilai yang berada dalam suatu
himpunan.
 geometric: translasi, rotasi, scale, shear,…
 Why are they important to graphics?
 moving objects on screen / in space
 mapping from model space to screen space
 specifying parent/child relationships
…

204481 Foundation of Computer Graphics By Eko


3 Syamsudin & Anisya November 16, 2021
Transformation (2/3)
Translation ty
Moving an object
Scale
tx
Changing the size of an
object
xnew = xold + tx; ynew = yold + ty

hnew
hold

wold wnew sx=wnew/wold sy=hnew/hold


204481 Foundation of Computer Graphics By Eko
4 Syamsudin & Anisya xnew = sxxold y new =sy
November 16, 2021
y old
Transformation (3/3)

To rotate a line or polygon, (x,y)

we must rotate each of its


vertices
Shear

Original Data y Shear x Shear


204481 Foundation of Computer Graphics By Eko
5 Syamsudin & Anisya November 16, 2021
What is a 2D Linear Transform?
   
Definition :T( a x  y )  aT ( x )  T( y ) ,
 
for scalar a and vectors x and y.
Example

Scale in x, by 2, say :
2 (x 0  x1), y0  y1   ( 2 x0 , y0 )  ( 2 x1 , y1)

204481 Foundation of Computer Graphics By Eko


6 Syamsudin & Anisya November 16, 2021
Example
y y

Scale in x by 2  2 x0  2 x1, y0  y1


( x1 , y1 ) (2 x1 , y1 ) ( 2 x1 , y1 )
 2 x0  2 x1, y0  y1

x x

(2 x0 , y0 )
( x0 , y0 ) (2 x0 , y0 )
y
 2( x0  x1), y0  y1
y
 2( x0  x1), y0  y1
 ( x0  x1), y0  y1  ( x0  x1), y0  y1
( x1 , y1 )

 2( x0  x1), y0  y1  2( x0  x1), y0  y1


x
x

204481( xFoundation of Computer Graphics By Eko


7 0 , y0 ) November 16, 2021
Syamsudin & Anisya
Transformations: Translation (1/2)
 A translation is a
straight line
movement of an
object from one
position to another.
 A point (x,y) is
transformed to the
point (x’,y’) by
adding the translation
distances Tx and Ty:
x’ = x + Tx
y’ = y + Ty
204481 Foundation of Computer Graphics By Eko
8 Syamsudin & Anisya November 16, 2021
Transformations: Translation(2/2)
 moving a point by
P (5,10)
a given tx and ty
amount P(15,10)
t x  10
 e.g. point P is T   
t y   0 
translated to point
P’
 moving a line by a P1(5,20)
given tx and ty
amount P 2(5,10) t x   0  P1(5,10)
T   
 translate each of t
  
y  10 P 2(5,0)
the 2 endpoints
204481 Foundation of Computer Graphics By Eko
9 Syamsudin & Anisya November 16, 2021
Transformations: Rotation (1/4)
Objects rotated according to angle of rotation
theta ()
Suppose a point P(x,y) is transformed to the point
P'(x',y') by an anti-clockwise rotation about the
origin by an angle of  degrees, then:

Given x = r cos  , y = r sin 


x’ = x cos  – y sin 
y’ = y sin  + y cos 

204481 Foundation of Computer Graphics By Eko


10 Syamsudin & Anisya November 16, 2021
Transformations: Rotation (2/4)
Rotation P by  anticlockwise relative to origin (0,0)

P( x, y )

P ( x, y ) r y P ( x, y )


(0,0) x
(0,0)

204481 Foundation of Computer Graphics By Eko


11 Syamsudin & Anisya November 16, 2021
Transformations: Rotation (3/4)
Rotation about an arbitary pivot point (x R,yR)
Step 1: translation of the object by (-xR,-yR)
x1 = x - x R
y1 = y - y R
Step 2: rotation about the origin
x2 = x1 cos() - y1sin ()
y2 = y1cos() - x1sin ()
Step 3: translation of the rotated object by (xR,yR)
x’ = xr + x2
y’ = yr + y2
204481 Foundation of Computer Graphics By Eko
12 Syamsudin & Anisya November 16, 2021
Transformations: Rotation (4/4)
object can be
rotated around an
arbitrary point
(xr,yr) known as
rotation or pivot
point by:

x' = xr + (x - xr) cos() - (y - yr) sin ()


y' = yr + (x - xr) sin ()+(y - yr) cos()
204481 Foundation of Computer Graphics By Eko
13 Syamsudin & Anisya November 16, 2021
Transformations: Scaling (1/5)
Scaling changes the size of an object
Achieved by applying scaling factors sx and sy
Scaling factors are applied to the X and Y
co-ordinates of points defining an object’s

204481 Foundation of Computer Graphics By Eko


14 Syamsudin & Anisya November 16, 2021
Transformations: Scaling (2/5)
uniform scaling is produced when sx and sy have same
value i.e. sx = sy
non-uniform scaling is produced when sx and sx are
not equal - e.g. an ellipse from a circle.
i.e. sx  sy

x2 = sxx1 y2 = syy1
204481 Foundation of Computer Graphics By Eko
15 Syamsudin & Anisya November 16, 2021
Transformations: Scaling (3/5)
Simple scaling -
relative to (0,0) P1( x, y )
 General form:
P1( x, y )

x  s x * x
y  s y * y

P1(2,3) P1(4,3)
Ex: sx = 2 and sy=1
P 2(3,1) P 2(6,1)

204481 Foundation of Computer Graphics By Eko


16 Syamsudin & Anisya November 16, 2021
Transformations: Scaling (4/5)
If the point (xf,yf) is to be the fixed point, the
transformation is:
x' = xf + (x - xf) Sx
y' = yf + (y - yf) Sy
This can be rearranged to give:
x' = x Sx + (1 - Sx) xf
y' = y Sy + (1 - Sy) yf
which is a combination of a scaling about the
origin and a translation.

204481 Foundation of Computer Graphics By Eko


17 Syamsudin & Anisya November 16, 2021
Transformations: Scaling (5/5)

204481 Foundation of Computer Graphics By Eko


18 Syamsudin & Anisya November 16, 2021
x' = xr + (x - xr) cos() - (y - yr) sin
y' = yr + (x - xr) sin ()+(y - yr) cos
Transformation as Matrices

Scale: cos 60 = 0.5


x’ = sxx sin 60 = 0.866 sx 0   x   sx  x 
 0 s     s  y 
y’ = syy  y  y  y 

Rotation:
x’ = xcos - ysin 
cos  sin    x   x cos  y sin  
 sin  
y’ = xsin  + ycos   cos   y   x sin   y cos 
  

Translation: t x   x   x  t x 
x’ = x + tx t       y  t 
y’ = y + ty
 y   y  y

204481 Foundation of Computer Graphics By Eko


19 Syamsudin & Anisya November 16, 2021
Transformations: Shear (1/2)
1 a   x   x  ay 
Shear in x: Shx       
0 1   y   y 

(a,1)
(0,1) (1,1)

(1, 0) (1, 0)
204481 Foundation of Computer Graphics By Eko
20 Syamsudin & Anisya November 16, 2021
Transformations: Shear (2/2)
1 0  x   x 
Shear in y: Shy       
b 1 y
    bx  y 

(0,1) (0,1) (1,1)

(0,0) (0,0)
(1, 0)

21 Syamsudin & Anisya


(1,b)
204481 Foundation of Computer Graphics By Eko
November 16, 2021
Shear in x then in y
(1,1)
(1  a,1)
(0,1) (a,1)
(0,1)

(0,0) (0,0)
(1,0) (1,0)

(a,1  ab)
     
(1,1  b) (1  a  ab,1  b)

(0,1) (0,1)
(1, b)
(1  ab, b)
(0,0) (0,0)
204481 Foundation of Computer Graphics By Eko
22 Syamsudin & Anisya November 16, 2021
Shear in y then in x
(1,1) (0,1) (a,1) (1  a,1)
(0,1)

(0,0) (0,0) (1  a,1  b  ab)


(1,0) (1,0)

(a,1  ab)
(1,1  b)

(0,1)
(0,1)
(1, b) (1,  b)

(0,0)
204481 Foundation of Computer Graphics By Eko
23 Syamsudin & Anisya November 16, 2021
Homogeneous coordinate
As translations do not have a 2 x 2 matrix
representation, we introduce homogeneous
coordinates to allow a 3 x 3 matrix representation.
The Homogeneous coordinate corresponding to the
point (x,y) is the triple (xh, yh, w) where:
xh = wx
yh = wy
For the two dimensional transformations we can set
w = 1.
204481 Foundation of Computer Graphics By Eko
24 Syamsudin & Anisya November 16, 2021
Matrix representation
 x cos  sin  0
P( x , y )   y  R   sin  cos 0
 1   0 0 1

sx 0 0 1 0 t x 
S x, y   0 s y 0 Tx , y  0 1 t y 
 0 0 1 0 0 1 

204481 Foundation of Computer Graphics By Eko


25 Syamsudin & Anisya November 16, 2021
Basic Transformation (1/3)
Translation

204481 Foundation of Computer Graphics By Eko


26 Syamsudin & Anisya November 16, 2021
Basic Transformation (2/3)
Rotation

 cos t sin t 0
 
P   ( x , y ,1)  ( x , y ,1)  sin t cos t 0  PS (t )
 
 0 0 1

204481 Foundation of Computer Graphics By Eko


27 Syamsudin & Anisya November 16, 2021
Basic Transformation (3/3)
Scaling

204481 Foundation of Computer Graphics By Eko


28 Syamsudin & Anisya November 16, 2021
Composite Transformation
Suppose we wished P2  T3,1 P1
to perform multiple
transformations on
P3  S2, 2 P2
a point: P4  R30 P3
M  R30 S2,2 T3,1
P4  MP1

204481 Foundation of Computer Graphics By Eko


29 Syamsudin & Anisya November 16, 2021
Example of Composite Transformation(1/3)
A scaling transformation at an arbitrary angle is a
combination of two rotations and a scaling:

R(-t) S(Sx,Sy) R(t)

A rotation about an arbitrary point (xf,yf) by and


angle t anti-clockwise has matrix:

T(-xf,-yf) R(t) T(xf,yf)

204481 Foundation of Computer Graphics By Eko


30 Syamsudin & Anisya November 16, 2021
Example of Composite Transformation(2/3)
Reflection about the y-axis Reflection about the x-
axis  1 0 0
  1 0 0 
0 1 0 0  1 0 
  
 0 0 1 0 0 1

204481 Foundation of Computer Graphics By Eko


31 Syamsudin & Anisya November 16, 2021
Example of Composite Transformation(3/3)

Reflection about the origin Reflection about the


line y=x
 1 0 0  0 1 0 
 0  1 0 1 0 0 
   
 0 0 1  0 0 1 

204481 Foundation of Computer Graphics By Eko


32 Syamsudin & Anisya November 16, 2021
Cos 30 -Sin 30 2 ((Cos 30)*2)+((-sin 30)*1)
x =
Sin 30 Cos 30 1 ((Sin 30)*2)+((cos 30)*1)

((0.866)*2)+((-0.5)*1) (1.732)+(-0.5) 1.232


= =
((0.5)*2)+((0.866)*1) (1)+(0.866) 1.866

204481 Foundation of Computer Graphics By Eko


33 Syamsudin & Anisya November 16, 2021

You might also like