Sie sind auf Seite 1von 46

Department of Computer Science and Engineering (CSE)

Computer Graphics

2-D Transformations

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Outline
2-D Transformations
Transformation principles
Homogeneous coordinate systems
Concatenations
Matrix representations of transformations.

University Institute of Engineering (UIE) 2


Department of Computer Science and Engineering (CSE)

Transformations

The term transform means to change. This change can either be of shape, size
or position of the object. To perform transformation on any object, object
matrix X is changed by the transformation matrix T.
[X*]=[X] [T]
[X*] is matrix of transformed object.
[X] is a matrix of original object
[T] is transformation matrix.
The various transformations possible on an object are:
Translation
Rotation Basic Transformations
Scaling
Reflection
Shearing Other Transformations

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Transformations

A transformation on an object is an operation that changes how the object is


finally drawn to screen
There are two ways of understanding a transformation
An Object Transformation alters the coordinates of each point
according to some rule, leaving the underlying coordinate system
unchanged
A Coordinate Transformation produces a different coordinate system,
and then represents all original points in this new system

University Institute of Engineering (UIE) 4


Department of Computer Science and Engineering (CSE)

Basic classes of transformations

There are three basic classes of transformations:


1. Rigid body - Preserves distance and angles.
Examples: translation and rotation.
2. Conformal - Preserves angles.
Examples: translation, rotation, and uniform scaling.
3. Affine - Preserves parallelism. Lines remain lines.
Examples: translation, rotation, scaling, shear, and reflection.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Elementary Transformations

Affine transformations are usually combinations of four elementary


transformations:

1: Translation

2: Scaling

3: Rotation

4: Shearing

University Institute of Engineering (UIE) 6


Department of Computer Science and Engineering (CSE)

Translation

A translation moves an object into a different position in a scene


tx
This is achieved by adding an offset/translation vector t
ty
Qx Px t x
Q P t
In Vector notation: y y y

Translate by t

Original points Transformed points

University Institute of Engineering (UIE) 7


Department of Computer Science and Engineering (CSE)

Scaling

A scaling changes the size of an object with two scale factors, Sx and Sy (if
Sx != Sy, then we have a non-uniform scale)

Qx Px S x
Q P S
y y y

Sx, Sy

Scale by
Sx, Sy
Original points Transformed points

University Institute of Engineering (UIE) 8


Department of Computer Science and Engineering (CSE)

Rotation

Using the trigonometric relations, a point rotated by an angle q about the


origin is given by the following equations:

Qx Px cos Py sin Qy Px sin Py cos


So
Qx Px cos Py sin
Q P sin P cos
y x y

Rotate by

Original points Transformed points

University Institute of Engineering (UIE) 9


Department of Computer Science and Engineering (CSE)

Rotation - derivation
QX Rcos( ) [1]
Q y Rsin( ) [2]

PX R cos ( ) [3]
Py R sin ( ) [4]
Q
cos ( ) cos () cos () sin () sin ()

sin( ) sin () cos () cos () sin ()


PY P
[1] Qx Rcos ( ) cos ( ) Rsin ( ) sin ( )

PX Substituting from [3] and [4]


R
Qx Pxcos ( ) Py sin ( )
Similarly from [2]
Qy Pycos ( ) Px sin ( )
Background info

University Institute of Engineering (UIE) 10


Department of Computer Science and Engineering (CSE)

Other transformations: Reflection

A reflection is a transformation that produces a mirror image of an object.


The mirror image for a two-dimensional reflection is generated relative to an
axis of reflection by rotating the object 180 degree about the reflection axis

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

2D Reflections

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

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

yx y x

0 1 0 0 1 0
1 0 0 1 0 0

0 0 1 0 0 1

University Institute of Engineering (UIE) 13


Department of Computer Science and Engineering (CSE)

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

University Institute of Engineering (UIE) 14


Department of Computer Science and Engineering (CSE)

Other transformations

Reflection:
origin line x=y

1 0 0 0 1 0
0 1 0 1 0 0

0 0 1
0 0 1

University Institute of Engineering (UIE) 15


Department of Computer Science and Engineering (CSE)

Shearing
A shearing affects an object in a particular direction (in 2D, its either in the
x or in the y direction),results only in distortion of shape.
A shear in the x direction would be as follows:

Qx Px hPy
Q P
y y
The quantity h specifies what fraction of the y-coordinate should be added to
the x-coordinate, and may be positive or negative
More generally: a simultaneous shear in both the x and y directions would be

Qx Px hPy
Q P gP
y y x

University Institute of Engineering (UIE) 16


Department of Computer Science and Engineering (CSE)

Shearing

g =0, h=0.5

Shear by {g,h}

Original points Transformed points

g=0.5, h=0.5

Shear by {g,h}

Original points Transformed points

University Institute of Engineering (UIE) 17


Department of Computer Science and Engineering (CSE)

Other transformations

Shear:
x-direction y-direction

1 shx 0 1 0 0
0 1 0 sh 1 0
y
0 0 1 0 0 1

