Sie sind auf Seite 1von 52

Computer Graphics

2D-Transformations
Contents

Why transformations
Transformations
• Translation
• Scaling
• Rotation
Homogeneous coordinates
Matrix multiplications
Combining transformations
Transformation

• What is transformations?

• The geometrical changes of an object from a current state to modified state.


Why Transformations?

• To manipulate the initially created object and to display the modified object
without having to redraw it.
Translation

Simply moves an object from one position to another


xnew = xold + dx ynew = yold + dy

y
6

0
1 2 3 4 5 6 7 8 9 10 x

Note: House shifts position relative to origin


2D Translation

Y 6 5 
6 
 
5
4
dx = 2 3 3
3
dy = 3  
2
1

0
1 2 3 4 5 6 7 8 9 10
X

x   x'  dx 
v   , v'   , t   
Component-wise addition of vectors  y  y ' dy 

v’ = v + t where
x’ = x + dx
y’ = y + dy
Scaling

Scalar multiplies all coordinates


WATCH OUT: Objects grow and move!
xnew = Sx × xold ynew = Sy × yold

y
6

3 6  9 
3 3
   
2

1 2 3
1  1
   
0
1 2 3 4 5 6 7 8 9 10 x

Note: House shifts position relative to origin


2D Scaling

sx  3
sy  2

2 3
6  9 
1  1 2 2
       

• Component-wise scalar multiplication of vectors

v’ = Sv where x   x' 
v   , v'   
 y  y '

and x'  s x x
sx 0
S  
0 sy 
 y'  s y y
Rotation

Rotates all coordinates by a specified angle


xnew = xold × cosθ – yold × sinθ
ynew = xold × sinθ + yold × cosθ
Points are always rotated about the origin

2


1 6
x
0
1 2 3 4 5 6 7 8 9 10
Rotation
Consider rotation about the origin by q degrees
• radius stays the same, angle increases by q

x  r cos(   )  r cos  cos   r sin  sin 


y  r sin(   )  r cos  sin   r sin  cos 

x’ = r cos (φ +  )
y’ = r sin (φ+  )

x’=x cos –y sin 


y’ = x sin  + y cos
x = r cos φ
y = r sin φ
2D Rotation


Rotate by q
 
6
about the origin.

x   x' 
v   , v'   
 y  y '
v’ = Rθ v where cos 
R  
 sin  
sin  cos   

and x’ = x cos Ө – y sin Ө


y’ = x sin Ө + y cos Ө
Homogeneous Coordinates

• A point (x, y) can be re-written in homogeneous


coordinates as (xh, yh, h)
• The homogeneous parameter h is a non-
zero value such that:

xh yh
•We can then  any point (x, y)yas(hx, hy, h)
x write
h
•We can conveniently choose hh = 1 so that
(x, y) becomes (x, y, 1)
Homogeneous Coordinates

• P2d is intersection of line determined by Ph with the w = 1 plane


W

Ph (x,y,w)

x  1

• What is
 y ? P2d (x/w,y/w,1)
 
w X

• Infinite number of points correspond to (x, y, 1) : they


constitute the whole line (tx, ty, tw)
Why Homogeneous Coordinates?

A number of transformations can be combined into one


matrix to make things easy

Using homogeneous coordinates allows us to use the


matrix multiplication to calculate transformations
extremely efficient!

We will see in a moment that all of the transformations


we discussed previously can be represented as 3*3
matrices.
Matrix Multiplication

Recall how matrix multiplication takes place:

a b c   x  a * x  b * y  c * z 
d e    
f    y   d * x  e * y  f * z 

 g h i   z   g * x  h * y  i * z 
Homogeneous Translation

The translation of a point by (dx, dy) can be written in


matrix form as: 1 0 dx 
0 1 dy 

0 0 1 
Representing the point as a homogeneous column
vector we perform the calculation as:

1 0 dx   x  1* x  0 * y  dx *1  x  dx 
0 1 dy    y   0 * x  1* y  dy *1   y  dy 
       
0 0 1  1   0 * x  0 * y  1*1   1 
Homogenous Coordinates

To make operations easier, 2-D points are written as


homogenous coordinate column vectors.

1 0 dx   x   x  dx 
     
Translation: 0 1 dy    y    y  dy  : v'  T (dx, dy )v
0 0 1  1   1 

sx 0 0  x   sx  x 
 0 sy 0   y   sy  y  : v'  S (sx, sy )v
Scaling:
     
 0 0 1 1   1 
Homogenous Coordinates (cont…)

Rotation:

cos   sin  0  x  cos   x  sin   y 


 sin  cos  0   y   sin   x  cos   y  : v'  R( )v

 0 0 1 1   1 
Inverse Transformations

Transformations can easily be reversed using inverse


transformations.
1 
1 0  dx  s 0 0
 x 
1  
T  0 1  dy  S 1
0
1
0
 sy 
0 0 1  0

0 1

 
 cos  sin  0
R 1   sin  cos  0
 0 0 1
Composite Transformations

•Setting up a matrix for any sequence of Transformation


•Forming products of transformation matrices is concatenation or composition of matrices

