Вы находитесь на странице: 1из 60

Computer Graphics

2D TRANSFORMATIONS
By
Dr. Rajeev Srivastava
CSE, IIT(BHU)

2D Transformations
What is transformations?
The geometrical changes of an object from a
current state to modified state.

Why the transformations is needed?


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

2D Transformations
2 ways
Object Transformation
Alter the coordinates descriptions an object
Translation, rotation, scaling etc.
Coordinate system unchanged

Coordinate transformation
Produce a different coordinate system

Matrix Math
Why do we use matrix?
More convenient organization of data.
More efficient processing
Enable the combination of various
concatenations

Matrix addition and subtraction


a
b

c
d

a c
b d

Matrix Math
How about it?
d
a c
b

Matrix Multiplication
Dot product
a
c

b
d

e
g

f
= a.e + b.g
h
c.e + d.g

a.f + b.h
c.f + d.h

Matrix Math
What about this?
1

2
3

1
3

1
3

Type of matrix
a

Row-vector

2 =
1

2
1

tak boleh!!

a
b
Column-vector

Matrix Math
Is there a difference between possible
representations?
a b e ae + bf

= +
c d f ce df

[e

a b
f ]
= [ae + cf
c d

be + df ]

[e

a c
f ]
= [ae + bf
b d

ce + df ]

Matrix Math
Well use the column-vector representation for a
point.
Which implies that we use pre-multiplication of
the transformation it appears before the point to
be transformed in the equation.
A B x Ax + By
C D y = Cx + Dy

What if we needed to switch to the other


convention?

Translation
A translation moves all points
in an object along the same
straight-line path to new
positions.
The path is represented by a
vector, called the translation or
shift vector.
We can write the components:
p'x = px + tx
p'y = py + ty
or in matrix form:
P' = P + T
tx
x
x
y = y + ty

?
ty=4
(2, 2)

tx= 6

Rotation
A rotation repositions
all points in an object
along a circular path in
the plane centered at
the pivot point.

First, well assume the


pivot is at the origin.

Rotation
Review Trigonometry
=> cos = x/r , sin = y/r
x = r. cos , y = r.sin
=> cos (+ ) = x/r
x = r. cos (+ )
x = r.coscos -r.sinsin
x = x.cos y.sin
=>sin (+ ) = y/r
y = r. sin (+ )
y = r.cossin + r.sincos
y = x.sin + y.cos

P(x, y)

P(x,y)

x
Identity of Trigonometry

Rotation
We can write the components:
p'x = px cos py sin
p'y = px sin + py cos
P(x, y)

or in matrix form:
P' = R P
can be clockwise (-ve) or
counterclockwise (+ve as our
example).
Rotation matrix

cos
R=
sin

sin
cos

P(x,y)

Rotation
Example
Find the transformed point, P, caused by
rotating P= (5, 1) about the origin through an
angle of 90.
cos
sin

sin x x cos y sin


=

cos y x sin + y cos


5 cos 90 1 sin 90
=

5 sin 90 + 1 cos 90

5 0 11
=

5 1 + 1 0
1
=
5

Scaling

Scaling changes the size of an


object and involves two scale
factors, Sx and Sy for the xand y- coordinates
respectively.
Scales are about the origin.
We can write the components:
p'x = sx px
p'y = sy py
or in matrix form:
P' = S P
Scale matrix as:
sx
S=
0

0
s y

Scaling

If the scale factors are in between 0


and 1 the points will be moved
closer to the origin the object
will be smaller.
Example :
P(2, 5), Sx = 0.5, Sy = 0.5
Find P ?

P(2, 5)

Scaling

If the scale factors are in between 0


and 1 the points will be moved
closer to the origin the object
will be smaller.
Example :
P(2, 5), Sx = 0.5, Sy = 0.5
Find P ?
If the scale factors are larger than 1
the points will be moved away
from the origin the object will be
larger.
Example :
P(2, 5), Sx = 2, Sy = 2
Find P ?

P(2, 5)