University Institute of Engineering (UIE) 18


Department of Computer Science and Engineering (CSE)

Matrix Representation

All affine transformations in 2D can be generically described in terms of a


generic equation as follows:

Qx a b Px t x
Q P t
y c d y y
i.e.

Q MP t

University Institute of Engineering (UIE) 19


Department of Computer Science and Engineering (CSE)

Transformations Summary
1: Translation T
Qx 1 0 Px t x
Q P t
y 0 1 y y

T
2: Scaling
Qx S x 0 Px 0
Q 0
S y Py 0
y

3: Rotation T

Qx cos sin Px 0
Q P 0
y sin cos y

4: Shearing T
Qx 1 h Px 0
Q P 0

y g 1 y

University Institute of Engineering (UIE) 20


Department of Computer Science and Engineering (CSE)

Problem
An affine transformation is composed of a linear combination followed by a
translation
Unfortunately, the translation portion is not a matrix multiplication but must
instead be added as an extra term, or vector this is inconvenient

P = M1P + M2
where M1 takes care of rotation and scaling,
while M2 takes care of translation.
This is undesirable!

Thus, any point (a,b) in 2D Cartesian system is represented as (a,b,1) is


homogeneous coordinate system.
Any point (a,b,w) where w 0 is homogenous coordinate corresponds the
a b
point ( , ) in 2D Cartesian system.
w w

University Institute of Engineering (UIE) 21


Department of Computer Science and Engineering (CSE)

Homogeneous Coordinates & Matrix Representations


The trick we use is to add an additional component 1 to both P and Q, and
also a third row and column to M, consisting of zeros and a 1
i.e.
Qx a b t x Px
Q c d t P
y y y

1 0 0 1 1

Translation by Tx, Ty 1 0 t x Rotate by :


cos sin 0
M 0 1 t y M sin cos 0
0 0 1 0 0 1

S x 0 0 1 h 0
Scale by Sx, Sy
M 0 Sy 0
Shear by g, h: M g 1 0
0 0 1 0 0 1

University Institute of Engineering (UIE) 22


Department of Computer Science and Engineering (CSE)

Homogeneous Coordinates

Advantages
1. 2D transformations can be represented as 3x 3 matrices so that usage of the
operations become relatively easy.
2. It is a technique based on projective geometry.
3. Floating point arithmetic can be avoided by transforming them into integer
arithmetic.
1 1
e.g. ( , ) in 2D can be represented as (3,2,6) in homogeneous coordinates.
2 3

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Multiple Transformations

It is rare that we want to perform just one elementary transformation.


Usually an application requires that we build a complex transformation out of
several elementary ones
e.g. translate an object, rotate it, and scale it, all in one move
These individual transformations combine into one overall transformation
This is called the composition of transformations.
The composition of two or more affine transformations is also an affine
transformation

University Institute of Engineering (UIE) 24


Department of Computer Science and Engineering (CSE)

Rotation and Scaling

The simple versions of rotation and scaling have been based around the
origin.
This means that when we rotate or scale, the object will also move, with
respect to the origin

Not only is the object


rotated, but it also
moves around the origin

University Institute of Engineering (UIE) 25


Department of Computer Science and Engineering (CSE)

Pivotal points

Often we wish to rotate or scale with respect to some pivotal point, not the
origin
Most significantly, we often wish to rotate or scale an object about its
centre, or midpoint
In this way, the objects location does not change
To do this, we relate the rotation or scaling about the pivotal point V, to an
elementary rotation or scaling about the origin
We first translate all points so that V coincides with the origin
We then rotate or about the origin
then all points are translated back, so that V is restored to its original
location

University Institute of Engineering (UIE) 26


Department of Computer Science and Engineering (CSE)

{x1,y1}

{0,0}

To rotate the square by angle: about the point {x1, y1}

Translate all points through {-x1,-y1}


Rotate all points about the origin by
Translate all points back through {x1y1}

University Institute of Engineering (UIE) 27


Department of Computer Science and Engineering (CSE)

Rotation about a pivot point

1 0 x1 cos sin 0 1 0 x1
0 1 y sin cos
0 0 1 y1
1

0 0 1 0 0 1 0 0 1

cos sin x1 (1 cos ) y1 sin



sin cos
y1 (1 cos ) x1 sin
0 0 1

University Institute of Engineering (UIE) 28


Department of Computer Science and Engineering (CSE)

Useful compositions

Rotate about a pivot point:


T(pivot) R() T(pivot) P

Scale about a fixed point:


T(fixed) S(scale) T(fixed) P

General scaling directions:


R() S(scale) R() P

University Institute of Engineering (UIE) 29


Department of Computer Science and Engineering (CSE)

Concatenation Properties
Matrix multiplication is associative.
AB C = (AB )C = A(B C)
i.e. 2 3 4 (2 3) 4 2 (3 4)

Transformation products may not be commutative A B B A


Be careful about the order in which the composite matrix is evaluated.
Except for some special cases: i.e. A B B A
Two successive rotations
Two successive translations
Two successive scaling
rotation and uniform scaling

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Concatenation Properties

