Geometric transformation 2d chapter 5

28
© 2005 Pearson Education Computer Graphics

Transcript of Geometric transformation 2d chapter 5

Page 1: Geometric transformation 2d   chapter 5

© 2005 Pearson Education

Computer Graphics

Page 2: Geometric transformation 2d   chapter 5

© 2005 Pearson Education

Chapter 5Geometric

Transformations

Page 3: Geometric transformation 2d   chapter 5

Computer Graphics with OpenGL, Third Edition, by Donald Hearn and M.Pauline Baler.IBSN 0-12-0-153-90-7 @ 2004 Pearson Education, Inc., Upper Saddle River, NJ. All right reserved © 2005 Pearson Education

Geometric Transformations• Basic transformations:

– Translation– Scaling– Rotation

• Purposes:– To move the position of objects– To alter the shape / size of objects– To change the orientation of objects

Page 4: Geometric transformation 2d   chapter 5

Computer Graphics with OpenGL, Third Edition, by Donald Hearn and M.Pauline Baler.IBSN 0-12-0-153-90-7 @ 2004 Pearson Education, Inc., Upper Saddle River, NJ. All right reserved © 2005 Pearson Education

Basic two-dimensional geometric transformations (1/1)

• Two-Dimensional translation– One of rigid-body transformation, which move objects without

deformation– Translate an object by Adding offsets to coordinates to generate

new coordinates positions– Set tx,ty be the translation distance, we have

– In matrix format, where T is the translation vector

xtx'x yty'y

yx

P

y

x

tt

T

'y'x

'P

TP'P

Page 5: Geometric transformation 2d   chapter 5

Computer Graphics with OpenGL, Third Edition, by Donald Hearn and M.Pauline Baler.IBSN 0-12-0-153-90-7 @ 2004 Pearson Education, Inc., Upper Saddle River, NJ. All right reserved © 2005 Pearson Education

– We could translate an object by applying the equation to every point of an object.• Because each line in an object is made up of an

infinite set of points, however, this process would take an infinitely long time.

• Fortunately we can translate all the points on a line by translating only the line’s endpoints and drawing a new line between the endpoints.

• This figure translates the “house” by (3, -4)

Basic two-dimensional geometric transformations (1/2)

Page 6: Geometric transformation 2d   chapter 5

Computer Graphics with OpenGL, Third Edition, by Donald Hearn and M.Pauline Baler.IBSN 0-12-0-153-90-7 @ 2004 Pearson Education, Inc., Upper Saddle River, NJ. All right reserved © 2005 Pearson Education

Translation Example

y

x0 1

1

2

2

3 4 5 6 7 8 9 10

3

4

5

6

(1, 1) (3, 1)

(2, 3)

Page 7: Geometric transformation 2d   chapter 5

Computer Graphics with OpenGL, Third Edition, by Donald Hearn and M.Pauline Baler.IBSN 0-12-0-153-90-7 @ 2004 Pearson Education, Inc., Upper Saddle River, NJ. All right reserved © 2005 Pearson Education

Basic two-dimensional geometric transformations (2/1)

• Two-Dimensional rotation– Rotation axis and angle are specified for rotation– Convert coordinates into polar form for calculation

– Example, to rotation an object with angle a• The new position coordinates

• In matrix format

• Rotation about a point (xr, yr)

cosrx sinry

cossinsincos

R PR'P

