Computer Graphic 2 D Transformation
Outline 1. Why transformations ? 2. Basic 2D Transformations Translation Scaling Rotation 3. Homogeneous Coordinate system 4. Composite Transformations 5. Reflection and Shearing
Why transformations ? In graphics, once we have an object described, transformations are used to move that object, scale it and rotate it
Basic 2D Transformations Translation Simply moves an object from one position to another Note: House shifts position relative to origin y x 1 2 3 4 5 6 7 8 9 10
2D Translation Moving a polygon from position (a) to position (b) with the translation vector (-5, 10), i.e. x y 5 10 15 20 x y 5 10 15 20 p3(15,10) P1(10,5) P2(20,5) (a) (b)
Basic 2D Transformations Scaling Scalar multiplies all coordinates WATCH OUT: Objects grow and move! Note: House shifts position relative to origin y x 1 2 3 4 5 6 7 8 9 10
Example: Non- Uniform Scaling (b) Non-uniform scaling
Basic 2D Transformations Rotation sin(a+b) = sin(a)cos(b) + sin(b)cos(a) cos(a+b) = cos(a)cos(b) - sin(a)sin(b) sin(a+b) = sin(a)cos(b) + sin(b)cos(a) cos(a+b) = cos(a)cos(b) - sin(a)sin(b) Similarly,
Rotation
Homogenous 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)
Why Homogenous 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!
Example: Rotate a polygonal object defined by vertices A(0,0),B(1,0) ,C(1,1) and D(0,1) by 45 about the origin
Points are always rotated about the origin Rotation Points are always rotated about the origin y 6 5 4 3 2 1 x 1 2 3 4 5 6 7 8 9 10
2D Transformations in Homogenous Notation Rotation Scaling Translation
Inverse Transformations Inverse of Rotation Inverse of Scaling Inverse of Translation
Example Consider the line with endpoints (10, 10) and (30, 25). Translate it by tx = -20, ty = -10 and then rotate it by θ = 90º. x y (10, 10) (30, 25) Right-to-left
Solution x y (10, 10) (30, 25)
Solution (continue) Point (10, 10) Point (30, 25) y x (30, 25) (0, -10) (-15, 10) Point (10, 10) Point (30, 25)
Exercises Consider the following object: Apply a rotation by 145º then scale it by Sx=2 and Sy=1.5 and then translate it by tx=20 and ty=-30. Scale it by Sx=½ and Sy=2 and then rotate it by 30º. Apply a rotation by 90º and then another rotation by 45º. Apply a rotation by 135º. x y 10 25 45
Scaling about a Reference Point If the center of the object is not at the origin, in this case we should consider a scaling as a composite transformation which involves the following steps. Translation of the reference (xc,yc) to the origin Scale the object by scale factor by (Sx,Sy) Translation of the reference point back to its original position
(a) Original position of object and fixed point (xc, yc)T (a) Original position of object and fixed point (b) Translate object so that fixed point (xr, yr) is at origin (xc, yc)T (c) Scale object with respect to origin (d) Translate object so that fixed point is returned to position (xr, yr)T 22
Combining the Transformation The three transformation matrices are combined as follows
Example1: Consider a triangle having a vertices at A(0,0), B(5,1), and C(3,4) scale this triangle using scaling factors Sx=2 and Sy=1 about a) the origin b) about the reference point (2,3) a) At origin ( xr=0, yr=0)
b) Scaling about the reference point (2,3) : Here, the transformation matrix
Rotation about an Arbitrary Point Rotation about an arbitrary point is composite transformation . This operation involve s three basic transformations: Translation of the object center (xc,yc) to the origin Rotation of the object about the origin by angle θ in.say, the anticlockwise direction Translation of the rotated object back to its original position
1 2 3 4
Example: Consider a triangle having a vertices at A(0,0), B(5,1), and C(3,4) scale this triangle. Rotate the triangle by 90 about a) the origin b) about the reference point (2,3) Solution: a) about the origin The transformation matrix for rotation about the origin :
b) Rotation about point (2,3 )
Exercises Consider the following object: Apply a rotation by 60° on the Pivot Point (-10, 10) and display it. Apply a rotation by 30° on the Pivot Point (45, 10) and display it. Apply a rotation by 270° on the Pivot Point (10, 0) and then translate it by tx = -20 and ty = 5. Display the final result. x y 10 25 45
Reflection About the x axis About the y axis y x y x Reflection of an object about the x axis x y About the y axis x y Reflection of an object about the y axis
Same as a rotation with 180º Reflection Relative to the coordinate origin Same as a rotation with 180º x y With respect to the line y = x x y y = x
Reflection about x=-y line : the x and y coordinates are interchanged and their values are negated after reflection Original position y y = -x 3 1 2 Reflected position 1’ 3’ 2’ x
Reflection about any arbitrary line: y=mx+b This kind of reflection can be implemented by a sequence of the following steps: Translate the line, along the y-axis so that it pass the origin Rotate through an angle θ =tan-1(m) in clockwise so that the line is aligned with x-axis Reflect the object about x-axis Perform inverse Rotation and Translation
Example: Consider a triangle whose vertices are (2 2), (4 2) and (4 4) Example: Consider a triangle whose vertices are (2 2), (4 2) and (4 4). Find the concatenated transformation matrix and the transformed vertices for rotation of 90 about the origin followed by reflection through the line y = -x. Comment on the sequence of transformations.
Ex: Reflect triangle (2,4),(4,6),(2,6) about line y=0.5(x+4) Translate (0, -2) so that the line passes through the origin b ϴ Rotate the line about the x axis by -θo
Reflect object about the x axis Rotate back the line by θo
5. Translate back (0,2)
The final position of line after reflection about the line is
Shearing about the origin y y y (1, 3) (1, 2) (1, 1) (2, 1) (3, 1) (0, 1) (0, 1) x x x (0, 0) (1, 0) (0, 0) (1, 0) (0, 0) (a) (b) (c) SHy=2 SHx=2 41
Shearing about the a reference point ( xr,yr)
Shear Example