Computer Graphics (CSE 4103)
Computer Graphics (CSE 4103)
University of Dhaka
Lecture 10
Geometric Transformation
Transformation
Ability to simulate both the movement and the manipulation of
objects in the plane.
Types of Transformation:
i) Geometric Transformation
ii) Coordinate Transformation
Geometric Transformation
Translation
y
x′ = x + t x , y ′ = y + t y
P′
x x′ t x
T P = , P′ = , T =
y y′ t y
P x P′ = P + T
Geometric Transformation
Rotation
x′ = xr + ( x − xr ) cos θ − ( y − yr ) sin θ
r P(x,y)
θ
r y′ = yr + ( x − xr ) sin θ + ( y − yr ) cos θ
x
P′ = Pr + R ⋅ ( P − Pr )
Φ
cos θ − sin θ
R=
sin θ cos θ
7
April 2010
Geometric Transformation
Scaling
y′ = y ⋅ s y + y f (1 − s y )
(x f , yf )
x
P′ = P ⋅ S + P f ⋅ ( 1 - S )
9
Homogeneous Coordinates
Rotation and scaling can be represented by matrix
multiplication, but translation is a vector addition.
It would be nice if all the basic transformations are matrix
multiplication. Then it is possible to represent multiplying
matrices.
It turns out we can do this if we represent 2-D points not by a
pair of numbers (x, y), but by triple (X, Y, Z), which is called a set
of homogeneous coordinates.
Homogeneous Coordinates
x′ 1 0 t x x
2D Translation
y ( x y)
y ′ = 0 1 t ⋅ y , P′ = T t , t ⋅ P
1 0 0 1 1
x′ cos θ − sin θ 0 x
2D Rotation y′ = sin θ cos θ 0 ⋅ y , P′ = R (θ ) ⋅ P
1 0 0 1 1
x′ S x 0 0 x
2D Scaling y′ = 0
Sy 0 ⋅ y , P′ = S ( S x , S y ) ⋅ P
1 0 0 1 1
April 2010
Homogeneous Coordinates
Inverse transformations:
1 0 −t x cos θ sin θ 0 1 S x 0 0
T −1 = 0 1 −t y , R −1 = − sin θ cos θ 0 , S −1 = 0 1 S y 0
0 0 1 0 0 1 0 0 1
Composite transformations: P ′ = M 2 ( M1 ⋅ P ) = ( M 2 ⋅ M1 ) ⋅ P = M ⋅ P
{ } { }
P′ = T ( t2 x , t2 y ) T ( t1x , t1 y ) ⋅ P = T ( t2 x , t2 y ) ⋅ T ( t1x , t1 y ) ⋅ P
1 0 t2 x 1 0 t1x 1 0 t1x + t2 x
0 1 t ⋅ 0 1 t = 0 1 t + t
Composite translations:
2y 1y 1y 2y
T ( t2 x , t2 y ) ⋅ T ( t1x , t1 y ) = T ( t1x + t2 x , t1 y + t2 y )
12
April 2010
Homogeneous Coordinates
P′ = R (θ 2 ) {R (θ1 ) ⋅ P} = {R (θ 2 ) ⋅ R (θ1 )} ⋅ P
P′ = R (θ1 + θ 2 ) ⋅ P
S2 x 0 0 S1x 0 0 S1x ⋅ S 2 x 0 0
0 S2 y 0 ⋅ 0 S1 y 0 = 0 S1 y ⋅ S 2 y 0
0 0 1 0 0 1 0 0 1
13
Geometric Transformation
CHANGE THE POSTION CHANGE THE SIZE OF A
OF A SHAPE SHAPE
Pmax
Pmin
P0
Translation:
Move block of pixels of frame buffer into new destination.
April 2010
Geometric Transformation
2D Reflection
y y y
x x
1 0 0 −1 0 0 −1 0 0
0 −1 0 0 1 0 0 −1 0
0 0 1 0 0 1 0 0 1
16
April 2010
Geometric Transformation
Move to origin Rotate Move back
( xr , yr )
1 0 xr cos θ − sin θ 0 1 0 − xr
0 1 yr ⋅ sin θ cos θ 0 ⋅ 0 1 − yr =
0 0 1 0 0 1 0 0 1
17
Geometric Transformation
Move to origin
Scaling Scale Move back
(x f , yf )
1 0 x f Sx 0 0 1 0 − x f S x 0 x f (1 − S x )
0 1
y f ⋅ 0 Sy
0 ⋅ 0 1 − y f = 0 Sy y f (1 − S y )
0 0 1 0 0 1 0 0 1 0 0 1
Composition of 2D Transformation
There are many situations in which the final transformation of a point is a
combination of several individual transformations. For example, the position of the
finger of a robot might be a function of the rotation of the robots hand, arm, and
torso, as well as the position of the robot on the railroad train and the position of the
train in the world, and the rotation of the planet around the sun, and . ….
Applying each transformation individually to all points in a model would take a lot
of time. Instead of applying several transformations matrices to each point we want
to combine the transformations to produce 1 matrix which can be applied to each
point.
Composition of 2D Transformation
Example: Consider any arbitrary point P (x, y). To perform a simple rotation. We need
to perform the following steps
i) Translate such that P1 is in origin
ii) Rotate
iii) Translate such that the point at the origin returns to P1
Composition of 2D Transformation
Composition of 2D Transformation
Example 1: Write a general form of the matrix for rotation about
a point P(h, k)
1 0 h cos Φ -sin Φ 0 1 0 -h
R Φ, P = 0 1 k sin Φ cos Φ 0 0 1 -k
0 0 1 0 0 1 0 0 1
A B C
cos 45 -sin 45 0 0 1 5
R 45 = sin 45 cos 45 0 0 1 2
0 0 1 1 1 1
Composition of 2D Transformation
Example 2: Perform a 450 rotation of triangle A(0, 1), B(1, 1), C(5, 2)
(a) About Origin (b) about P (-1,-1)
A B C
0 1 5
0 1 2
1 1 1
1 0 -1 cos 45 -sin 45 0 1 0 1 0 1 5
R 45, P = 0 1 -1 sin 45 cos 45 0 0 1 1 0 1 2
0 0 1 0 0 1 0 0 1 1 1 1
Composition of 2D Transformation
Example 3: Magnify the triangle with vertices A(0, 0), B (1, 1), and
C(5, 2) to twice its size while keeping C (5,2 ) fixed.
A B C
0 1 5
0 1 2
1 1 1
1 0 5 2 0 0 1 0 -5 0 1 5
R 2,2,C = 0 1 2 0 2 0 0 1 -2 0 1 2
0 0 1 0 0 1 0 0 1 1 1 1
April 2010
3D Transformations
Translation
Very similar to 2D. Using 4x4 matrices rather than 3x3.
Translation
x′ = x + t x
y
y′ = y + t y
z′ = z + t z
( x′, y′, z′ ) x′ 1 0 0 tx x
( x, y , z ) y′ 0
= 1 0 t y y
⋅
z′ 0 0 1 tz z
z x
1 0 0 0 1 1
26
April 2010
3D Transformations
Rotation
cos Φ -sin Φ 0
R Φ, Z= sin Φ cos Φ 0
0 0 1
cos Φ 0 sin Φ
R Φ, Y= 0 1 0
-sin Φ 0 cos Φ
1 0 0
R Φ, X= 0 cos Φ -sin Φ
0 sin Φ cos Φ
April 2010
General 3D Rotation
28
April 2010
y y y
P2
P2′
P1
P1′ P1′
x x x
P2′′
z z z
y y y
P2′ P2
P1′ P1′ P1
P2′′ x x x
θ
z z z
29
April 2010
1 0 0 − x1
0 1 0 − y1
The vector from P1 to P2 is: T=
0 0 1 − z1
V = P2 − P1 = ( x2 − x1 , y2 − y1 , z2 − z1 )
0 0 0 1
a = ( x2 − x1 ) | V | y u = ( a , b, c )
b = ( y2 − y1 ) | V |
c = ( z2 − z1 ) | V |
x
a 2 + b2 + c2 = 1
z
30
April 2010
Rotating u to coincide with z axis
cos α = c b 2 + c 2 = c d , sin α = b d .
( )
We obtained a unit vector w = a, 0, b 2 + c 2 = d in x − z plane.
y u = ( a, b, c )
1 0 0 0
0 c d −b d 0 u′
R x (α ) =
0 b d c d 0 α
α x
0 0 0 1
z
31
April 2010
Rotate w counterclockwise around y axis.
hence z − component is b 2 + c 2 = d . y u = ( a, b, c )
cos β = d , sin β = −a
d 0 a 0 β x
0 1 0 0
Ry (β ) =
−a 0 d 0 z
0 0 0 1 w = ( a, 0, d )
R (θ ) = T−1 ⋅ R −x 1 (α ) ⋅ R −y1 ( β ) ⋅ R z (θ ) ⋅ R y ( β ) ⋅ R x (α ) ⋅ T
32
April 2010
cos θ − sin θ 0 0
sin θ cos θ 0 0
R z (θ ) =
0 0 1 0
0 0 0 1
R (θ ) = T−1 ⋅ R −x 1 (α ) ⋅ R −y1 ( β ) ⋅ R z (θ ) ⋅ R y ( β ) ⋅ R x (α ) ⋅ T
MR (θ ) =
33
April 2010
3D Scaling
y y
x′ = x ⋅ S x
y′ = y ⋅ S y
z′ = x ⋅ S z
z x z x
Enlarging object also moves it from origin
x′ S x 0 0 0 x
y′ 0 Sy 0 0 y
P′ = = ⋅ = S⋅P
z′ 0 0 Sz 0 z
1 0 0 0 1 1
34
April 2010
Scaling with respect to a fixed point (not necessarily of object)
y y y
(x f , yf , zf ) (x f , yf , zf ) (x f , yf , zf )
z x z x z x
y
Sx 0 0 (1 − S x ) x f
−1
T⋅S ⋅T =
0 Sy 0 (1 − S y ) y f
( xf , yf , z f ) 0 0 Sz (1 − S z ) z f
0 0 0 1
z x
35
Thank you