Reversing the order


A sequence of transformations is performed may affect the transformed
position of an object.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Order of operations

So, it does matter. Lets look at an example

University Institute of Engineering (UIE) 32


Department of Computer Science and Engineering (CSE)

General Pivot-Point Rotation

With a graphics package that only provides a rotate function for revolving
objects about the coordinate origin, we can generate rotations about any
selected pivot point (x, y,) by performing the following sequence of
translate-rotate translate operations:
1. Translate the object so that the pivot-point position is moved to the coordinate
origin.
2. Rotate the object about the coordinate origin.
3. Translate the object so that the pivot point is returned to its original position.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

General Pivot-Point Rotation

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

cos sin xr 1 cos yr sin



sin cos yr 1 cos xr sin
0 0 1

T ( x , y ) R ( ) T ( x , y ) R ( x , y , )
r r r r r r

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

General Fixed-Point Scaling


Scaling with respect to a selected fixed position (xf,yf)
1. Translate object so that the fixed point coincides with the coordinate origin.
2. Scale the object with respect to the coordinate origin.
3. Use the inverse translation of step 1 to return the object to its original position.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

General Fixed-Point Scaling

Translate-scale-translate

1 0 x s f x
0 0 1 0 x f

0 1 y 0 s 0 0 1 y
f y
f

0 0 1 0 0 1 0 0 1

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

0 0 1

T ( x , y ) S ( s , s ) T ( x , y ) S ( x , y , s , s )
f f x y f f f f
x y

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Examples on 2D Transformations

Consider a line A(2,3) and B(8,10).Obtain the coordinates of transformed


4 0
line using T .
0 1

2 3 4 0 8 3
[X*]=[X][T]= =
8 10 0 1 32 10

Perform a 45 rotation of object A(2,1), B(5,1),C(5,6) in anti-clockwise


direction and give the coordinates of the transformed objects.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Examples on 2D Transformations
2 1
[X]= 5 1

5 6
[T]=cos 45 sin 45
sin 45 cos 45

2 1 1 1 3 1
[X][T]= 5 1 2 2 = 1 6 4
1 1 2
5 6 11 1
2 2

A* 3 , 1 ,B* 6 , 4 , C* 11 , 1
2 2 2 2 2 2

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Examples on 2D Transformations

Find the transformed point, P, caused by rotating P= (5, 1) about the origin
through an angle of 90 clockwise.
[P]=[P][T]

cos 90 sin 90
= 5 1 sin 90 cos 90

= 5 1 0 1
1 0

= 1 5

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Examples on 2D Transformations

Consider the triangle ABC whose coordinates are A(4,1),B(5,2),C(4,3)


a)Reflect the triangle about x-axis.

b)Reflect the triangle about y-axis.

c)Reflect the triangle about y=x.

d)Reflect the triangle about line y=-x.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Examples on 2D Transformations
4 1 4 1
1 0
a) 5 2 0 1 = 5 2


4 3

4 3

4 1 4 1
b) 5 1 0 = 5 2
2 0 1


4 3
4 3

4 1 1 4
c) 5 0 1 =
2 1 0 2 5

4 3
3 4

4 1 1 4
0 1
d) 5 2 1 0 = 2 5

4 3 3 4
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)

Examples on 2D Transformations

Consider a line AB with coordinates A(1,1) and B(10,10).First reflect


the line about y axis and then about the line y=-x. In case 2,simply
rotate the line by - 270 (clockwise).Prove that transformed object in
both the cases is same.
1 1 1 0 0 1 1 1
[X*]=[X][T]= 10 10 0 1 1 0 = 10 10 CASE1

cos 270 0, sin 270 1
sin( ) sin cos( ) cos
1 1 cos(270) sin(270)
[X*]= [X] [T]= sin( 270)
10 10 cos(270)

cos 270 sin 270


1 1 0 1 1 1
= 10 10 1
sin 270 cos 270
0 = 10 10 CASE2

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Examples on 2D Transformations

Consider the object with coordinates A(2,4),B(3,1),C(5,3). Transform it by


first reflecting it about x-axis and then rotating it by clockwise 60
[X]= 2 4
3 1


5 3
1 3
1 0 cos 60 sin 60 1 0 2 2
[T]= [Refx] [R()]= = 0
0 1 sin 60 cos 60 1 3 1
2 2

(1 2 3 3 2)
2 4 1 3
(

3 1 2 2 = 3

3 1
(3 3 1)
3
1 2 2 2
5 3 2 2
1
(5 3 3)
1
(5 3 3)
2 2

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

FAQ

What is the use of 2D transformations?


Write matrix representations of 2D transformations.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

References
Computer Graphics C Version, Donald Hearn and M.Pauline Baker
Pearson Education.
Computer Graphics Principles and Practice C.foley, VanDam, Feiner and
Hughes, Pearson Education

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Thanks!

University Institute of Engineering (UIE) 46

Das könnte Ihnen auch gefallen