Representing 2D Transformations as Matrices

Similar documents
Computer Graphics. June 23, 2009

Computing the 3D Viewing Transformation

Hello, welcome to the video lecture series on Digital Image Processing. So in today's lecture

COMP30019 Graphics and Interaction Three-dimensional transformation geometry and perspective

Introduction to Homogeneous coordinates

1 Affine and Projective Coordinate Notation

Math 7 Elementary Linear Algebra PLOTS and ROTATIONS

Computer Graphics Hands-on

To graph the point (r, θ), simply go out r units along the initial ray, then rotate through the angle θ. The point (1, 5π 6

Computer Graphics: Geometric Transformations

2D Object Definition (1/3)

UNIT 2 2D TRANSFORMATIONS

Transformations. Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico

CT5510: Computer Graphics. Transformation BOCHANG MOON

Homework #1. Displays, Image Processing, Affine Transformations, Hierarchical modeling, Projections

Graphics and Interaction Transformation geometry and homogeneous coordinates

COMP30019 Graphics and Interaction Transformation geometry and homogeneous coordinates

To graph the point (r, θ), simply go out r units along the initial ray, then rotate through the angle θ. The point (1, 5π 6. ) is graphed below:

3D Computer Graphics. Jared Kirschner. November 8, 2010

MATRIX REVIEW PROBLEMS: Our matrix test will be on Friday May 23rd. Here are some problems to help you review.

AQA GCSE Further Maths Topic Areas

Computer Graphics 7: Viewing in 3-D

Forward kinematics and Denavit Hartenburg convention

Analysis of Euler Angles in a Simple Two-Axis Gimbals Set

EEE 187: Robotics Summary 2

2D and 3D Transformations AUI Course Denbigh Starkey

Homework #1. Displays, Image Processing, Affine Transformations, Hierarchical Modeling

TImath.com. Geometry. Reflections and Rotations. Time required 45 minutes ID: 8286

Lesson 20: Exploiting the Connection to Cartesian Coordinates

CALCULATING TRANSFORMATIONS OF KINEMATIC CHAINS USING HOMOGENEOUS COORDINATES

Matrices. Chapter Matrix A Mathematical Definition Matrix Dimensions and Notation

Polar Coordinates. 2, π and ( )

form are graphed in Cartesian coordinates, and are graphed in Cartesian coordinates.

Combining Isometries- The Symmetry Group of a Square

Camera Model and Calibration

Supplementary Material: The Rotation Matrix

Finite Element Analysis Dr. B. N. Rao Department of Civil Engineering Indian Institute of Technology Madras. Module - 01 Lecture - 15

CS 4204 Computer Graphics

2D Image Transforms Computer Vision (Kris Kitani) Carnegie Mellon University

Computer Graphics with OpenGL ES (J. Han) Chapter IV Spaces and Transforms

Coordinate Frames and Transforms

Homogeneous Coordinates and Transformations of the Plane

Coordinate transformations. 5554: Packet 8 1

Mathematics 308 Geometry. Chapter 9. Drawing three dimensional objects

Transformations. Write three rules based on what you figured out above: To reflect across the y-axis. (x,y) To reflect across y=x.

Today s class. Viewing transformation Menus Mandelbrot set and pixel drawing. Informationsteknologi

Shadows in Computer Graphics

521493S Computer Graphics Exercise 2 Solution (Chapters 4-5)

+ i a y )( cosφ + isinφ) ( ) + i( a x. cosφ a y. = a x

CSC 305 The Graphics Pipeline-1

Section 10.1 Polar Coordinates

Transforms. COMP 575/770 Spring 2013

COMP 558 lecture 19 Nov. 17, 2010

Game Engineering: 2D

CS3621 Midterm Solution (Fall 2005) 150 points

DD2429 Computational Photography :00-19:00

Transforming Objects in Inkscape Transform Menu. Move

Geometric transformations in 3D and coordinate frames. Computer Graphics CSE 167 Lecture 3

Today s class. Geometric objects and transformations. Informationsteknologi. Wednesday, November 7, 2007 Computer Graphics - Class 5 1

Matrix Transformations The position of the corners of this triangle are described by the vectors: 0 1 ] 0 1 ] Transformation:

2D TRANSFORMATIONS AND MATRICES

Game Engineering CS S-05 Linear Transforms

Overview. Affine Transformations (2D and 3D) Coordinate System Transformations Vectors Rays and Intersections

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

(Refer Slide Time: 00:04:20)

MATHEMATICS FOR ENGINEERING TUTORIAL 5 COORDINATE SYSTEMS

