Lesson 4 - 2D Transformation

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

1

Lesson 4:
2D Transformations

Shima Muhammad Qafor


UHD
College Of Science & Technology
Computer Science Department

Computer Graphics - Theory


Outline
2

 Transformation
 Translation
 Scaling
 Rotation
 Homogenous coordinates
 Reflection
 Shearing
Transformations
3

 Operations that are applied to the geometric


description of an object to change its position,
orientation, or size.
y

 Major types of transformations:


 Translation x
 Rotation
y
 Scaling
 Other transformations:
 Shearing
x y
 Reflection

x
Translation
4

 Moving an object is called translation.


 Given a point P(x, y), and the translation distance (tx, ty)
The new point P’(x’, y’):
x’ = x + tx
y’ = y + ty
OR
P'  P  T, with
 x'   x  tx 
P'    , P    and T   
 y'  y ty 
Translation (Cont.)
5

 Translation is a rigid-body transformation that moves


objects without deformation.
 Thatis, every point on the object is translated by the same
amount.
 A straight-line segment is translated by applying
translation to the two endpoints.
 A polygon is translated by applying the same operation
on all points.
 The object is redrawing after applying the
transformation
Example: Translation
6

 Consider a triangle with vertices A(0, 0), B(2, 2) and C(3, 1),
translate 2 units in the Horizontal direction and 1 unit in the
Vertical direction.

(2,2)

(3,1)

(0,0)
Scaling
7

 Scaling alters the size of an object.


 Alter the size of an object by a scaling factor (Sx, Sy)
x’ = x . Sx x’ Sx 0 x
=
y’ = y . Sy y’ 0 Sy y

 if the Scaling Factor is greater than 1 its enlargement, if the


factor is less than 1, the object is made smaller.
Examples: Scaling
8

 Example 1: What is scaling of (1.4, 2.2) by scaling factor (3, 3)?


x’ = x . Sx = 1.4 * 3 = 4.2
y’ = y . Sy = 2.2 * 3 = 6.6
(4,4)
 Example 2: (2,2) Sx = 2, Sy = 2

(2,2)
(1,1)

 Not only the object size is changed, it also moved.


 Usually this is an undesirable effect.
Examples: Scaling (Cont.)
9

 Example 3: Consider a triangle with vertices A(0, 0),


B(2, 2) and C(3, 1). Scale the triangle by 2 units in the
X axes and 2 units in the Y axes

(2,2)

(3,1)

(0,0)
Scaling (With Respect to a Fixed Point)
10

 Whenever scaling is performed there is one point that remain at the


same location, this is called the Fixed Point (Xf, Yf) of the scaling
transformation.
 To scale an object from a specific fixed point (Xf, Yf) we perform the
following three steps:
 Step One: Translate the point (Xf, Yf) to the origin ,every point (x, y) is
moved to a new point (X1, Y1):
X1 = X – Xf
Y1 = Y – Yf
 Step Two: Scale the translated points (X1, Y1):
X2 = X1 * Sx
Y2 = Y1 * Sy

 Step Three: Translate the origin back on the fixed point (Xf, Yf):
X3 = X2 + Xf
Y3 = Y2 + Yf
Uniform and Non-Uniform Scaling
11

 When Sx and Sy are assigned the same value, a


uniform scaling is produced.
 Unequal values for Sx and Sy result in a differential
(non uniform) scaling.
Rotation About Origin
12

 Rotation repositions an object along a circular path.


 Rotation requires an angle (q) and a pivot point (0, 0).
 To rotate a line or polygon, we must rotate each of its
vertices.
 Objects not centered at the origin, an unwanted translation
might be introduced.

y y

q q
o x o x

q > 0 : Rotate counter clockwise q < 0 : Rotate clockwise


Rotation (1)
13

y
 Rotate point (x, y) about the (x’, y’)

origin by q (x’, y’)


q (x, y)
r
 How to compute (x’, y’) ? f

o x

y
(x’, y’)
 x = r cos (f) y = r sin (f)
 x’ = r cos (f  q) y’ = r sin (f  q) (x, y)
q
r
f

o x
Rotation (2)
14

 x = r cos (f) y = r sin (f)


 x’ = r cos (f  q) y = r sin (f  q) (x’,y’)

q (x,y)
 x’ = r cos (f  q) r
f
= r cos(f) cos(q) – r sin(f) sin(q)
= x cos(q) – y sin(q)
x’ = x cos(q) – y sin(q)
 y’ = r sin (f  q) y’ = y cos(q) + x sin(q)
= r sin(f) cos(q) + r cos(f) sin(q)
= y cos(q) + x sin(q)
Examples: Rotation
15

 Example 1: P = (4, 4) and q = 45 degrees


