490 likes | 812 Views
Computer Graphics. Chapter 5 Geometric Transformations. Andreas Savva. y. T. x. 2D Translation. Repositioning an object along a straight line path from one co-ordinate location to another ( x,y ) ( x’,y’ )
E N D
Computer Graphics Chapter 5 Geometric Transformations Andreas Savva
y T x 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
20 20 15 15 10 10 y y 5 5 5 5 10 10 15 15 20 20 x x 2D Translation • Moving a polygon from position (a) to position (b) with the translation vector (-5, 10), i.e. (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(); }
r r θ φ y (x’,y’) (x,y) x 2D Rotation • Repositioning an object along a circular path in the xy-plane The original coordinates are:
r r θ φ y (x’,y’) (x,y) x 2D Rotation • Substituting Matrix form
(x’,y’) r θ r (x,y) φ yr xr y x 2D Rotation about a Pivot position • Rotating about pivot position (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; }
Sx = Sy = ½ y x’ x Reduced in size and moved closer to the origin Sx = Sy = ½ x 2D Scaling • Altering the size of an object. Sx and Sy are the scaling factors. If Sx = Sy then uniform scaling. Matrix form
P1 P1’ (xf , yf) P2’ P2 P3’ P3 Sx = ¼ , Sy = ½ y x 2D Scaling relative to Fixed point • Scaling relative to fixed point(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
(30, 25) (10, 10) y x Example • Consider the line with endpoints (10, 10) and (30, 25). Translate it by tx = -20, ty = -10 and then rotate it by θ = 90º. Right-to-left
(30, 25) (10, 10) y x Solution
y (30, 25) (-15, 10) (10, 10) x (0, -10) Solution (continue) Point (10, 10) Point (30, 25)
y (30, 25) (-15, 10) (10, 10) x (0, -10) y y (10, 15) (-15, 10) (-10, 0) x x (0, -10) Result Step-by-step T(-20, -10) R(90º)
y 25 10 x 10 45 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º.
Exercises • Composite 2D Transformations • Translation: Show that: • Rotation: Show that: • Scaling: Show that:
(xr , yr) y y y y x x x x (xr , yr) General 2D Pivot-Point Rotation Original position and Pivot Point Translate Object so that Pivot Point is at origin Rotation about origin Translate object so that Pivot Point is return to position (xr , yr)
General Pivot-point Rotation Using Matrices
y 25 10 x 10 45 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.
(xf , yf) y y y y x x x x (xf , yf) General 2D Fixed-Point Scaling Original position and Fixed Point Translate Object so that Fixed Point is at origin 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
y 125 50 x 60 220 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.
y y x x Object is first translated in the x direction and then rotated by 90º Object is first rotated by 90º and then translated in the x direction Order of Transformations
y y x x Reflection of an object about the x axis Reflection of an object about the y axis Reflection About the x axis About the y axis
y x Same as a rotation with 180º y = x y x Reflection Relative to the coordinate origin With respect to the line y = x
1 1 Initial object y y 1 1 3 2 shx = 2 x x 2D Shear • x-direction shear Matrix form
y 1 1 x yref = -1 yref = -1 1 1 3 x 2 shx = ½, yref = -1 2D Shear • x-direction relative to other reference line Matrix form y
1 1 Initial object 3 y y 2 1 1 shy = 2 x x 2D Shear • y-direction shear Matrix form
y 1 x 1 xref = -1 xref = -1 y 2 1 x 1 2D Shear • y-direction relative to other reference line Matrix form shy = ½, xref = -1
y y’ y0 x x’ x0 Transformations between 2D Coordinate Systems • 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 2DCoordinate Systems • i.e. 1) 2) Concatenating:
y y’ 30º 10 (35, 20) x x’ 30 (10, 20) Example • Find the x’y’-coordinates of the xy points (10, 20) and (35, 20), as shown in the figure below:
y’ y’ (-12.38, 18.66) x’ y (9.31, 6.16) 30º 10 x’ 30 (35, 20) x (10, 20)
y y’ 20 10 60º x x’ 10 Exercise • Find the x’y’-coordinates of the rectangle shown in the figure below:
y T(tx, ty, tz) x z 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 txty 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)
y y x x z z 3D Rotation • z-axis • The 2D z-axis rotation equations are extended to 3D. Matrix form
y x z 3D Rotation • x-axis Matrix form
y x z 3D Rotation • y-axis Matrix form
y x z 3D Scaling Matrix form
y x z Other 3D Transformations • Reflection z-axis • Shears z-axis