2 dimensional transformations

Preview:

DESCRIPTION

Document based on computer graphics.

Citation preview

December 9, 2007 2D Transformations ١

CS 380 “Computer Graphics”

2D Transformations

December 9, 2007 2D Transformations ٢

2D Transformations

• Transformation means changes in orientation, size, and shape.

• The basic geometric transformations are translation, rotation, and scaling. Other transformations that are often applied to objects include reflection and shear.

December 9, 2007 2D Transformations ٣

Transformations (1)

• Translation– moving things

• Rotation– moving about a point

by a given angle• rotation point may be origin

or some other chosen point

• Scaling– changing size

• relative to originor some other chosen point

December 9, 2007 2D Transformations ٤

Transformations (2)

• Translation– similar in 3D as 2D– we have tx, ty and now tz to add to each point

• Scaling– similar in 3D as 2D– we have Sx, Sy and Sz scale factors– can be relative to origin or point in 3D space

December 9, 2007 2D Transformations ٥

Homogenous coordinates

• 2D, 3 × 1 row matrices

• 3D, 4 × 1 row matrices

⎥⎥⎥

⎢⎢⎢

⎡=

1yx

P

⎥⎥⎥⎥

⎢⎢⎢⎢

=

1zyx

P

December 9, 2007 2D Transformations ٦

2D TRANSLATION (1)

• Moving all points of an object by the same X and Y factors– tx, and ty

– e.g. tx = 5 and ty = -5

Y

X

December 9, 2007 2D Transformations ٧

2D TRANSLATION (2)1. Translation: Repositioning of an object from one coordinate

location to another using translation vector (tx , ty) as follows:

x' = x + tx , y' = y + ty

where tx & ty are the translation distances, (x, y) the coordinate of old point & (x', y') the coordinate of new point.

Example: Let P = (5, 9), T = (tx, ty) = ( 6, 8) , so P' = (11, 17)

Note: To translate a line translate its endpoints.To translate polygon translate its vertices.Circle or Ellipse: Translate boundary points.

December 9, 2007 2D Transformations ٨

2D TRANSLATION (3)

• We think of a point as a column vector, written as a column of numbers between parentheses.

• We add vectors by adding corresponding coordinates.

( ) ⎟⎟⎠

⎞⎜⎜⎝

⎛−

=−2

32,3

⎟⎟⎠

⎞⎜⎜⎝

⎛=⎟⎟

⎞⎜⎜⎝

⎛+−+

=⎟⎟⎠

⎞⎜⎜⎝

⎛+⎟⎟⎠

⎞⎜⎜⎝

⎛− 5