Linear algebra deals with matrixes: two-dimensional arrays of values. Here s a matrix: [ x + 5y + 7z 9x + 3y + 11z

Object Representation Affine Transforms. Polygonal Representation. Polygonal Representation. Polygonal Representation of Objects

2D transformations: An introduction to the maths behind computer graphics

Introduction to Transformations. In Geometry

THE VIEWING TRANSFORMATION

Lesson 5.6: Angles in Standard Position

Kevin James. MTHSC 206 Section 15.6 Directional Derivatives and the Gra

Lesson 28: When Can We Reverse a Transformation?

Part 3: 2D Transformation

OUTLINES. Variable names in MATLAB. Matrices, Vectors and Scalar. Entering a vector Colon operator ( : ) Mathematical operations on vectors.

Computer Graphics: Viewing in 3-D. Course Website:

LINEAR PROGRAMMING: A GEOMETRIC APPROACH. Copyright Cengage Learning. All rights reserved.

Reflection (M): Reflect simple plane figures in horizontal or vertical lines;

OpenGL Matrix/Vector Manipulation using SFML

Rational Numbers: Graphing: The Coordinate Plane

Camera Model and Calibration. Lecture-12

CMSC427: Computer Graphics Lecture Notes Last update: November 21, 2014

Unit 14: Transformations (Geometry) Date Topic Page

EECE 478. Learning Objectives. Learning Objectives. Linear Algebra and 3D Geometry. Linear algebra in 3D. Coordinate systems

On Your Own. ). Another way is to multiply the. ), and the image. Applications. Unit 3 _ _

Geometry Transformations

2-9 Operations with Complex Numbers

How to declare an array in C?

Image warping , , Computational Photography Fall 2017, Lecture 10

Lesson 27: Angles in Standard Position

MAT 343 Laboratory 4 Plotting and computer animation in MATLAB

2D/3D Geometric Transformations and Scene Graphs

Midterm Exam Fundamentals of Computer Graphics (COMP 557) Thurs. Feb. 19, 2015 Professor Michael Langer

Reflections and Rotations TEACHER NOTES

Computer Graphics - Treasure Hunter

Background for Surface Integration

Homework #1. Displays, Image Processing, Affine Transformations, Hierarchical Modeling

So we have been talking about 3D viewing, the transformations pertaining to 3D viewing. Today we will continue on it. (Refer Slide Time: 1:15)

Answers to practice questions for Midterm 1

Transcription:

Representing 2D Transformations as Matrices John E. Howland Department of Computer Science Trinity University One Trinity Place San Antonio, Texas 78212-7200 Voice: (210) 999-7364 Fax: (210) 999-7477 E-mail: jhowland@trinity.edu Web: http://www.cs.trinity.edu/ jhowland/ June 28, 2004 Abstract 2D graphics transformations are represented as matrices. J programs for manipulating transformations such as scaling, rotation and translation are given. Efficiency of matrix representation of transformations is discussed. Subject Areas: 2D Graphics Transformations. Keywords: Modeling, J Programming Language, 2D Graphics Transformations. 1 Introduction In these notes, we consider the problem of representing 2D graphics images which may be drawn as a sequence of connected line segments. Such images may be represented as a matrix of 2D points [ x i y i ]. In the following pages we use the J [Hui 2001] programming notation to describe the various transformations. For example: [ square =: 5 2 $ 1 10 1 1 0 1 10 10 0 10 represents the square shown in Figure 1 The idea behind this representation is that the first point represents the starting point of the first line segment drawn while the second point represents the end of the first line segment and the starting point of the second line segment. The drawing of line segments continues in similar fashion until all line segments have been drawn. A matrix having n + 1 points describes a figure consisting of n line segments. It is sometimes useful to think of each pair of consecutive points in this matrix representation, 1

2 ROTATION (0,10) (10,10) (0,0) (10,0) Figure 1: A Square [ ] xi 1 y i 1 x i y i 2. as as a vector so that the square shown in Figure 1 is the result of drawing the vectors shown in Figure (0,10) (10,10) (0,0) (10,0) Figure 2: The Vectors in A Square 2 Rotation Suppose we wish to rotate a figure around the origin of our 2D coordinate system. Figure 3 shows the point (x, y) being rotated θ degrees (by convention, counter clock-wise direction is positive) about the origin. The equations for changes in the x and y coordinates are: x = x cos(θ) y sin(θ) y = x sin(θ) + y cos(θ) (1) 2