Scaling
If the scale factors are the same,
Sx = Sy uniform scaling
Only change in size (as previous
example)

If Sx Sy differential scaling.
Change in size and shape
Example : square rectangle
P(1, 3), Sx = 2, Sy = 5 , P ?

P(1, 2)

What does scaling by 1 do?


What is that matrix called?
What does scaling by a negative value do?

Combining transformations
We have a general transformation of a point:
P' = M P + A

When we scale or rotate, we set M, and A is the additive


identity.
When we translate, we set A, and M is the multiplicative
identity.
To combine multiple transformations, we must explicitly
compute each transformed point.
Itd be nicer if we could use the same matrix operation all the
time. But wed have to combine multiplication and addition
into a single operation.

Homogenous Coordinates
y

w
x

Lets move our problem into 3D.


Let point (x, y) in 2D be represented by point (x, y, 1) in the
new space.
Scaling our new point by any value a puts us somewhere along
a particular line: (ax, ay, a).
A point in 2D can be represented in many ways in the new
space.
(2, 4) ---------- (8, 16, 4) or (6, 12, 3) or (2, 4, 1) or etc.

Homogenous Coordinates
We can always map back to the original 2D point by dividing
by the last coordinate
(15, 6, 3) --- (5, 2).
(60, 40, 10) - ?.
Why do we use 1 for the last coordinate?
The fact that all the points along each line can be mapped
back to the same point in 2D gives this coordinate system its
name homogeneous coordinates.

Matrix RepresentationHomogeneous Transformations


Point in column-vector:
x
y
1
Our point now has three coordinates. So our matrix is
needs to be 3x3.
Translation

x 1 0 t x x
y = 0 1 t y
y

1 0 0 1 1

Matrix Representation
Rotation

x cos
y = sin

1 0

sin
cos
0

0 x
0 y
1 1

Scaling

x s x
y = 0

1 0

0
sy
0

0 x
0 y
1 1

Inverse Transformations

1 0 t x
cos
T1 = 0 1 t y , R 1 = sin
0 0 1
0

sin
cos
0

0
0
1 S x
0 , S 1 = 0 1 S y
0
1
0

0
0
1

Composite Transformation
We can represent any sequence of transformations
as a single matrix.
No special cases when transforming a point matrix
vector.
Composite transformations matrix matrix.

Composite transformations:
Rotate about an arbitrary point translate, rotate,
translate
Scale about an arbitrary point translate, scale,
translate
Change coordinate systems translate, rotate, scale

Does the order of operations matter?

Composition Properties
Is matrix multiplication associative?
?

(A.B).C = A.(B.C)
a b e

c d g

f i

h k

j ae + bg
= +
l ce dg

af + bh i

cf + dh k

aei + bgi + afk + bhk


=
cei + dgi + cfk + dhk
a b e


c d g

f i

h k

l
aej + bgj + afl + bhl

cej + dgj + cfl + dhl

j a b ei + fk ej + fl
=
+

l c d gi hk gj + hl
aei + afk + bgi + bhk aej + afl + bgj + bhl
=

+
+
+
+
+
+
cei cfk dgi dhk cej cfl dgj dhl

Composition Properties
Is matrix multiplication commutative?
?

A.B=B.A

f ae + bg
=

h ce + dg

af + bh
cf + dh

f a b ea + fc

h c d ga + hc

eb + fd

gb + hd

a b e
c d g


e
g

Order of operations
So, it does matter. Lets look at an example:
1.
2.

Translate
Rotate

1.
2.

Rotate
Translate

Composite Transformations
Translations
P' = T (tx 2 , t y 2 ) {T (tx1, t y1 ) P}
= {T (tx 2 , t y 2 ) T (tx1, t y1 )} P
1 0 t x 2 1 0 t x1 1 0 t x1 + t x 2
0 1 t 0 1 t = 0 1 t + t
y2
y1
y1
y2

1
0 0 1 0 0 1 0 0