47]2[

1371

23

December 9, 2007 2D Transformations ٩

2D TRANSLATION (4)• We can multiply a matrix by a vector.

– 2×2 matrix multiplied by 2-D vector:

• Translation matrix:

⎟⎟⎠

⎞⎜⎜⎝

⎛++

=⎟⎟⎠

⎞⎜⎜⎝

⎛⎟⎟⎠

⎞⎜⎜⎝

⎛dycxbyax

yx

dcba

⎟⎟⎟

⎜⎜⎜

⎛=

=

1001001

),(

with, ),('

y

x

yx

yx

tt

ttT

xttTx

December 9, 2007 2D Transformations ١٠

2D TRANSLATION (5)• 3×3 homogenous matrix representation of

translation:

⎥⎥⎥

⎢⎢⎢

⎡=

1001001

y

x

tt

T

⎥⎥⎥

⎢⎢⎢

⎡=

1yx

P

⎥⎥⎥

⎢⎢⎢

⎡++

=•=′

1y

x

tytx

PTP

December 9, 2007 2D Transformations ١١

2D Scaling (1)• Scaling: A scaling transformation alters the size of an

object. This operation can be carried out for polygons by multiplying the coordinate values (x, y) of each vertex by scaling factors sx and sy to produce the transformed coordinates (x', y') as follows:

x' = x × sx y' = y × sy

• The scaling is called uniform if sx = sy, so that the proportions are not affected. The scaling is called differential if sx ≠ sy.

December 9, 2007 2D Transformations ١٢

2D Scaling (2)1. Scaling relative to the origin.

x and y values multiplied by scaling factors sxand sy

)10,10( )10,20(

)20,20()20,10()20,20( )20,40(

)40,40()40,20(

December 9, 2007 2D Transformations ١٣

2D Scaling (3)

3 × 3 matrix

⎥⎥⎥

⎢⎢⎢

⎡=

1000000

y

x

ss

S

⎥⎥⎥

⎢⎢⎢

⎡=

1yx

P

⎥⎥⎥

⎢⎢⎢

⎡••

=•=′

1y

x

sysx

PSP

December 9, 2007 2D Transformations ١٤

2D Scaling (4)2. Scaling relative to a fixed point (xf, yf )

x' = xf + (x - xf )sx = x • sx + xf (l - sx)

y' = yf + (y - yf )sy = y • sy + yf (l – sy)

where xf (l - sx) & yf (l – sy) are constants for all points in the object.

December 9, 2007 2D Transformations ١٥

2D Scaling (5)

⎥⎥⎥

⎢⎢⎢

⎥⎥⎥

⎢⎢⎢

⎡−−

=⎥⎥⎥

⎢⎢⎢

1.

100)1(0)1(0

1''

yx

syssxs

yx

yfy

xfx

December 9, 2007 2D Transformations ١٦

2D Scaling (6)

• Scaling of a square with scale factors– relative to fixed point that is center of square

⎥⎦

⎤⎢⎣

⎡=

22

S

)20,20(

⎥⎦

⎤⎢⎣

⎡=⎥

⎤⎢⎣

⎡=

1515

f

f

yx

F

?)(?,

December 9, 2007 2D Transformations ١٧

2D Rotations (1)• To generate a rotation, we specify a rotation angle θ

and the center of the rotation (xr, yr) (pivot point) about which the object is to be rotated.

• If θ is positive then we have a counterclockwise rotation.if θ is negative then we have a clockwise rotation.

•x' = x cos(θ) – y sin(θ) y' = x sin(θ) + y cos(θ)

December 9, 2007 2D Transformations ١٨

2D Rotations (2)• when the rotation angle equal to -θ

x' = x cos(θ) + y sin(θ) y' = -x sin(θ) + y cos(θ)since cos(-θ) = cos(θ) andsin(-θ) = - sin(θ)

• Rotation of a point about a pivot point (xr, yr)x' = xr + (x- xr ) cos(θ) - (y-yr) sin(θ)y' = yr + (x- xr ) sin(θ) + (y-yr) cos(θ)

December 9, 2007 2D Transformations ١٩

2D Rotations (3)

– rotation to date has all been of the X-Y plane– i.e. we have been rotating around the Z-axis

Y

X

Z

December 9, 2007 2D Transformations ٢٠

2D Rotations (4)• Rotation can now be expressed as:

xy

xy

''

cos sinsin cos

1

00

0 0 1 1

⎢⎢⎢

⎥⎥⎥=

−⎡

⎢⎢⎢

⎥⎥⎥•

⎢⎢⎢

⎥⎥⎥

θ θθ θ

PRP ⋅= )(' θ

December 9, 2007 2D Transformations ٢١

Reflection (1)• A reflection is a transformation that produces a

mirror image of an object. The mirror image is generated relative to an axis or point of reflection.

• Reflection about the diagonal line y = −x:It uses the following transformation matrix

⎥⎥

⎢⎢

⎥⎥

⎢⎢

⎡−

−=

⎥⎥

⎢⎢

⎡′′

11000

01

010

1yx

yx

December 9, 2007 2D Transformations ٢٢

Reflection (2)• Reflection about the x-axis:

It uses the following transformation matrix

⎥⎥

⎢⎢

⎥⎥

⎢⎢

⎡−=

⎥⎥

⎢⎢

⎡′′

11001

00

001

1yx

yx

December 9, 2007 2D Transformations ٢٣

Reflection (3)• Reflection about the y-axis:

It uses the following transformation matrix

⎥⎥

⎢⎢

⎥⎥

⎢⎢

⎡ −=

⎥⎥

⎢⎢

⎡′′

11001

00

001

1yx

yx

December 9, 2007 2D Transformations ٢٤

Reflection (4)• Reflection about the Origin:

It uses the following transformation matrix

⎥⎥

⎢⎢

⎥⎥

⎢⎢

⎡−

−=

⎥⎥

⎢⎢

⎡′′

11001

00

001

1yx

yx

December 9, 2007 2D Transformations ٢٥

Reflection (5)• Reflection about the diagonal line y= x:

It uses the following transformation matrix

⎥⎥

⎢⎢

⎥⎥

⎢⎢

⎡=

⎥⎥

⎢⎢

⎡′′

11000

01

010

1yx

yx

December 9, 2007 2D Transformations ٢٦

Shear (1)

• These transformations produce shape distortions that represent a twisting, or shear effect, as if an object were composed of layers that are caused to slide over each other " in another word Italic representation of an object".

• A transformation that distorts the shape of an object such that the transformed shape appears as if the object were composed of internal layers that had been caused to slide over each other is called a shear. Two common shearing transformations are those that shift coordinate x values and those that shift y values.

December 9, 2007 2D Transformations ٢٧

Shear (2)

Original Data y-direction Shear x-direction Shear

1 0 0 1 shx 0shy 1 0 0 1 00 0 1 0 0 1

Sox’ = x x’ = x + shx • yy’= y + shy • x y’ = y

December 9, 2007 2D Transformations ٢٨

Shear (3)

Example: x-direction shearGiven shx =2, relative to the x-axis ( y = 0), and a square with coordinates (0,0), (0,1), (1,1), (1,0). Solution: Use the following formula, to compute the new vertices of the square:

⎥⎥

⎢⎢

⎥⎥

⎢⎢

⎡=

⎥⎥

⎢⎢

⎡′′

11001

00

01

1yxsh

yx x

December 9, 2007 2D Transformations ٢٩

Shear (4)

Example

December 9, 2007 2D Transformations ٣٠

Shear (5)Example: y-direction shearGiven shy =2, relative to the y-axis ( x = 0), and a square with coordinates (0,0), (0,1), (1,1), (1,0). Solution: Use the following formula, to compute the new vertices of the square: x’ = xy’= y + shy • x

December 9, 2007 2D Transformations ٣١

Shear (6)

December 9, 2007 2D Transformations ٣٢

Shear (7)

x-direction shearing relative to other reference line ( y ≠ 0)

x’ = x + shx ( y – yref )y’ = y

⎥⎥

⎢⎢

⎥⎥

⎢⎢

⎡ −=⎥⎥

⎢⎢

⎡′′

11001

001

1

.

yxyshsh

yx refxx

December 9, 2007 2D Transformations ٣٣

December 9, 2007 2D Transformations ٣٤

Shear (8)Example: x-direction shear relative to reference line ( y ≠ 0)Given shx = 0.5, yref = -1, and the square with coordinates (0,0), (0,1), (1,1), (1,0).

Solution:x’ = x + shx ( y – yref )y’ = y

So (0,0) will become (0.5,0); (0,1) will become (1,1); (1,1) will become (2,1); and (1,0) will become (1.5,0)

December 9, 2007 2D Transformations ٣٥

Shear (9)

y-direction shear relative to the line (x= xref)x’ = xy’ = y + shy ( x – xref )

⎥⎥

⎢⎢

⎥⎥

⎢⎢

⎡−=

⎥⎥

⎢⎢

⎡′′

1101

0

001

1. y

xxshshy

xrefyy

December 9, 2007 2D Transformations ٣٦

Shear (10)

December 9, 2007 2D Transformations ٣٧

Shear (11)Example: y-direction shear relative to the line (x=xref) Given shy = 3.5, xref = -12, and the square with coordinates (0,0), (0,1), (1,1), (1,0). Solution:

x’ = xy’ = y + shy ( x – xref )

So (0,0) will become (0,42); (0,1) will become (0,43); (1,1) will become (1,46.5); and (1,0) will become (1,45.5).

December 9, 2007 2D Transformations ٣٨

Composite Transformation

Forming products of transformation matrices is often referred to as a concatenation, or composition, of matrices.

1) TranslationIf two successive translations (tx1, ty1) and (tx2, ty2) are applied to a coordinate point P

