Two-Dimensional Geometric Transformations
Total Page:16
File Type:pdf, Size:1020Kb
Two-Dimensional Geometric Transformations 1 Basic Two-Dimensional Geometric Transformations 2 Matrix Representations and Homogeneous Coordinates 3 Inverse Transformations 4 Two-Dimensional Composite Transformations 5 Other Two-Dimensional Transformations 6 Raster Methods for Geometric Transformations 7 OpenGL Raster Transformations 8 Transformations between Two-Dimensional Coordinate Systems 9 OpenGL Functions for Two-Dimensional Geometric Transformations 10 OpenGL Geometric-Transformation o far, we have seen how we can describe a scene in Programming Examples S 11 Summary terms of graphics primitives, such as line segments and fill areas, and the attributes associated with these primitives. Also, we have explored the scan-line algorithms for displaying output primitives on a raster device. Now, we take a look at transformation operations that we can apply to objects to reposition or resize them. These operations are also used in the viewing routines that convert a world-coordinate scene description to a display for an output device. In addition, they are used in a variety of other applications, such as computer-aided design (CAD) and computer animation. An architect, for example, creates a layout by arranging the orientation and size of the component parts of a design, and a computer animator develops a video sequence by moving the “camera” position or the objects in a scene along specified paths. Operations that are applied to the geometric description of an object to change its position, orientation, or size are called geometric transformations. Sometimes geometric transformations are also referred to as modeling transformations, but some graphics packages make a From Chapter 7 of Computer Graphics with OpenGL®, Fourth Edition, Donald Hearn, M. Pauline Baker, Warren R. Carithers. Copyright © 2011 by Pearson Education, Inc. Published by Pearson Prentice Hall. All rights reserved. Two-Dimensional Geometric Transformations distinction between the two. In general, modeling transformations are used to con- struct a scene or to give the hierarchical description of a complex object that is com- posed of several parts, which in turn could be composed of simpler parts, and so forth. For example, an aircraft consists of wings, tail, fuselage, engine, and other com- ponents, each of which can be specified in terms of second-level components, and so on, down the hierarchy of component parts. Thus, the aircraft can be described in terms of these components and an associated “modeling” transformation for each one that describes how that component is to be fitted into the overall aircraft design. Geometric transformations, on the other hand, can be used to describe how objects might move around in a scene during an animation sequence or simply to view them from another angle. Therefore, some graphics packages provide two sets of transforma- tion routines, while other packages have a single set of functions that can be used for both geometric transformations and modeling transformations. 1 Basic Two-Dimensional Geometric Transformations The geometric-transformation functions that are available in all graphics pack- ages are those for translation, rotation, and scaling. Other useful transformation routines that are sometimes included in a package are reflection and shearing operations. To introduce the general concepts associated with geometric trans- formations, we first consider operations in two dimensions. Once we under- stand the basic concepts, we can easily write routines to perform geometric trans- formations on objects in a two-dimensional scene. Two-Dimensional Translation y We perform a translation on a single coordinate point by adding offsets to its coordinates so as to generate a new coordinate position. In effect, we are moving the original point position along a straight-line path to its new location. Simi- larly, a translation is applied to an object that is defined with multiple coordinate PЈ positions, such as a quadrilateral, by relocating all the coordinate positions by the same displacement along parallel paths. Then the complete object is displayed at T the new location. To translate a two-dimensional position, we add translation distances tx and P ′ ′ ty to the original coordinates ( x, y) to obtain the new coordinate position ( x , y ) x as shown in Figure 1. ′ ′ F I G U R E 1 x = x + tx, y = y + ty (1) P Translating a point from position to The translation distance pair (t , t ) is called a translation vector or shift vector. position P′ using a translation x y vector T. We can express Equations 1 as a single matrix equation by using the follow- ing column vectors to represent coordinate positions and the translation vector: ′ x ′ x tx P = , P = ′ , T = (2) · y ¸ · y ¸ · ty ¸ This allows us to write the two-dimensional translation equations in the matrix form P′ = P + T (3) Two-Dimensional Geometric Transformations y 10 5 0 5 10 15 20 x (a) y 10 5 F I G U R E 2 0 5 10 15 20 x Moving a polygon from position (a) to position (b) with the (b) translation vector ( −5.50, 3.75). Translation is a rigid-body transformation that moves objects without defor- mation. That is, every point on the object is translated by the same amount. A straight-line segment is translated by applying Equation 3 to each of the two line endpoints and redrawing the line between the new endpoint positions. A polygon is translated similarly. We add a translation vector to the coordinate position of each vertex and then regenerate the polygon using the new set of vertex coordinates. Figure 2 illustrates the application of a specified translation vector to move an object from one position to another. The following routine illustrates the translation operations. An input transla- tion vector is used to move the n vertices of a polygon from one world-coordinate position to another, and OpenGL routines are used to regenerate the translated polygon. class wcPt2D { public: GLfloat x, y; }; void translatePolygon (wcPt2D * verts, GLint nVerts, GLfloat tx, GLfloat ty) { GLint k; for (k = 0; k < nVerts; k++) { verts [k].x = verts [k].x + tx; verts [k].y = verts [k].y + ty; } glBegin (GL_POLYGON); for (k = 0; k < nVerts; k++) glVertex2f (verts [k].x, verts [k].y); glEnd ( ); } If we want to delete the original polygon, we could display it in the back- ground color before translating it. Other methods for deleting picture components Two-Dimensional Geometric Transformations are available in some graphics packages. Also, if we want to save the original poly- gon position, we can store the translated positions in a different array. Similar methods are used to translate other objects. To change the position of a circle or ellipse, we translate the center coordinates and redraw the figure in the new location. For a spline curve, we translate the points that define the curve path and then reconstruct the curve sections between the new coordinate positions. Two-Dimensional Rotation We generate a rotation transformation of an object by specifying a rotation axis and a rotation angle. All points of the object are then transformed to new positions u by rotating the points through the specified angle about the rotation axis. yr A two-dimensional rotation of an object is obtained by repositioning the object along a circular path in the xy plane. In this case, we are rotating the object about a rotation axis that is perpendicular to the xy plane (parallel to the coordinate θ xr z axis). Parameters for the two-dimensional rotation are the rotation angle and a position ( x , y ), called the rotation point (or pivot point), about which the FIGURE 3 r r object is to be rotated (Figure 3). The pivot point is the intersection position Rotation of an object through angle θ of the rotation axis with the xy plane. A positive value for the angle θ defines a about the pivot point (xr , yr ). counterclockwise rotation about the pivot point, as in Figure 3, and a negative value rotates objects in the clockwise direction. To simplify the explanation of the basic method, we first determine the trans- formation equations for rotation of a point position P when the pivot point is at the coordinate origin. The angular and coordinate relationships of the origi- nal and transformed point positions are shown in Figure 4. In this figure, r is the constant distance of the point from the origin, angle φ is the original angu- (xЈ, yЈ) lar position of the point from the horizontal, and θ is the rotation angle. Using standard trigonometric identities, we can express the transformed coordinates in r terms of angles θ and φ as u ′ r (x, y) x = r cos(φ + θ) = r cos φ cos θ − r sin φ sin θ (4) ′ = + = + f y r sin(φ θ) r cos φ sin θ r sin φ cos θ The original coordinates of the point in polar coordinates are F I G U R E 4 x = r cos φ, y = r sin φ (5) Rotation of a point from position ( x , y) to position ( x ′, y ′) through an angle θ Substituting expressions 5 into 4, we obtain the transformation equations for relative to the coordinate origin. The rotating a point at position ( x, y) through an angle θ about the origin: original angular displacement of the x′ = x cos θ − y sin θ point from the x axis is φ. (6) y′ = x sin θ + y cos θ With the column-vector representations 2 for coordinate positions, we can write the rotation equations in the matrix form P′ = R · P (7) where the rotation matrix is cos θ − sin θ R = (8) " sin θ cos θ # A column-vector representation for a coordinate position P, as in Equa- tions 2, is standard mathematical notation. However, early graphics systems sometimes used a row-vector representation for point positions.