cs 4204 computer graphics
play

CS 4204 Computer Graphics 2D Transformations 2D Transformations - PowerPoint PPT Presentation

CS 4204 Computer Graphics 2D Transformations 2D Transformations Yong Cao Yong Cao Virginia Tech Virginia Tech References: References: Introduction to Computer Graphics Introduction to Computer Graphics course notes by Doug


  1. CS 4204 Computer Graphics 2D Transformations 2D Transformations Yong Cao Yong Cao Virginia Tech Virginia Tech References: References: “Introduction to Computer Graphics Introduction to Computer Graphics” ” course notes by Doug Bowman course notes by Doug Bowman “ Interactive Computer Graphics, Fourth Edition, Ed Angle Interactive Computer Graphics, Fourth Edition, Ed Angle

  2. Transformations What are they? What are they? • changing something to something else via rules changing something to something else via rules • • mathematics: mapping between values in a range set • mathematics: mapping between values in a range set and domain set (function/relation) and domain set (function/relation) • geometric: translate, rotate, scale, shear, geometric: translate, rotate, scale, shear,… … • Why are they important to graphics? Why are they important to graphics? • moving objects on screen / in space moving objects on screen / in space • • mapping from model space to world space to camera mapping from model space to world space to camera • space to screen space space to screen space • specifying parent/child relationships specifying parent/child relationships • • … … •

  3. Translation Moving an object is called a translation. We translate a point by by Moving an object is called a translation. We translate a point adding to the x and y coordinates, respectively, the amount the adding to the x and y coordinates, respectively, the amount the point should be shifted in the x and y directions. We translate We translate point should be shifted in the x and y directions. an object by translating each vertex in the object. an object by translating each vertex in the object. t y t x x new = x old + t x ; y new = y old + t y

  4. Scaling Changing the size of an object is called a scale. We scale an object by bject by Changing the size of an object is called a scale. We scale an o scaling the x and y coordinates of each vertex in the object. scaling the x and y coordinates of each vertex in the object. h new h old w old w new s x =w new /w old s y =h new /h old x new = s x x old y new = s y y old

  5. Rotation about the origin Consider rotation about the origin by q Consider rotation about the origin by q degrees degrees • radius stays the same, angle increases by radius stays the same, angle increases by q q • x = r cos ( φ + θ) y = r sin ( φ + θ) x’=x cos θ –y sin θ y’ = x sin θ + y cos θ x = r cos φ y = r sin φ

  6. Transformations as matrices Scale: Scale: ⋅ ⎡ ⎤ ⎡ ⎤ ⎡ ⎤ s 0 s x x x new = s s x x old x new = x x = x x ⎢ ⎥ ⎢ ⎥ old ⎢ ⎥ ⋅ 0 s ⎣ ⎦ s y ⎣ ⎦ ⎣ ⎦ y y new = s s y y old y new = y y y y old Rotation: Rotation: θ − θ θ − θ ⎡ ⎤ ⎡ ⎤ ⎡ ⎤ cos sin x x cos y sin cos θ θ - sin θ θ x 2 = x 1 - y y 1 = x 2 = x 1 cos 1 sin ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ θ θ θ + θ ⎣ ⎦ ⎣ ⎦ ⎣ ⎦ sin cos y x sin y cos θ + y θ sin θ cos θ y 2 = x 1 + y 1 y 2 = x 1 sin 1 cos Translation: Translation: + ⎡ ⎤ ⎡ ⎤ ⎡ ⎤ t x t x + = x x ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ x new x new = = x x old old + + t t x + x ⎣ ⎦ t y t ⎣ ⎦ ⎣ ⎦ y y y y new = y y old + t t y y new = old + y

  7. Homogeneous Coordinates In order to represent a translation as a matrix multiplication In order to represent a translation as a matrix multiplication operation we use 3 x 3 matrices and pad our points to become 3 operation we use 3 x 3 matrices and pad our points to become 3 x 1 matrices. This coordinate system (using three values to x 1 matrices. This coordinate system (using three values to represent a 2D point) is called homogeneous coordinates. represent a 2D point) is called homogeneous coordinates. θ − θ ⎡ ⎤ cos sin 0 ⎡ ⎤ x ⎢ ⎥ ⎢ ⎥ = θ θ = R sin cos 0 ⎢ ⎥ P y θ ⎢ ⎥ ( x , y ) ⎢ ⎥ ⎢ ⎥ ⎣ ⎦ 0 0 1 ⎣ ⎦ 1 ⎡ ⎤ 1 0 t ⎡ ⎤ s 0 0 x ⎢ ⎥ x ⎢ ⎥ = = T 0 1 t ⎢ ⎥ S 0 s 0 ⎢ ⎥ x , y y x , y y ⎢ ⎥ ⎢ ⎥ ⎣ ⎦ 0 0 1 ⎣ ⎦ 0 0 1

  8. Composite Transformations Suppose we wished to perform multiple transformations on a point Suppose we wished to perform multiple transformations on a point: : 2 = T 3,1 P P 1 3 = S 2, 2 P P 2 4 = R 30 P P 3 M = R 30 S 2,2 T 3,1 4 = MP P 1 Remember: • Matrix multiplication is associative, not commutative! • Transform matrices must be pre-multiplied • The first transformation you want to perform will be at the far right, just before the point

  9. Composite Transformations - Scaling Given our three basic transformations we can create other Given our three basic transformations we can create other transformations. transformations. Scaling with a fixed point Scaling with a fixed point A problem with the scale transformation is that it also moves the e A problem with the scale transformation is that it also moves th object being scaled. object being scaled. Scale a line between (2, 1) (4,1) to twice its length. Scale a line between (2, 1) (4,1) to twice its length. After Before 0 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9 10

  10. Composite Transforms - Scaling (cont.) If we scale a line between (0,0) & (2,0) to twice its length, the he If we scale a line between (0,0) & (2,0) to twice its length, t left left- -hand endpoint does not move. hand endpoint does not move. After Before 0 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9 10 (0,0) is known as a fixed point for the basic scaling transformation. We can use composite transformations to create a scale transformation with different fixed points.

  11. Fixed Point Scaling Scale by 2 with fixed point = (2,1) Scale by 2 with fixed point = (2,1) Translate the point (2,1) to the origin Translate the point (2,1) to the origin Scale by 2 Scale by 2 Translate origin to point (2,1) Translate origin to point (2,1) Before − − ⎡ ⎤ ⎡ ⎤ ⎡ ⎤ ⎡ ⎤ 1 0 2 2 0 0 1 0 2 2 0 2 0 1 2 3 4 5 6 7 8 9 10 ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ − = 0 1 1 0 1 0 0 1 1 0 1 0 ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎣ 0 0 1 ⎦ ⎣ 0 0 1 ⎦ ⎣ 0 0 1 ⎦ ⎣ 0 0 1 ⎦ T S T C − − 2 , 1 2 , 1 2 , 1 − − ⎡ ⎤ ⎡ ⎤ ⎡ ⎤ ⎡ ⎤ ⎡ ⎤ ⎡ ⎤ 2 0 2 4 6 2 0 2 2 2 ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ After = = 0 1 0 1 1 0 1 0 1 1 ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎣ ⎦ ⎣ ⎦ ⎣ ⎦ ⎣ ⎦ ⎣ ⎦ ⎣ ⎦ 0 0 1 1 1 0 0 1 1 1 0 1 2 3 4 5 6 7 8 9 10 C C

  12. Example of 2D transformation Rotate around an arbitraty arbitraty point O: point O: Rotate around an Rotate around an arbitraty point O: P y O x

  13. Rotate around an arbitraty point x P P’ 90 O y

  14. Rotate around an arbitraty point We know how to rotate around the origin We know how to rotate around the origin We know how to rotate around the origin P y O x

  15. Rotate around an arbitraty point …but that is not what we want to do! but that is not what we want to do! … …but that is not what we want to do! P y O P’ 90 x P’

  16. So what do we do? x P A y

  17. Transform it to a known case Translate(- -Ox, Ox,- -Oy Oy) ) Translate( Translate(-Ox,-Oy) P y O P’ O’ x

  18. Second step: Rotation Translate(- -Ox, Ox,- -Oy Oy) ) Translate( Translate(-Ox,-Oy) Rotate(- -90) 90) Rotate( Rotate(-90) P y O P’ O’ x P’’

  19. Final: Put everything back Translate(- -Ox, Ox,- -Oy Oy) ) Translate( Translate(-Ox,-Oy) Rotate(90) Rotate(90) Rotate(90) P y Translate(Ox,Oy) ) Translate(Ox,Oy Translate(Ox,Oy) O P’’’ P’ O’ x P’’

  20. Rotation about arbitrary point IMPORTANT!: Order IMPORTANT!: Order IMPORTANT!: Order M = T(Ox,Oy)R(- -90)T( 90)T(- -Ox, Ox,- -Oy) Oy) M = T(Ox,Oy)R( M = T(Ox,Oy)R(-90)T(-Ox,-Oy) P y O P’’’ P’ O’ x P’’

  21. Rotation about arbitrary point θ Degrees About Point ( Rotation Of θ Degrees About Point (x,y x,y) ) Rotation Of Translate ( Translate (x,y x,y) to origin ) to origin (x,y) (x,y) Rotate Rotate Translate origin to (x,y x,y) ) Translate origin to ( θ θ − − ⎡ ⎤ ⎡ ⎤ ⎡ ⎤ 1 0 x cos sin 0 1 0 x ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ θ θ − = C 0 1 y sin cos 0 0 1 y ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎣ ⎦ ⎣ ⎦ ⎣ ⎦ 0 0 1 0 0 1 0 0 1 T R T θ − − x , y x , y

  22. Shears Original Data Original Data y Shear y Shear x Shear x Shear ⎡ ⎤ ⎡ ⎤ 1 0 0 1 b 0 ⎢ ⎥ ⎢ ⎥ a 1 0 0 1 0 ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎣ ⎦ ⎣ ⎦ 0 0 1 0 0 1

  23. Reflections Reflection about the y- -axis axis Reflection about the x- -axis axis Reflection about the y Reflection about the x ⎡− ⎤ ⎡ ⎤ 1 0 0 1 0 0 ⎢ ⎥ ⎢ ⎥ − 0 1 0 0 1 0 ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎣ ⎦ 0 0 1 ⎣ ⎦ 0 0 1

Recommend


More recommend