then PttTttTPttTttTP yxyXyxyX ⋅⋅=⋅⋅= )},(),({}),({),( 11221122'

⎥⎥⎥

⎢⎢⎢

⎡++

=⎥⎥⎥

⎢⎢⎢

⎡⋅

⎥⎥⎥

⎢⎢⎢

1001001

1001001

1001001

21

21

1

1

2

2

yy

xx

y

x

y

x

tttt

tt

tt

)()()( 21211122 yyxxyxyx t,tttT,ttT,ttT ++=⋅

December 9, 2007 2D Transformations ٣٩

2) Rotation: Two successive rotations with θ1

and θ2 applied to point P produced P’ as:

– two successive rotations are also additive

PθRθRPθRθRP ⋅⋅=⋅⋅= )}()({})({)(' 1212

)()()( 2121 θθRθRθR +=⋅

PθθRP ⋅+= )(' 21

⎥⎥⎥

⎢⎢⎢

⎡+++−+

=+1000)cos()sin(0)sin()cos(

)21( 2121

2121

θθθθθθθθ

θθRwhere

December 9, 2007 2D Transformations ٤٠

• Rotation about arbitrary point p (xr, yr)(General pivot-point Rotation)

step1. translate P to originstep2. Rotation about originstep3. Retranslation to position P.

December 9, 2007 2D Transformations ٤١

⎥⎥⎥

⎢⎢⎢

⎡−−+−−

=

⎥⎥⎥

⎢⎢⎢

⎡−−

⎥⎥⎥

⎢⎢⎢

⎡ −

⎥⎥⎥

⎢⎢⎢

100sin)cos1(cossinsin)cos1(sincos

1001001

.1000cossin0sincos

.100

1001

θθθθθθθθ

θθθθ

rr

rr

r

r

r

r

xyyx

yx

yx

December 9, 2007 2D Transformations ٤٢

3) Scaling: Two successive scaling operations produces the following composite scaling matrix:

⎥⎥⎥

⎢⎢⎢

⎡=

⎥⎥⎥

⎢⎢⎢

⎥⎥⎥

⎢⎢⎢

1000000

1000000

1000000

2.1

2.1

1

1

2

2

yy

xx

y

x

y

x

ssss

ss

ss

)()()( 21211122 YYXXYXYX S,SSSS,SSS,SSS ⋅⋅=⋅

December 9, 2007 2D Transformations ٤٣

3D Transformations1. Translations:

⎥⎥⎥⎥

⎢⎢⎢⎢

=

1000100010001

z

y

x

ttt

T

⎥⎥⎥⎥

⎢⎢⎢⎢

+++

=•=′

1z

y

x

tztytx

TPP⎥⎥⎥⎥

⎢⎢⎢⎢

=

1zyx

P

December 9, 2007 2D Transformations ٤٤

2. Scaling (relative to the origin) -1

• x, y and z values multiplied by scaling factors sx, sy and sz

December 9, 2007 2D Transformations ٤٥

2. Scaling (relative to the origin) - 2

⎥⎥⎥⎥

⎢⎢⎢⎢

=

1000000000000

z

y

x

ss

s

S

⎥⎥⎥⎥

⎢⎢⎢⎢

=

1zyx

P

⎥⎥⎥⎥

⎢⎢⎢⎢

•••

=•=′

1z

y

x

szsysx

PSP

December 9, 2007 2D Transformations ٤٦

3. Scaling relative to a fixed point - 1

Fixed point

December 9, 2007 2D Transformations ٤٧

3. Scaling relative to a fixed point - 2

• for general scaling (relative to fixed point F)

• where

))(( FFgen TSTS −••=

⎥⎥⎥⎥

⎢⎢⎢⎢

−−−

=

1000)1(00)1(00)1(00

fzz

fyy

fxx

zssyssxss

S

PSP •=′

December 9, 2007 2D Transformations ٤٨

4. ROTATION – (z-axis)

• Rotating around the z axis:

⎥⎥⎥⎥⎥⎥⎥

⎢⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥⎥⎥

⎢⎢⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥⎥⎥

⎢⎢⎢⎢⎢⎢⎢⎢

=

11000010000cossin00sincos

1'''

zyx

zyx

θθθθ

December 9, 2007 2D Transformations ٤٩

4. ROTATION – (x-axis)

• Rotation around the X axis:

⎥⎥⎥⎥⎥⎥⎥⎥

⎢⎢⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥⎥⎥

⎢⎢⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥⎥⎥

⎢⎢⎢⎢⎢⎢⎢⎢

•−

=

110000cossin00sincos00001

1'''

zyx

zyx

θθθθ

December 9, 2007 2D Transformations ٥٠

4. ROTATION – (y-axis)

• Rotation around the Y axis:

⎥⎥⎥⎥⎥⎥⎥

⎢⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥⎥⎥

⎢⎢⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥⎥⎥

⎢⎢⎢⎢⎢⎢⎢⎢

•−

=

110000cos0sin00100sin0cos

1'''

zyx

zyx

θθ

θθ

Recommended