50
December 9, 2007 2D Transformations ١ CS 380 “Computer Graphics” 2D Transformations

2 dimensional transformations

Embed Size (px)

DESCRIPTION

Document based on computer graphics.

Citation preview

Page 1: 2 dimensional transformations

December 9, 2007 2D Transformations ١

CS 380 “Computer Graphics”

2D Transformations

Page 2: 2 dimensional 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.

Page 3: 2 dimensional transformations

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

Page 4: 2 dimensional transformations

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

Page 5: 2 dimensional transformations

December 9, 2007 2D Transformations ٥

Homogenous coordinates

• 2D, 3 × 1 row matrices

• 3D, 4 × 1 row matrices

⎥⎥⎥

⎢⎢⎢

⎡=

1yx

P

⎥⎥⎥⎥

⎢⎢⎢⎢

=

1zyx

P

Page 6: 2 dimensional transformations

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

Page 7: 2 dimensional transformations

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.

Page 8: 2 dimensional transformations

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

Page 9: 2 dimensional transformations

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

Page 10: 2 dimensional transformations

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

Page 11: 2 dimensional transformations

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.

Page 12: 2 dimensional transformations

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(

Page 13: 2 dimensional transformations

December 9, 2007 2D Transformations ١٣

2D Scaling (3)

3 × 3 matrix

⎥⎥⎥

⎢⎢⎢

⎡=

1000000

y

x

ss

S

⎥⎥⎥

⎢⎢⎢

⎡=

1yx

P

⎥⎥⎥

⎢⎢⎢

⎡••

=•=′

1y

x

sysx

PSP

Page 14: 2 dimensional transformations

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.

Page 15: 2 dimensional transformations

December 9, 2007 2D Transformations ١٥

2D Scaling (5)

⎥⎥⎥

⎢⎢⎢

⎥⎥⎥

⎢⎢⎢

⎡−−

=⎥⎥⎥

⎢⎢⎢

1.

100)1(0)1(0

1''

yx

syssxs

yx

yfy

xfx

Page 16: 2 dimensional transformations

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

?)(?,

Page 17: 2 dimensional transformations

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(θ)

Page 18: 2 dimensional transformations

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(θ)

Page 19: 2 dimensional transformations

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

Page 20: 2 dimensional transformations

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 ⋅= )(' θ

Page 21: 2 dimensional transformations

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

Page 22: 2 dimensional transformations

December 9, 2007 2D Transformations ٢٢

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

It uses the following transformation matrix

⎥⎥

⎢⎢

⎥⎥

⎢⎢

⎡−=

⎥⎥

⎢⎢

⎡′′

11001

00

001

1yx

yx

Page 23: 2 dimensional transformations

December 9, 2007 2D Transformations ٢٣

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

It uses the following transformation matrix

⎥⎥

⎢⎢

⎥⎥

⎢⎢

⎡ −=

⎥⎥

⎢⎢

⎡′′

11001

00

001

1yx

yx

Page 24: 2 dimensional transformations

December 9, 2007 2D Transformations ٢٤

Reflection (4)• Reflection about the Origin:

It uses the following transformation matrix

⎥⎥

⎢⎢

⎥⎥

⎢⎢

⎡−

−=

⎥⎥

⎢⎢

⎡′′

11001

00

001

1yx

yx

Page 25: 2 dimensional transformations

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

Page 26: 2 dimensional transformations

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.

Page 27: 2 dimensional transformations

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

Page 28: 2 dimensional transformations

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

Page 29: 2 dimensional transformations

December 9, 2007 2D Transformations ٢٩

Shear (4)

Example

Page 30: 2 dimensional transformations

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

Page 31: 2 dimensional transformations

December 9, 2007 2D Transformations ٣١

Shear (6)

Page 32: 2 dimensional transformations

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

Page 33: 2 dimensional transformations

December 9, 2007 2D Transformations ٣٣

Page 34: 2 dimensional 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)

Page 35: 2 dimensional transformations

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

Page 36: 2 dimensional transformations

December 9, 2007 2D Transformations ٣٦

Shear (10)

Page 37: 2 dimensional transformations

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).

Page 38: 2 dimensional transformations

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 ++=⋅

Page 39: 2 dimensional transformations

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

Page 40: 2 dimensional transformations

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.

Page 41: 2 dimensional transformations

December 9, 2007 2D Transformations ٤١

⎥⎥⎥

⎢⎢⎢

⎡−−+−−

=

⎥⎥⎥

⎢⎢⎢

⎡−−

⎥⎥⎥

⎢⎢⎢

⎡ −

⎥⎥⎥

⎢⎢⎢

100sin)cos1(cossinsin)cos1(sincos

1001001

.1000cossin0sincos

.100

1001

θθθθθθθθ

θθθθ

rr

rr

r

r

r

r

xyyx

yx

yx

Page 42: 2 dimensional transformations

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 ⋅⋅=⋅

Page 43: 2 dimensional transformations

December 9, 2007 2D Transformations ٤٣

3D Transformations1. Translations:

⎥⎥⎥⎥

⎢⎢⎢⎢

=

1000100010001

z

y

x

ttt

T

⎥⎥⎥⎥

⎢⎢⎢⎢

+++

=•=′

1z

y

x

tztytx

TPP⎥⎥⎥⎥

⎢⎢⎢⎢

=

1zyx

P

Page 44: 2 dimensional transformations

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

Page 45: 2 dimensional transformations

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

Page 46: 2 dimensional transformations

December 9, 2007 2D Transformations ٤٦

3. Scaling relative to a fixed point - 1

Fixed point

Page 47: 2 dimensional transformations

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 •=′

Page 48: 2 dimensional transformations

December 9, 2007 2D Transformations ٤٨

4. ROTATION – (z-axis)

• Rotating around the z axis:

⎥⎥⎥⎥⎥⎥⎥

⎢⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥⎥⎥

⎢⎢⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥⎥⎥

⎢⎢⎢⎢⎢⎢⎢⎢

=

11000010000cossin00sincos

1'''

zyx

zyx

θθθθ

Page 49: 2 dimensional transformations

December 9, 2007 2D Transformations ٤٩

4. ROTATION – (x-axis)

• Rotation around the X axis:

⎥⎥⎥⎥⎥⎥⎥⎥

⎢⎢⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥⎥⎥

⎢⎢⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥⎥⎥

⎢⎢⎢⎢⎢⎢⎢⎢

•−

=

110000cossin00sincos00001

1'''

zyx

zyx

θθθθ

Page 50: 2 dimensional transformations

December 9, 2007 2D Transformations ٥٠

4. ROTATION – (y-axis)

• Rotation around the Y axis:

⎥⎥⎥⎥⎥⎥⎥

⎢⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥⎥⎥

⎢⎢⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥⎥⎥

⎢⎢⎢⎢⎢⎢⎢⎢

•−

=

110000cos0sin00100sin0cos

1'''

zyx

zyx

θθ

θθ