Composite translations:

   
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 
 2y   1y   1y 2y 

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

T  t2 x , t2 y   T  t1x , t1 y   T  t1x  t2 x , t1 y  t2 y 
Two successive Translations are Additive
Composite Transformations

Composite Rotations: P  R  2  R 1   P  R  2   R 1   P

R  2   R 1   R 1   2 

P  R 1   2   P
Two successive Rotations are Additive

Composite Scaling:
 S2 x 0 0   S1x 0 0   S1x  S2 x 0 0
 0 S2 y 0    0 S1 y 0    0 S1 y  S2 y 0 

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

S  S2 x , S2 y   S  S1x , S1 y   S  S1x  S2 x , S1 y  S2 y 

Two successive Scaling are Multiplicative


Composite Transformations

Concatenation Properties

Matrix multiplication is associative

A . B . C = ( A . B ) .C = A . ( B . C )

Transformed products may not be commutative

A.B=B.A
Composite Transformations

y
Order of Transformations

x
Object is first rotated by 90º and then
translated in the x direction
y

x
Object is first translated in the x direction
and then rotated by 90º
Composite Transformations

Imagine rotating a polygon around a point other than the


origin
• Transform the centre point to origin
• Rotate around origin
• Transform back to centre point
Composite Transformations (cont…)

House (H ) T (dx,dy ) H
1 2

T (dx, dy ) R( )T (dx,dy ) H R( )T (dx,dy ) H

4
3
Pivot -Point Rotation
Pivot-Point Rotation

(xr,yr) (xr,yr) (xr,yr) (xr,yr)

Translate Rotate Translate

T xr , yr  R  T  xr , yr   Rxr , yr , 


1 0 x r  cos   sin  0 1 0  x r  cos   sin  x r (1  cos  )  y r sin  
0 1 y    sin  cos  0  0 1  y    sin  cos  y (1  cos  )  x sin  
 r    r  r r 
0 0 1   0 0 1 0 0 1   0 0 1 
General Fixed-Point Scaling
General Fixed-Point Scaling

(xf,yf) (xf,yf) (xf,yf) (xf,yf)

Translate Scale Translate

T x f , y f  S s x , s y  T  x f , y f   S x f , y f , s x , s y 
1 0 xf  s x 0 0 1 0  x f   s x 0 x f (1  s x ) 
0 1 yf 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 
2D Reflection

In case of reflection, the image formed is on the opposite side of the


reflective medium with the same size. Therefore we use the identity
matrix with positive and negative signs according to the situation
respectively.
2D Reflection

The reflection about the x-axis can be shown as

The reflection about the y-axis can be shown as


2D Reflection

• The reflection about the origin can be shown as


2D Reflection

y y=x
• With respect to the line y = x

 x   0 1 0  x 
    
y
    1 0 0  y 
 1   0 0 1  1 
     x

• With respect to the line y = -x


y  x
2D Shearing

A transformation that distorts the shape of an object such that the


transformed shape is slided over each other is called a shear.
2D Shearing

The shear transformation in the x-direction with shearing factor shx is the
following:

The shear transformation in the y-direction with shearing factor shy is the
following:
y y
3

1 2
1

1 x 1 x
Initial object shy = 2
2D Shearing
• x-direction relative to other reference line
y
x  x  shx ( y  yref )
1
y  y
1 x
yref = -1

Matrix form y

 x   1 shx shx  yref  x  1


    
 y   0 1 0  y 
 1  0 0  1 
   1   1 2 3 x
yref = -1

shx = ½, yref = -1
2D Shearing
• y-direction relative to other reference line y
x  x
y  y  shy ( x  xref )
1

xref = -1 1 x
Matrix form
y
 x   1 0 0  x  2
    
 y    shx 1  shy  xref  y 
1  0  1  1
   0 1  
xref = -1 1 x
shy = ½, xref = -1
Transformations between 2D Coordinate Systems

y0

x0
x

• To translate object descriptions from xy coordinates to x’y’


coordinates, we set up a transformation that superimposes the x’y’
axes onto the xy axes. This is done in two steps:
1. Translate so that the origin (x0, y0) of the x’y’ system is moved to
the origin (0, 0) of the xy system.
2. Rotate the x’ axis onto the x axis.
Transformations between 2D Coordinate Systems

y
1)
 1 0  x0 
 
T ( x0 ,  y0 )   0 1  y0  y0

2) 0 0 1 
 
x0
x
Concatenating: 
cos  sin  0 y
 
R( )    sin  cos  0
 0 1 
 0

x
M xy , xy  R( )  T ( x0 ,  y0 )
Equations

Translation:
xnew = xold + dx ynew = yold + dy

Scaling:
xnew = Sx × xold ynew = Sy × yold

Rotation:
xnew = xold × cosθ – yold × sinθ
ynew = xold × sinθ + yold × cosθ
Exercises 1

Translate the shape below by (7, 2)


y

(2, 3)
3

(1, 2) (3, 2)
2

1
(2, 1)

0 x
1 2 3 4 5 6 7 8 9 10
Exercises 2

Scale the shape below by 3 in x and 2 in y


y

4
(2, 3)