Solution: X’ = 4 cos(45) – 4 sin(45)
Y’ = 4 sin(45) + 4 cos(45)

 Example 2: Rotate a triangle with points P1(2, 2) , p2(5,


2) , p3(4, 4) by angle 45o.
Solution:
 Rotate point P1: X’= 2 cos(45) – 2 sin(45)
Y’= 2 sin(45) + 2 cos(45)
 What is P2 and P3 after rotation?
Rotate About a Fixed Point
16

 Rotation of ø Degrees About Point (x, y)


 Translate (x, y) to origin
 Rotate T  R  T -1
 Translate origin to (x, y)

(x,y) (x,y)
Homogeneous Co-ordinates
17

 Many graphics applications involve sequences of


transformations.
 For example an animation, might require an object to be
translated and rotated (Composite Transformation).
 Translation is performed with an addition, while scale and
rotation are performed with multiplication.
 We’d like to treat them all the same, then computations can be
optimized.
 Solution: homogeneous coordinate.

 The homogeneous co-ordinate system provides a uniform


framework for different geometric transformations.
 Standard in graphics software and hardware
Homogeneous Co-ordinates (Cont.)
18

 By using the homogeneous co-ordinate system a 2D point (x, y)


would be (x, y, 1).
 Translation for (tx, ty) is :  x   x  1 0 tx 
    
 y    y .0 1 ty 

1  
1 0 0 1 

 x  x  cosq  sinq 0
 Rotation about origin by q:  y    y .  sinq cosq 0
    

1   
1   0 0 1

 x'   Sx 0 0  x 
 Scaling with (Sx, Sy):  y '   0 Sy 0. y 
    
1   0 0 1  1 
Inverse Transformations
19

 Inverse Translation: T-1 (tx, ty) = T (– tx, – ty) x’ = x – tx


y’ = y – ty

Inverse Scaling: x’ = x / Sx
 S-1 (Sx, Sy) = S (1/Sx, 1/Sy)
y’ = y / Sy

x’ = x cos(q) + y sin(q)
 Inverse Rotation: R-1(q) = R (– q)
y’ = y cos(q) – x sin(q)
Reflection (1)
20

 Reflection about the x-axis:  Reflection about the y-axis:


x’= x, y’= y x’= x, y’= y
1 0 0  1 0 0
Re x  0 1 0 Re y   0 1 0
0 0 1  0 0 1
y
1 y
Original
Position Original Reflected
Position Position

2 3 2 2’

x 1 1’
2’ 3’
3 3’
Reflected
Position
x
1’
Reflection (2)
21

 A transformation that produces a mirror image of an object.


 It is produced by rotating the object by 180 deg about the rotating axis

 Reflection about the origin:  Reflection about the line x=y:


x’= x, y’= y x’= y, y’= x
y Original y
2 Position 3 Original Y=X
Position
2
1 3
1
x Reflected
2’ 1’ 1’
Position

Reflected 3’
Position 3’ 2’
x
Reflection Examples
22

y-axis
x-axis

Origin
Shearing
23

 Distorts the shape of an object.


 The transformed shape appears as if the object
were composed of internal layers that had
been caused to slide over each other.

 x-direction Shear:  y-direction Shear:


x’= x + shx . y x’= x
y’= y y’= y + shy . x
Where shx and
shy are Shear
parameters
Example: Shearing
24

 Shear the following shape with shear factor shx =


0.5
Composing Transformations
25

 The process of applying several


transformation in succession to form one
overall transformation

 For the Shape:


1. Scale(2, 2) then Translate(3, 1):
p' = T (Sp) = T Sp

2. Translate(3, 1) then Scale(2, 2):


p' = S (Tp) = S Tp
Composing Transformations: Example
26

 Rotate by 45 degree then Translate(3,0)


 Translate(3, 0) then Rotate by (45 deg).
To Summarize:
27
References
28

 John F. Hughes, Andries Van Dam, Morgan Mcguire, David F.


Sklar, James D. Foley, Steven K. Feiner and Kurt
Akeley,"Computer Graphics: Principles and Practice", 3th
Ed., 2014, Ch. 10, Page 233.
 Edward Angel and Dave Shreiner, “Interactive computer
graphics: A Top-Down Approach With Shader-Based
OpenGL”, 6th Ed., 2012, Ch.1, P. 5.
 Hearn Baker Carithers, "Computer Graphics with OpenGL",
4th Ed., Ch. 5, Page 190.
 Introduction to computer graphics by Bishnu Rawal, Chapter
2, Page 26.
29

You might also like