Chapter 5 Geometric Transformations Computer Graphics Chapter 5 Geometric Transformations Andreas Savva
2D Translation Repositioning an object along a straight line path from one co-ordinate location to another (x,y) (x’,y’) To translate a 2D position, we add translation distances tx and ty to the original coordinates (x,y) to obtain the new coordinate position (x’,y’) x’= x + tx , y’= y + ty Matrix form T x y
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 (a) (b)
Translating a Polygon class Point2D { public: GLfloat x, y; }; void translatePoly(Point2D P[], GLint n, GLfloat tx, GLfloat ty) { GLint i; for (i=0; i<n; i++) { P[i].x = P[i].x + tx; P[i].y = P[i].y + ty; } glBegin (GL_POLYGON); for (i=0; i<n; i++) glVertex2f(P[i].x, P[i].y); glEnd();
2D Rotation Repositioning an object along a circular path in the xy-plane x y (x,y) (x’,y’) φ θ r The original coordinates are:
2D Rotation Substituting x y (x,y) (x’,y’) φ θ r Matrix form
2D Rotation about a Pivot position Rotating about pivot position (xr, yr) x y (x,y) (x’,y’) φ θ r xr yr
Translating a Polygon class Point2D { public: GLfloat x, y; }; void rotatePoly(Point2D P[], GLint n, Point2D pivot, GLdouble theta) { Point2D *V; V = new Point2D[n]; GLint i; for (i=0; i<n; i++) { V[i].x = pivot.x + (P[i].x – pivot.x) * cos(theta) - (P[i].y – pivot.y) * sin(theta); V[i].y = pivot.y + (P[i].x – pivot.x) * sin(theta) - (P[i].y – pivot.y) * cos(theta); } glBegin (GL_POLYGON); for (i=0; i<n; i++ glVertex2f(V[i].x, V[i].y); glEnd(); delete[] V;
Reduced in size and moved 2D Scaling Altering the size of an object. Sx and Sy are the scaling factors. If Sx = Sy then uniform scaling. x y Sx = Sy = ½ Matrix form Sx = Sy = ½ x’ x Reduced in size and moved closer to the origin
2D Scaling relative to Fixed point Scaling relative to fixed point (xf, yf) x y Sx = ¼ , Sy = ½ P1 P2 P3 P1’ P2’ P3’ (xf , yf) OR where the additive terms xf(1-Sx) and yf(1-Sy) are constants for all points in the object.
Translating a Polygon class Point2D { public: GLfloat x, y; }; void scalePoly(Point2D P[], GLint n, Point2D fixedPt, GLfloat Sx, GLfloat Sy) { Point2D *V; V = new Point2D[n]; GLfloat addx = fixedPt.x * (1 – Sx); GLfloat addy = fixedPt.y * (1 – Sy); GLint i; for (i=0; i<n; i++) { V[i].x = P[i].x * Sx + addx; V[i].y = P[i].y * Sy + addy; } glBegin (GL_POLYGON); for (i=0; i<n; i++ glVertex2f(V[i].x, V[i].y); glEnd(); delete[] V;
Matrix Representation Use 3×3 matrices to combine transformations Translation Rotation Scaling
Inverse Transformations Translation Rotation Scaling
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)
Result Step-by-step x y x y x y T(-20, -10) R(90º) (30, 25) (-15, 10) (10, 10) (30, 25) (0, -10) (-15, 10) Step-by-step x y (10, 15) (-10, 0) x y (0, -10) (-15, 10) T(-20, -10) R(90º)
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
Exercises Composite 2D Transformations Translation: Show that: Rotation: Show that: Scaling: Show that:
General 2D Pivot-Point Rotation x y (xr , yr) x y Original position and Pivot Point Translate Object so that Pivot Point is at origin x y (xr , yr) x y Rotation about origin Translate object so that Pivot Point is return to position (xr , yr)
General Pivot-point Rotation Using Matrices
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
General 2D Fixed-Point Scaling y (xf , yf) x y Original position and Fixed Point Translate Object so that Fixed Point is at origin (xf , yf) x y x y Scale Object with respect to origin Translate Object so that Fixed Point is return to position (xf , yf)
General 2D Fixed-Point Scaling Using Matrices
Exercises Consider the following object: Scale it by sx = 2 and sy = ½ relative to the fixed point (140, 125) and display it. Apply a rotation by 90° on the Pivot Point (50, 60) and then scale it by sx = sy = 2 relative to the Fixed Point (0, 200). Display the result. Scale it sx = sy = ½ relative to the Fixed Point (50, 60) and then rotate it by 180° on the Pivot Point (50, 60). Display the final result. x y 60 50 125 220
Order of Transformations Object is first translated in the x direction and then rotated by 90º x y Object is first rotated by 90º and then translated in the x direction x y
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
2D Shear x-direction shear Matrix form y x Initial object y x shx = 2 1 Matrix form x y shx = 2 1 2 3
2D Shear x-direction relative to other reference line Matrix form y x 1 yref = -1 Matrix form y x shx = ½, yref = -1 1 2 3 yref = -1
2D Shear y-direction shear Matrix form y x Initial object y x shy = 2 1 Matrix form x y shy = 2 1 2 3
2D Shear y-direction relative to other reference line Matrix form y x 1 xref = -1 Matrix form y x 1 2 xref = -1 shy = ½, xref = -1
Transformations between 2D Coordinate Systems x y x0 x’ y’ y0 To translate object descriptions from xy coordinates to x’y’ coordinates, we set up a transformation that superimposes the x’y’ axes onto the xy axes. This is done in two steps: Translate so that the origin (x0, y0) of the x’y’ system is moved to the origin (0, 0) of the xy system. Rotate the x’ axis onto the x axis.
Transformations between 2D Coordinate Systems i.e. 1) 2) Concatenating:
Example Find the x’y’-coordinates of the xy points (10, 20) and (35, 20), as shown in the figure below: x y 30 x’ y’ 10 30º (10, 20) (35, 20)
y y’ y’ x’ x’ x (-12.38, 18.66) (35, 20) (10, 20) (9.31, 6.16) 30º 10
Exercise Find the x’y’-coordinates of the rectangle shown in the figure below: x y 10 x’ y’ 60º 20
x’= x + tx , y’= y + ty , z’= z + tz 3D Translation Repositioning an object along a straight line path from one co-ordinate location to another (x,y,z) (x’,y’,z’) To translate a 3D position, we add translation distances tx ty and tz to the original coordinates (x,y,z) to obtain the new coordinate position (x’,y’) x’= x + tx , y’= y + ty , z’= z + tz Matrix form (4 × 4) T(tx, ty, tz) x y z
3D Rotation x y z z-axis The 2D z-axis rotation equations are extended to 3D. Matrix form x y z
3D Rotation x-axis x y z Matrix form
3D Rotation y-axis x y z Matrix form
3D Scaling x y z Matrix form
Other 3D Transformations Reflection z-axis x y z Shears z-axis