3 SCALING (x,y ) θ (x,y) Figure 3: Rotating a Point About the Origin If we consider the coordinates of the point (x, y) as a one row two column matrix [ x matrix [ cos(θ) sin(θ) ] sin(θ) cos(θ) y ] and the then, given the J definition for matrix product, mp =: +/. *, we can write Equations (1) as the matrix equation [ ] [ ] [ ] x y cos(θ) sin(θ) = x y mp (2) sin(θ) cos(θ) We can define a J monad, rotate, which produces the rotation matrix. This monad is applied to an angle, expressed in degrees. Positive angles are measured in a counter-clockwise direction by convention. rotate =: monad def 2 2 $ 1 1 _1 1 * 2 1 1 2 o. (o. y.) % 180 rotate 90 0 1 _1 0 rotate 360 1 _2.44921e_16 2.44921e_16 1 We can rotate the square of Figure 1 by: square mp rotate 90 0 10 _10 10 _1 producing the rectangle shown in Figure 4. 3 Scaling Next we consider the problem of scaling (changing the size of) a 2D line drawing. Size changes are always made from the origin of the coordinate system. The equations for changes in the x and y coordinates are: 3

3 SCALING ( 10,10) (0,10) ( 10,0) (0,0) Figure 4: The Square, Rotated 90 Degrees x = x S x y = y S y (3) As before, we consider the coordinates of the point (x, y) as a one row two column matrix [ x the matrix [ ] Sx 0 0 S y then, we can write Equations (3) as the matrix equation [ ] [ ] [ ] x y Sx 0 = x y mp 0 S y y ] and We next define a J monad, scale, which produces the scale matrix. This monad is applied to a list of two scale factors for x and y respectively. scale =: monad def 2 2 $ (0 { y.),0,0,(1 { y.) scale 2 3 2 0 0 3 We can now scale the square of Figure 1 by: square mp scale 2 3 2 20 30 0 30 producing the square shown in Figure 5. (4) 4

4 TRANSLATION (0,30) (20,30) (0,0) (20,0) Figure 5: Scaling a Square 4 Translation The third 2D graphics transformation we consider is that of translating a 2D line drawing by an amount T x along the x axis and T y along the y axis. The translation equations may be written as: x = x + T x y = y + T y (5) We wish to write the Equations 5 as a single matrix equation. This requires that we find a 2 by 2 matrix, [ ] a b c d such that x a + y c = x + T x. From this it is clear that a = 1 and c = 0, but there is no way to obtain the T x term required in the first equation of Equations 5. Similarly we must have x b + y d = y + T y. Therefore, b = 0 and d = 1, and there is no way to obtain the T y term required in the second equation of Equations 5. 4.1 Homogenous Coordinates From the above argument we now see the impossibility of representing a translation transformation as a 2 by 2 matrix. What is required at this point is to change the setting (2D coordinate space) in which we phrased our original problem. In geometry, when one encounters difficulty when trying to solve a problem in n space, it is customary to attempt to re-phrase and solve the problem in n + 1 space. In our case this means that we should look at our 2D problem in 3 dimensional space. But how can we do this? Consider that, given a point (x, y) in 2 space, we map that point to (x, y, 1). That is, we inject each point in the 2D plane into the corresponding point in 3 space in the plane z = 1. If we are able to solve our problem in this plane and find that the solution lies in the plane z = 1, then we may project this solution back to 2 space by mapping each point (x, y, 1) to (x, y). To summarize, we inject the 2D plane into 3 space by the mapping (x, y) (x, y, 1) (6) 5

4 TRANSLATION 4.1 Homogenous Coordinates Then we solve our problem, ensuring that our solution lies in the plane z = 1. obtained by the projection of the plane z = 1 on 2 space by the mapping Our final answer is (x, y, 1) (x, y) (7) This process is referred to as using homogeneous coordinates. In the context of our problem (finding matrix representations of rotation, scaling and translation transformations) we must inject our 2D line drawings into the plane z = 1. In J we do this by using stitch,,.. square,. 1 1 1 1 10 10 1 0 10 1 1 We now must rewrite the Equations 5 as Consider the 3 by 3 matrix x = x + T x y = y + T y z = z 1 T x T y 1 (8) We now see that the Equations 8 may be written as the matrix equation [ x y 1 ] = [ x y 1 ] mp 1 (9) T x T y 1 We define the J monad translate, which is applied to a list of two translate values T x T y. translate =: monad def 3 3 $ 1, y., 1 translate 10 _10 1 10 _10 1 We translate the square of Figure 1 by (square,. 1) mp translate 10 _10 10 _10 1 20 _10 1 2 1 1 1 10 _10 1 6

5 SCALING AND ROTATION USING HOMOGENEOUS COORDINATES 4.2 Efficiency of Transformations 4.2 Efficiency of Transformations Notice that the translate matrix (having a last column 1) always produces a result which lies in the plane z = 1. We can perform the translation operation and project the result back on the 2D plane (saving computation time by not doing unnecessary multiplications and additions) by (square,. 1) mp 3 2 {. translate 10 _10 10 _10 20 _10 2 1 10 _10 producing the translated square shown in Figure 6 (10,0) (20,0) (10, 10) (20, 10) Figure 6: Translating a Square 5 Scaling and Rotation Using Homogeneous Coordinates We want to be able to combine sequences of rotations, scaling and translations together as a single 2D graphics transformation. We accomplish this by simply multiplying the matrix representations of each transformation using matrix multiplication. However, to do this, we must go back and rewrite the Equations 1 and 3 as the following: x = x cos(θ) y sin(θ) y = x sin(θ) + y cos(θ) z = z (10) x = x S x y = y S y z = z Similarly we rewrite the matrix Equations 2 and 4 as: [ x y 1 ] = [ x y 1 ] mp cos(θ) sin(θ) 0 sin(θ) cos(θ) 0 1 (11) (12) 7

5 SCALING AND ROTATION USING HOMOGENEOUS COORDINATES [ x y 1 ] = [ x y 1 ] mp S x 0 S y 0 1 (13) We extend our earlier J definitions of rotate and scale to the homogenous coordinate system. rotate =: monad def ((2 2 $ 1 1 _1 1 * 2 1 1 2 o. (o. y.) % 180),.0), 1 rotate 180 _1 0 _1 0 1 (square,. 1) mp 3 2 {. rotate 180 _1 _10 _10 0 _10 ( 10,0) (0,0) ( 10, 10) (0, 10) Figure 7: Rotating a Square 180 Degrees scale =: monad def 3 3 $ (0 { y.), 0, (1 { y.), 0 1 scale 2 3 2 0 3 0 1 (square,. 1) mp 3 2 {. scale 2 3 2 20 30 0 30 Figure 5 shows the resulting scaled square. 8

6 COMBINING TRANSFORMATIONS 6 Combining Transformations We can now combine together two transformations to form a single graphics operation. For example, suppose we wish to first rotate an object 90 degrees and then scale the object by 2 along the x axis. The rotation would be expressed as: [r =: rotate 90 _1 1 Then the scaling operation would be expressed as: [s =: scale 2 1 2 1 Applying these operations to the square, we have: (((square,. 1) mp 3 2 {. r),. 1) mp 3 2 {. s 0 10 _20 10 _2 6.1 Efficiency of Operations However, notice that (square,. 1) mp 3 2 {. r mp s 0 10 _20 10 _2 produces the same result using far fewer multiplications and additions. Figure 8 shows the rotated and scaled square. We are allowed to perform the matrix multiplications of r and s before multiplying by square,. 1 because matrix multiplication is associative. Be careful! Matrix multiplication is not commumative. r mp s _2 1 s mp r 0 2 0 _1 1 9

7 ROTATING AN OBJECT ABOUT A POINT ( 20,10) (0,10) ( 20,0) (0,0) Figure 8: Rotated and Scaled Square This means we must be careful about the order of application of graphics transformations. One might be concerned about whether or not multiplying rotation, scaling and/or translation matrices produces a transformation which leaves our 2D lines in the plane z = 1. We can answer this question by observing that each of these matrices has a last column of 1. Hence, when multiplying any two of these matrices, the product matrix has a last column of 1. 7 Rotating an Object About a Point As a final example, suppose we wish to rotate the square of Figure 1 90 degrees about its upper right corner. We must first translate the point (10, 10) to the origin. This is the matrix translate _10 _10 1 _10 _10 1 Then we must rotate 90 degrees rotate 90 _1 1 Finally, we translate the square back with the matrix translate 10 10 1 10 10 1 Putting this all together we have: [xform =: (translate _10 _10) mp (rotate 90) mp translate 10 10 10

REFERENCES REFERENCES _1 2 1 (square,. 1) mp 3 2 {. xform 2 20 10 10 10 1 2 which is shown in Figure 9. (10,10) (20,10) (10,0) (20,0) Figure 9: Rotating a Square 90 Degrees About (10,10) References [Hui 2001] Hui, Roger K. W., Iverson, Kenneth E., J Dictionary, J Software, Toronto, Canada, May 2001. 11