3
(1, 2) (3, 2)

1
(2, 1)

x
0 1 2 3 4 5 6 7 8 9 10
Exercises 3

Rotate the shape below by 30° about the origin


y

4
(7, 3)

3
(6, 2) (8, 2)

1
(7, 1)

x
0 1 2 3 4 5 6 7 8 9 10
2D Homogeneous Coordinate Transformations

• Translate [1,3] by [7,9]

1 0 7 1  8 
0 1 9  3  12
     
0 0 1 1  1 

• Scale [2,3] by 5 in the X direction and 10 in the Y direction

5 0 0 2 10 
0 10 0  3   30
     
0 0 1 1  1 
2D Homogeneous Coordinate Transformations

• Rotate [2,2] by 90° (π/2)

cos( / 2)  sin(  / 2) 0 2 0  1 0 2  2


 sin(  / 2) cos( / 2) 0  2  1 0 0  2  2 
         
 0 0 1 1  0 0 1 1  1 
Problem-1
(a) Find the matrix that represents rotation of an
object by 30o about the origin
(b)What are the new coordinates of the point P(2,-4)
after the rotation
Soln:
A)R30o

cos 30  sin 30  3/ 2 1/ 2 



B) The new coordinatescan be found by multiplying  
 sin 30 cos 30   1/ 2 3 / 2

 3/2  1 / 2  2   32 
   
 1/ 2 3 / 2   4  1  2 3 
Problem-2
• 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
• Soln

Formula:Scale by sx, sy around fixed point (x,y)


M  T(x, y)  S(sx, sy)  T(- x,- y)

1 0 5 2 0 0 1 0  5 2 0  5
0 1 2  0 2 0  0 1  2  0 2  2
       
0 0 1 0 0 1 0 0 1  0 0 1 
Problem-2(contd…)
•Represent point P with coord(x,y) by the
column vector  xy  we have,

1 

2 0  5 0   5 
0 2  2
0  
  2 

  
•S2,2.A= 0 0 1  1  
1  
2 0  5 1  3
0 2  2
1  0 
•S2,2.B=    


0 0 1  1 1 
2 0  5 5  5 
•S2,2.C= 0 2  2
2  2
 

  
0 0 1  1  
1 
Problem-3
• Consider the line with endpoints (10, 10) and (30, 25).
Translate it by tx = -20, ty = -10 and then rotate it by θ = 90º.

y
(30, 25)

(10, 10)

x
Right-to-left

 x   cos 90  sin 90 0  1 0 20  x 


     
y
    sin 90 cos 90 0  0 1 10  y 
1  0  0 0 1  1 
   0 1   
Solution
 x   cos 90  sin 90 0  1 0 20  x 
     
y
    sin 90 cos 90  0  0 1 10  y 
1  0 1   
   0  0 0 1  1 

 0 1 0  1 0 20  x 
   
  1 0 0  0 1 10  y 
 0 0 1  0 0 1  1 
   
y
 0 1 10  x 
   (30, 25)
  1 0 20  y 
0 0  1 
 1   (10, 10)

x
Solution (continue)
 x   0 1 10  x  y
    
y
    1 0 20  y  (30, 25)
 1  0 0  1 
   1   (-15, 10)
(10, 10)
x
Point (10, 10) (0, -10)

 x   0 1 10 10   0 
      
y
    1 0 20 10
   10 
 1  0 0  1   1 
   1    
Point (30, 25)

 x   0 1 10  30   15 
      
y
    1 0 20 25
   10 
 1  0 0 1    
    1   1 
Problem-4,5,6,7
4. Given rectangular coordinates are (2, 1), (5,1), (5, 3) and (2, 3). Scale the given
rectangular by twice of its size with the reference point (2, 1). Check the displacement of
the rectangle. If it is displaced then give the reason, if it is not displaced give the reason.
5. Given triangles three vertexes are not on the origin. You are asked to rotate the triangle
about 60o in anti clock wise direction with respect to any one of the vertex of the triangle.
What type of rotation it is? Derive the formula for the rotation to be performed by you.
6. The Triangular coordinates are (2, 1), (6, 1) and (4, 4). Apply the following transformation
individually and find out the transformed coordinates for the transformations.
a)Translate the given triangle towards x axis 3 units and towards y axis 2 units.
b)Perform scaling to reduce the triangle to its half of its size.
c)Rotate the triangle about 45 degree in anti clock wise direction with respect to origin.
7. Given Rectangular coordinates are (3, 2), (7, 2), (7, 4) and (3, 4). Perform the following
transformation individually.
a)Perform translation towards x axis 3 units and towards y axis 4 units.
b)Perform scaling to make the given rectangle twice of its given size.
c)Rotate the given triangle about 60 degree towards anti clock wise direction with
respect to origin.
Problem-8

8. Construct a triangle ABC whose coordinates are A(4,1), B(5,2) and C(4,3)
(i). Reflect the given triangle about X axis
(ii). Reflect the given triangle about Y axis
(iii). Reflect the given triangle about Y=X axis
In each case find the Coordinates of reflected triangle
The End

Das könnte Ihnen auch gefallen