T (tx 2 , t y 2 ) T (tx1, t y1 ) = T (tx1 + tx 2 , t y1 + t y 2 )

Composite Transformations
Scaling
sx 2
0

0
sy2
0

0 sx1
0 0

1 0

0 0 sx1 sx 2
s y1 0 = 0

0 1 0

0
s y1 s y 2
0

S (s x 2 , s y 2 ) S (s x1 , s y1 ) = S (s x1 s x 2 , s y1 s y 2 )

0
0

Composite Transformations
Rotations
P' = R(2 ) {R(1 ) P}
= {R(2 ) R(1 )} P
cos 2
sin
2

sin 2
cos 2
0

0 cos 1 sin 1 0 cos(1 + 2 ) sin(1 + 2 ) 0


0 sin 1 cos 1 0 = sin(1 + 2 ) cos(1 + 2 ) 0

1 0
0
1
0
0
1

R(2 ) R(1 ) = R(1 + 2 )

Composite Transformation Matrix

Arrange the transformation matrices in order from right to left.

General Pivot- Point Rotation

Operation :1. Translate (pivot point is moved to origin)


2. Rotate about origin
3. Translate (pivot point is returned to original position)
T(pivot) R() T(pivot)

1
0
0

0
1
0

tx
ty
1

1
0
0

0
1
0

tx
ty
1

cos -sin 0
sin cos 0
0
0
1

1
0
0

0
1
0

-tx
-ty
1

cos -sin -tx cos+ ty sin


sin cos -tx sin - ty cos
0
0
1

cos -sin -tx cos+ ty sin + tx


sin cos -tx sin - ty cos + ty
0
0
1

General Pivot-Point Rotation


Rotations about any selected pivot point (xr,yr)
Translate-rotate-translate

General Pivot-Point Rotation


1 0
0 1

0 0

xr cos
yr sin

1 0

cos
= sin

sin
cos
0

sin
cos
0

0 1 0 xr
0 0 1 yr

1 0 0 1
xr (1 cos ) + yr sin
yr (1 cos ) xr sin

T ( xr , yr ) R( ) T ( xr , yr ) = R( xr , yr , )

General Pivot Point Rotation


Example
Perform 60 rotation of a point P(2, 5) about a
pivot point (1,2). Find P?
cos -sin -tx cos+ ty sin + tx
sin cos -tx sin - ty cos + ty
0
0
1

0.5
-0.866 -1.0.5 + 2.0.866 + 1
0.866 0.5 -1.0.866- 2.0.5 + 2
0
0
1
0.5 - 0.866 2.232
0.866 0.5 0.134
0
0
1

2
5
1

x
y
1

Sin 60 = 0.8660
Cos 60 = 1/2

2
5
1
-1.098
4.366
1

P = (-1, 4)

Without using composite


homogenous matrix
Example
Perform 90 rotation of a point P(5, 1) about a pivot
point (2, 2). Find P?

1. Translate pivot point

( tx = -2, ty = -2)

P(5, 1 ) P (3, -1)

2. Rotate P = 90 degree
P(3, -1) -- > cos 90 -sin 90 3 = 0
-1 3 = 1
sin 90 cos 90 -1
1
0 -1
3
3. Translate back pivot point (tx = 2 , ty = 2)

original point (1, 3 ) transformed point (3, 5)

Composite Transformation Matrix


General Fixed-Point Scaling
Operation :1. Translate (fixed point is moved to origin)
2. Scale with respect to origin
3. Translate (fixed point is returned to original position)
T(fixed) S(scale) T(fixed)
Find the matrix that represents scaling of an
object with respect to any fixed point?

General Fixed-Point Scaling


Scaling with respect to a selected fixed position (xf,yf)

General Fixed-Point Scaling


Translate-scale-translate
1 0 xr sx 0 0 1 0 xr
0 1 y 0 s 0 0 1 y
r
y
r


0 0 1 0 0 1 0 0 1
sx 0 x f (1 sx )
= 0 s y y f (1 s y )

1
0 0