cossincossinsincos)sin('sincossinsincoscos)cos('

yxrrryyxrrrx

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

rrr

rrr

yyxxyyyyxxxx

rr

Page 8: Geometric transformation 2d   chapter 5

Computer Graphics with OpenGL, Third Edition, by Donald Hearn and M.Pauline Baler.IBSN 0-12-0-153-90-7 @ 2004 Pearson Education, Inc., Upper Saddle River, NJ. All right reserved © 2005 Pearson Education

– This figure shows the rotation of the house by 45 degrees.

• Positive angles are measured counterclockwise (from x towards y)

• For negative angles, you can use the identities:– cos(-) = cos() and sin(-)=-sin()

Basic two-dimensional geometric transformations (2/2)

6

y

x 0 1

1

2

2

3 4 5 6 7 8 9 10

3

4

5

6

Page 9: Geometric transformation 2d   chapter 5

Computer Graphics with OpenGL, Third Edition, by Donald Hearn and M.Pauline Baler.IBSN 0-12-0-153-90-7 @ 2004 Pearson Education, Inc., Upper Saddle River, NJ. All right reserved © 2005 Pearson Education

Rotation Example

y

0 1

1

2

2

3 4 5 6 7 8 9 10

3

4

5

6

(3, 1) (5, 1)

(4, 3)

Page 10: Geometric transformation 2d   chapter 5

Computer Graphics with OpenGL, Third Edition, by Donald Hearn and M.Pauline Baler.IBSN 0-12-0-153-90-7 @ 2004 Pearson Education, Inc., Upper Saddle River, NJ. All right reserved © 2005 Pearson Education

Basic two-dimensional geometric transformations (3/1)

• Two-Dimensional scaling– To alter the size of an object by multiplying the coordinates

with scaling factor sx and sy

– In matrix format, where S is a 2by2 scaling matrix

– Choosing a fix point (xf, yf) as its centroid to perform scaling

xsx'x ysyy

yx

s00s

'y'x

y

x PS'P

)s1(ysy'y)s1(xsx'x

yfy

xfx

Page 11: Geometric transformation 2d   chapter 5

Computer Graphics with OpenGL, Third Edition, by Donald Hearn and M.Pauline Baler.IBSN 0-12-0-153-90-7 @ 2004 Pearson Education, Inc., Upper Saddle River, NJ. All right reserved © 2005 Pearson Education

– In this figure, the house is scaled by 1/2 in x and 1/4 in y• Notice that the scaling is about the origin:

– The house is smaller and closer to the origin

Basic two-dimensional geometric transformations (3/2)

Page 12: Geometric transformation 2d   chapter 5

Computer Graphics with OpenGL, Third Edition, by Donald Hearn and M.Pauline Baler.IBSN 0-12-0-153-90-7 @ 2004 Pearson Education, Inc., Upper Saddle River, NJ. All right reserved © 2005 Pearson Education

Scaling

Note: House shifts position relative to origin

y

x 0

1

1

2

2

3 4 5 6 7 8 9 10

3

4

5

6

12

13

36

39

– If the scale factor had been greater than 1, it would be larger and farther away.

WATCH OUT: Objects grow and move!

Page 13: Geometric transformation 2d   chapter 5

Computer Graphics with OpenGL, Third Edition, by Donald Hearn and M.Pauline Baler.IBSN 0-12-0-153-90-7 @ 2004 Pearson Education, Inc., Upper Saddle River, NJ. All right reserved © 2005 Pearson Education

Scaling Example

y

0 1

1

2

2

3 4 5 6 7 8 9 10

3

4

5

6

(1, 1) (3, 1)

(2, 3)

Page 14: Geometric transformation 2d   chapter 5

Computer Graphics with OpenGL, Third Edition, by Donald Hearn and M.Pauline Baler.IBSN 0-12-0-153-90-7 @ 2004 Pearson Education, Inc., Upper Saddle River, NJ. All right reserved © 2005 Pearson Education

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:

• We can then write any point (x, y) as (hx, hy, h)• We can conveniently choose h = 1 so that (x, y) becomes (x, y, 1)

hxx h h

yy h

Page 15: Geometric transformation 2d   chapter 5

Computer Graphics with OpenGL, Third Edition, by Donald Hearn and M.Pauline Baler.IBSN 0-12-0-153-90-7 @ 2004 Pearson Education, Inc., Upper Saddle River, NJ. All right reserved © 2005 Pearson Education

Why Homogeneous Coordinates?

• Mathematicians commonly use homogeneous coordinates as they allow scaling factors to be removed from equations • We will see in a moment that all of the transformations we discussed previously can be represented as 3*3 matrices• Using homogeneous coordinates allows us use matrix multiplication to calculate transformations – extremely efficient!

Page 16: Geometric transformation 2d   chapter 5

Computer Graphics with OpenGL, Third Edition, by Donald Hearn and M.Pauline Baler.IBSN 0-12-0-153-90-7 @ 2004 Pearson Education, Inc., Upper Saddle River, NJ. All right reserved © 2005 Pearson Education

Homogenous Coordinates

• Combine the geometric transformation into a single matrix with 3by3 matrices

• Expand each 2D coordinate to 3D coordinate with homogenous parameter

• Two-Dimensional translation matrix

• Two-Dimensional rotation matrix

• Two-Dimensional scaling matrix

1yx

100t10t01

1'y'x

y

x

1yx

1000cossin0coscos

1'y'x

1yx

1000s000s

1'y'x

y

x

Page 17: Geometric transformation 2d   chapter 5

Computer Graphics with OpenGL, Third Edition, by Donald Hearn and M.Pauline Baler.IBSN 0-12-0-153-90-7 @ 2004 Pearson Education, Inc., Upper Saddle River, NJ. All right reserved © 2005 Pearson Education

Inverse transformations

• Inverse translation matrix

• Two-Dimensional translation matrix

• Two-Dimensional translation matrix

100t10t01

T y

x1

1000cossin0sincos

R 1

100

0s10

00s1

Sx

x

1

Page 18: Geometric transformation 2d   chapter 5

Computer Graphics with OpenGL, Third Edition, by Donald Hearn and M.Pauline Baler.IBSN 0-12-0-153-90-7 @ 2004 Pearson Education, Inc., Upper Saddle River, NJ. All right reserved © 2005 Pearson Education

Two-dimensional composite transformation (1)

• Composite transformation– A sequence of transformations– Calculate composite transformation matrix rather than

applying individual transformations

• Composite two-dimensional translations– Apply two successive translations, T1 and T2

– Composite transformation matrix in coordinate form

PM'PPMM'P 12

PTTPTTP

ttTT

ttTT

yx

yx

)()('

),(

),(

1212

222

111

),(),(),( 21211122 yyxxyxyx ttttTttTttT

Page 19: Geometric transformation 2d   chapter 5

Computer Graphics with OpenGL, Third Edition, by Donald Hearn and M.Pauline Baler.IBSN 0-12-0-153-90-7 @ 2004 Pearson Education, Inc., Upper Saddle River, NJ. All right reserved © 2005 Pearson Education

Two-dimensional composite transformation (2)

• Composite two-dimensional rotations– Two successive rotations, R1 and R2 into a point P

– Multiply two rotation matrices to get composite transformation matrix

• Composite two-dimensional scaling

PRRPPRRP

)}()({'

})({)('

12

12

PssssSP

ssssSssSssS

yyxx

yyxxyxyx

),('

),(),(),(

2121

21212211

PRPRRR

)('

)()()(

21

2112

Page 20: Geometric transformation 2d   chapter 5

Computer Graphics with OpenGL, Third Edition, by Donald Hearn and M.Pauline Baler.IBSN 0-12-0-153-90-7 @ 2004 Pearson Education, Inc., Upper Saddle River, NJ. All right reserved © 2005 Pearson Education

Two-dimensional composite transformation (3)

• General two-dimensional Pivot-point rotation– Graphics package provide only origin rotation– Perform a translate-rotate-translate sequence

• Translate the object to move pivot-point position to origin

• Rotate the object• Translate the object back to the original position

– Composite matrix in coordinates form),y,x(R)y,x(T)(R)y,x(T rrrrrr

Page 21: Geometric transformation 2d   chapter 5

Computer Graphics with OpenGL, Third Edition, by Donald Hearn and M.Pauline Baler.IBSN 0-12-0-153-90-7 @ 2004 Pearson Education, Inc., Upper Saddle River, NJ. All right reserved © 2005 Pearson Education

Two-dimensional composite transformation (4)

• Example of pivot-point rotation

Page 22: Geometric transformation 2d   chapter 5

Computer Graphics with OpenGL, Third Edition, by Donald Hearn and M.Pauline Baler.IBSN 0-12-0-153-90-7 @ 2004 Pearson Education, Inc., Upper Saddle River, NJ. All right reserved © 2005 Pearson Education

Pivot-Point Rotation

100sin)cos1(cossinsin)cos1(sincos

1001001

1000cossin0sincos

1001001

rr

rr

r

r

r

r

xyyx

yx

yx

,,,, rrrrrr yxRyxTRyxT

Translate Rotate Translate

(xr,yr)

(xr,yr)

(xr,yr)

(xr,yr)

Page 23: Geometric transformation 2d   chapter 5

Computer Graphics with OpenGL, Third Edition, by Donald Hearn and M.Pauline Baler.IBSN 0-12-0-153-90-7 @ 2004 Pearson Education, Inc., Upper Saddle River, NJ. All right reserved © 2005 Pearson Education

Two-dimensional composite transformation (5)

• General two-dimensional Fixed-point scaling– Perform a translate-scaling-translate sequence

• Translate the object to move fixed-point position to origin

• Scale the object wrt. the coordinate origin• Use inverse of translation in step 1 to return the object

back to the original position– Composite matrix in coordinates form

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

Page 24: Geometric transformation 2d   chapter 5

Computer Graphics with OpenGL, Third Edition, by Donald Hearn and M.Pauline Baler.IBSN 0-12-0-153-90-7 @ 2004 Pearson Education, Inc., Upper Saddle River, NJ. All right reserved © 2005 Pearson Education

Two-dimensional composite transformation (6)

• Example of fixed-point scaling

Page 25: Geometric transformation 2d   chapter 5

Computer Graphics with OpenGL, Third Edition, by Donald Hearn and M.Pauline Baler.IBSN 0-12-0-153-90-7 @ 2004 Pearson Education, Inc., Upper Saddle River, NJ. All right reserved © 2005 Pearson Education

General Fixed-Point Scaling

100)1(0)1(0

1001001

1000000

1001001

yfy

xfx

f

fx

f

f

syssxs

yx

ss

yx

y

Translate Scale Translate

(xr,yr)

(xr,yr)

(xr,yr)

(xr,yr)

yxffffyxff ssyxSyxTssSyxT ,,,, ,,

Page 26: Geometric transformation 2d   chapter 5

Computer Graphics with OpenGL, Third Edition, by Donald Hearn and M.Pauline Baler.IBSN 0-12-0-153-90-7 @ 2004 Pearson Education, Inc., Upper Saddle River, NJ. All right reserved © 2005 Pearson Education

Two-dimensional composite transformation (7)

• General two-dimensional scaling directions– Perform a rotate-scaling-rotate sequence– Composite matrix in coordinates form

1000cosssinssincos)ss(0sincos)ss(sinscoss

)(R)s,s(S)(R

22

2112

122

22

1

211

s1

s2

Page 27: Geometric transformation 2d   chapter 5

Computer Graphics with OpenGL, Third Edition, by Donald Hearn and M.Pauline Baler.IBSN 0-12-0-153-90-7 @ 2004 Pearson Education, Inc., Upper Saddle River, NJ. All right reserved © 2005 Pearson Education

General Scaling Directions

• Converted to a parallelogram

1000cossinsincos)(0sincos)(sincos

)(),()( 22

2112

122

22

1

211

ssss

ssssRssSR

x

y

(0,0)

(3/2,1/2)

(1/2,3/2)

(2,2)

x

y

(0,0)

(1,0)

(0,1)

(1,1)

x

y s2

s1

Scale

Page 28: Geometric transformation 2d   chapter 5

Computer Graphics with OpenGL, Third Edition, by Donald Hearn and M.Pauline Baler.IBSN 0-12-0-153-90-7 @ 2004 Pearson Education, Inc., Upper Saddle River, NJ. All right reserved © 2005 Pearson Education

Basic transformations such as translation, rotation and scaling are include in most graphics package. Some package provide additional transformations that are useful in certain applications. Two such transformations are reflection and shear.

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

The mirror image generated by rotating an object 180 deg about reflection axis

We can choose axis of reflection in the xy plane or perpendicular to xy plane.