Understanding 2D Transformations in Computer Graphics
Geometrical changes of objects through translations, rotations, and scaling are essential in computer graphics. Matrix math plays a crucial role in efficiently processing and combining transformations to alter object coordinates effectively.
Download Presentation
Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
E N D
Presentation Transcript
2D TRANSFORMATIONS Copied and modified from: http://ebooks.bharathuniv.ac.in/gdlc1/gdlc1/Libraries/Computer%20Graphics%20Library/Gdlc/2D%20Combining%20Transformations%20(175)/2D%20Combining%20Transformations%20-%20Gdlc.ppt
2D Transformations What are transformations? Geometrical changes of an object from a current state to modified state. Why are transformations is needed? To manipulate the initially created object and to display the modified object without having to redraw it.
2D Transformations 2 ways Object Transformation Alter the coordinates descriptions an object Translation, rotation, scaling etc. Coordinate system unchanged Coordinate transformation Produce a different coordinate system
Matrix Math Why do we use matrix? More convenient organization of data. More efficient processing Enable the combination of various concatenations Matrix addition and subtraction a c b d c d a b =
Matrix Math How about it? a b c d e f Matrix Multiplication Element-wise product e g f h a c b d a.e + b.g a.f + b.h c.e + d.g c.f + d.h . =
Matrix Math What about this? 1 2 . 1 2 1 = 6 6 3 1 3 2 1 = ? 2 3. Type of matrix a a b b Row-vector Column-vector
Matrix Math Is there a difference between possible representations? + ae a b e bf = ce + c d f df a b e ae = + + f cf be df c d a c e ae = + + f bf ce df b d
Matrix Math We ll use the column-vector representation for a point. Which implies that we use pre-multiplication of the transformation it appears before the point to be transformed in the equation. Ax + By Cx + Dy ? ?= ? ? ? ? What if we needed to switch to the other convention?
Translation A translation moves all points in an object along the same straight-line path to new positions. The path is represented by a vector, called the translation or shift vector. We can write the components: p'x = px + tx p'y = py + ty or in matrix form: P' = P + T x y y = + ? =4 ty = 6 tx (2, 2) x tx ty
Rotation A rotation repositions all points in an object along a circular path in the plane centered at the pivot point. P P First, we ll assume the pivot is at the origin.
Rotation Review Trigonometry => cos = x/r , sin = y/r x = r. cos , y = r.sin P (x , y ) => cos ( + ) = x /r x = r. cos ( + ) x = r.cos cos -r.sin sin x = x.cos y.sin r y P(x,y) r y =>sin ( + ) = y /r y = r. sin ( + ) y = r.cos sin + r.sin cos y = x.sin + y.cos x x Identity of Trigonometry
Rotation We can write the components: p'x = px cos py sin p'y = px sin + py cos P (x , y ) or in matrix form: P' = R P can be clockwise (-ve) or counterclockwise (+ve as our example). Rotation matrix y P(x,y) r y ? =cos? sin? cos? sin? x x
Rotation Example Find the transformed point, P , caused by rotating P= (5, 1) about the origin through an angle of 90 . ? ? cos? sin? ? cos? ? sin? ? sin? + ? cos? sin? cos? = =5 cos90 1 sin90 5 sin90 + 1 cos90 =5 0 1 1 5 1 + 1 0 = 1 5
Scaling Scaling changes the size of an object and involves two scale factors, Sx and Sy for the x- and y- coordinates respectively. Scales are about the origin. We can write the components: p'x = sx px p'y = sy py or in matrix form: P' = S P Scale matrix as: ? ?? 0 P P 0 ?? =
Scaling If the scale factors are in between 0 and 1 the points will be moved closer to the origin will be smaller. Example : P(2, 5), Sx = 0.5, Sy = 0.5 Find P ? the object P(2, 5) P
Scaling If the scale factors are in between 0 and 1 the points will be moved closer to the origin will be smaller. Example : P(2, 5), Sx = 0.5, Sy = 0.5 Find P ? If the scale factors are larger than 1 the points will be moved away from the origin the object will be larger. P the object P(2, 5) P Example : P(2, 5), Sx = 2, Sy = 2 Find P ?
Scaling If the scale factors are the same, Sx = Sy uniform scaling Only change in size (as previous example) If Sx Sy differential scaling. Change in size and shape Example : square P(1, 3), Sx = 2, Sy = 5 , P ? P rectangle P(1, 2) What does scaling by 1 do? What is that matrix called? What does scaling by a negative value do?
Homogenous Coordinates y y w x x Let s move our problem into 3D. Let point (x, y) in 2D be represented by point (x, y, 1) in the new space. Scaling our new point by any value a puts us somewhere along a particular line: (ax, ay, a). A point in 2D can be represented in many ways in the new space. (2, 4) ---------- (8, 16, 4) or (6, 12, 3) or (2, 4, 1) or etc.
Homogenous Coordinates We can always map back to the original 2D point by dividing by the last coordinate (15, 6, 3) --- (5, 2). (60, 40, 10) - ?. Why do we use 1 for the last coordinate? The fact that all the points along each line can be mapped back to the same point in 2D gives this coordinate system its name homogeneous coordinates.
Matrix Representation Point in column-vector: x y 1 Our point now has three coordinates. So our matrix is needs to be 3x3. Translation ? ? 1 0 ? ? 1 1 0 0 1 0 ?? ?? 1 =
Matrix Representation Rotation ? ? 1 ? ? 1 cos? sin? 0 sin? cos? 0 0 0 1 = Scaling ? ? 1 ? ? 1 ?? 0 0 0 ?? 0 0 0 1 =
Composite Transformation We can represent any sequence of transformations as a single matrix. No special cases when transforming a point matrix vector. Composite transformations matrix matrix. Composite transformations: Rotate about an arbitrary point translate, rotate, translate Scale about an arbitrary point translate, scale, translate Change coordinate systems translate, rotate, scale Does the order of operations matter?
Composition Properties Is matrix multiplication associative? (A.B).C = A.(B.C) ? + + ae a b e f i j bg af bh i j = ce + + c d g h k l dg cf dh k l + + + + + + aei bgi afk bhk aej bgj afl bhl = cei + + + + + + dgi cfk dhk cej dgj cfl dhl + + ei a b e f i j a b fk ej fl = gi + + c d g h k l c d hk gj hl + + + + + + aei afk bgi bhk aej afl bgj bhl = cei + + + + + + cfk dgi dhk cej cfl dgj dhl
Composition Properties Is matrix multiplication commutative? A . B = B . A ? ae + bg ce + dg af + bh cf + dh ? ? ? = ? ? ? ? ea + fc ga + hc eb + fd gb + hd ? ? ? ? ? ? = ?
Order of operations So, it does matter. Let s look at an example: 1. 2. Rotate Translate 1. 2. Translate Rotate
Composite Transformation Matrix Arrange the transformation matrices in order from right to left. General Pivot- Point Rotation Operation :- 1. Translate (pivot point is moved to origin) 2. Rotate about origin 3. Translate (pivot point is returned to original position) T(pivot) R( ) T( pivot) 1 0 tx 0 1 ty 0 0 1 . 1 0 -tx 0 1 -ty 0 0 1 cos -sin 0 sin cos 0 0 0 1 . 1 0 tx 0 1 ty 0 0 1 . cos -sin -tx cos + ty sin sin cos -tx sin - ty cos 0 0 1 cos -sin -tx cos + ty sin + tx sin cos -tx sin - ty cos + ty 0 0 1
Composite Transformation Matrix Example Perform 60 rotation of a point P(2, 5) about a pivot point (1,2). Find P ? x y 1 Sin 60 = 0.8660 Cos 60 = 1/2 cos -sin -tx cos + ty sin + tx sin cos -tx sin - ty cos + ty 0 0 . 1 2 5 1 0.5 -0.866 -1.0.5 + 2.0.866 + 1 0.866 0.5 -1.0.866- 2.0.5 + 2 0 0 1 . 2 5 1 -1.098 4.366 1 0.5 - 0.866 2.232 0.866 0.5 0.134 0 0 1 . = P = (-1, 4)
Composite Transformation Matrix General Fixed-Point Scaling Operation :- 1. Translate (fixed point is moved to origin) 2. Scale with respect to origin 3. Translate (fixed point is returned to original position) T(fixed) S(scale) T( fixed) Find the matrix that represents scaling of an object with respect to any fixed point? Given P(6, 8) , Sx = 2, Sy = 3 and fixed point (2, 2). Use that matrix to find P ?
Answer 1 0 tx 0 1 ty 0 0 1 . 1 0 -tx 0 1 -ty 0 0 1 Sx 0 0 0 Sy 0 0 0 1 . Sx 0 0 Sy -ty Sy 0 0 1 -tx Sx 1 0 tx 0 1 ty 0 0 1 . Sx 0 0 Sy -ty Sy + ty 0 0 1 -tx Sx + tx = x =6, y = 8, Sx = 2, Sy = 3, tx =2, ty = 2 6 8 1 10 20 1 2 0 0 3 0 0 -2(2) + 2 -2(3) + 2 1 = .
Composite Transformation Matrix General Scaling Direction Operation :- 1. Rotate (scaling direction align with the coordinate axes) 2. Scale with respect to origin 3. Rotate (scaling direction is returned to original position) R( ) S(scale) R( ) Find the composite transformation matrix by yourself !!
S . T . R 1 0 tx 0 1 ty 0 0 1 cos -sin 0 sin cos 0 0 0 1 Sx 0 0 0 Sy 0 0 0 1 . . cos -sin tx sin cos ty 0 0 1 Sx 0 0 0 Sy 0 0 0 1. Sxcos Sx(-sin ) Sx tx Sy sin Sy cos Sy ty 0 0 1
Other transformations Reflection: x-axis 0 0 1 0 0 1 y-axis 1 0 0 1 0 0 0 1 0 0 0 1
Other transformations Reflection: origin 0 0 1 0 0 1 line x=y 0 1 0 1 0 0 0 0 1 1 0 0
Other transformations Shear: x-direction sh? 0 1 0 0 1 y-direction 1 0 0 sh? 1 0 0 0 1 1 0 0