T ( x f , y f ) S ( sx , s y ) T ( x f , y f ) = S ( x f , y f , xr , yr )

Given P(6, 8) , Sx = 2, Sy = 3 and fixed point (2, 2). Use


composite transformation matrix to find P?
1
0
0
1
0
0

0
1
0
0
1
0

tx
ty
1
tx
ty
1

.
.

Sx 0
0 Sy
0 0

0
0
1

1
0
0

0
1
0

-tx
-ty
1

Sx 0 -tx Sx
0 Sy -ty Sy
0 0
1

x =6, y = 8, Sx = 2, Sy = 3, tx =2, ty = 2

2
0
0

0
3
0

-2( 2) + 2
-2(3) + 2
1

6
8
1

10
20
1

Sx 0 -tx Sx + tx
0 Sy -ty Sy + ty
0 0
1

Composite Transformation Matrix


General Scaling Direction
Operation :1. Rotate (scaling direction align with the coordinate axes)
2. Scale with respect to origin
3. Rotate (scaling direction is returned to original position)
R() S(scale) R()
Find the composite transformation matrix?

General Scaling Directions


Scaling factors sx and sy scale objects along the x and y
directions.
We scale an object in other directions with scaling
factors s1 and s2

General Scaling Directions

Another Example
Rotation followed by translation followed by
scaling

Sx 0
0 Sy
0 0

0
0
1

Sx 0
0 Sy
0 0

0
0
1

Sxcos
Sy sin
0

T
0
1
0

1
0
0

tx
ty
1

cos -sin tx
sin cos ty
0
0
1

Sx(-sin) Sx tx
Sy cos Sy ty
0
1

R
cos -sin 0
sin cos 0
0
0
1

Other transformations
Reflection:
x-axis

y-axis

1 0 0
0 1 0

0 0 1

1 0 0
0 1 0

0 0 1

Other transformations
Reflection:
origin
1 0 0
0 1 0

0 0 1

line x=y
0 1 0
1 0 0

0 0 1

Reflection
A transformation produces a mirror image of an object.
Axis of reflection
A line in the xy plane
A line perpendicular to the xy plane
The mirror image is obtained by rotating the object 1800 about
the reflection axis.

Rotation path
Axis in xy plane: in a plane perpendicular to the xy plane.
Axis perpendicular to xy plane: in the xy plane.

Reflection
Reflection about the x axis

Reflection
Reflection about the y axis

Reflection
Reflection relative to the coordinate origin

Reflection
Reflection of an object relative to an axis perpendicular
to the xy plane through Prfl

Reflection
Reflection about the line y = x

Other transformations
Shear:
x-direction
1 shx
0 1

0 0

0
0
1

y-direction
1
sh
y
0

0 0
1 0
0 1

Shear
The x-direction shear relative to x axis
1 shx
0 1

0 0
If shx = 2:

0
0

x ' = x + shx y
y' = y

Shear
The x-direction shear relative to y = yref
1 shx
0 1

0 0

shx yref

x ' = x + shx ( y yref )


y' = y

If shx = yref = -1:


1

1/2

3/2

Shear
The y-direction shear relative to x = xref
1
sh
y
0

0
0

1 shy xref

0
1

x' = x

y ' = shy ( x xref ) + y


3/2

If shy = xref = -1:

1/2
1

Coordinate System Transformations

We often need to transform points from one coordinate system to


another:
1. We might model an object in non-Cartesian space (polar)
2. Objects may be described in their own local system
3. Other reasons: textures, display, etc

Transformations between Coordinate


Systems

x
y

y0

x0

Transformations between Coordinate


Systems
Method 1:

1 0 x0
T ( x0 , y0 ) = 0 1 y0

1
0 0

cos
R( ) = sin

sin
cos
0

M xy , x ' y ' = R( ) T ( x0 , y0 )
Method 2:

V
v=
= (vx , v y )
V
u = ( v y ,v x ) = ( u x , u y )

u x
R = vx

uy
vy
0

0
0

0
0

END

Вам также может понравиться