2D transformations: An introduction to the maths behind computer graphics Lecturer: Dr Dan Cornford d.cornford@aston.ac.uk http://wiki.aston.ac.uk/dancornford CS2150, Computer Graphics, Aston University, Birmingham, UK http://wiki.aston.ac.uk/c2150 October 12, 2009 Dan Cornford 2D Transformations 1/14
Outline: 2D transformations What are we transforming? Why consider 2D transformations? Revision on vectors and matrices. Definition of common 2D affine transformations. Homogeneous coordinates. Dan Cornford 2D Transformations 2/14
2D geometrical transformations Start by considering 2D objects. Later we will consider scan conversion: objects primitives pixels. This section discusses the methods we use to model 2D objects and transformations of those objects: objects objects. In 2D, possible primitives are points, lines and polygons, which can be combined to give higher level objects. In 2D it is generally easy to see how to represent certain objects. To understand transformations we need to use basic vector algebra! Dan Cornford 2D Transformations 3/14
Vectors A vector is an n-tuple of numbers: [ ] 2 r =. 3 Vectors are denoted by lower case bold letters. The rule for the addition is: r 1 s 1 r 1 + s 1 r + s = r 2 + s 2 = r 2 + s 2. r 3 s 3 r 3 + s 3 Vector addition is commutative: r + s = s + r. Dan Cornford 2D Transformations 4/14
2D transformations Use the vector and matrix algebra. Translations are offsets from the existing position of the object. Consider a point at r. Translate it by an amount t = (t x, t y ) : new location will be r = r + t. Before translation After translation Dan Cornford 2D Transformations 5/14
Matrices m p p n A * = m B C n A matrix is a rectangular array of numbers. A general matrix will be represented by an upper case letter: a 1,1 a 1,2 a 1,3 A = a 2,1 a 2,2 a 2,3. a 3,1 a 3,2 a 3,3 The element on the ith row and jth column is denoted by a i,j. Note that we start indexing at 1, whereas C indexes arrays from 0. Dan Cornford 2D Transformations 6/14
2D transformations Scalings are stretchings of the object, about the origin. The scaling matrix S is: [ ] sx 0 S =, 0 s y r = Sr : s x is the x-axis scaling and s y is the y-axis scaling. If s x = s y = s the scaling is said to be uniform. If not the scaling is called differential. Before scaling After scaling Dan Cornford 2D Transformations 7/14
2D transformations implementation In order to apply the scaling we need to multiple each vertex of the object (a vector) by the scaling matrix: [ ] [ ] [ ] [ ] sx 0 r1 sx r Sr = = 1 + 0 r 2 sx r = 1 0 s y 0 r 1 + s y r 2 s y r 2 r 2, It is important to be able to multiply a matrix times a vector: remember the first element of the answer is the sum of the elements of the first row of the vector times the original vector. Dan Cornford 2D Transformations 8/14
2D transformations Rotations about the origin by an angle θ are defined by the rotation matrix R which is given by: [ ] cos θ sin θ R =. sin θ cos θ The rotated point, r = Rr. A positive θ implies an anti-clockwise rotation. Before rotation After rotation Dan Cornford 2D Transformations 9/14
Homogeneous coordinates Homogeneous coordinates allow us to treat all transformations in the same way, as matrix multiplications. The consequence is that our 2-vectors become extended to 3-vectors, with a resulting increase in storage and processing. We represent a point (x, y) by the extended triple (x, y, w). The normalised homogeneous coordinates are (x/w, y/w, 1). Points with w = 0 are called points at infinity, and are not frequently used. If you like then you can think of 2D space corresponding to the plane w = 1. Dan Cornford 2D Transformations 10/14
Homogeneous coordinates In homogeneous coordinates the transformations are: translation: r = x y = 1 0 t x 0 1 t y x y = T r ; 1 0 0 1 1 scaling: x s x 0 0 x r = y = 0 s y 0 y = Sr ; 1 0 0 1 1 rotation: r = x cos θ sin θ 0 y = sin θ cos θ 0 x y = Rr. 1 0 0 1 1 Dan Cornford 2D Transformations 11/14
Homogeneous coordinates Apply each of these transformations to a vector [x, y, 1] ] and compute the resulting vector: translation: 1 0 t x x? 0 1 t y y =? ; 0 0 1 1? scaling: rotation: s x 0 0 x? 0 s y 0 y =? ; 0 0 1 1? cos θ sin θ 0 x? sin θ cos θ 0 y =?. 0 0 1 1? Dan Cornford 2D Transformations 12/14
Homogeneous coordinates Rigid body transformations preserve length and angles (e.g. translation or rotation). Affine transformations preserve parallelism in lines (e.g. translation, rotation, scaling and shearing). A shear transformation is given by: x 1 h x 0 x r = y = h y 1 0 y = Hr, 1 0 0 1 1 h x and h y represent the amount of shear along the x and y axes respectively. Dan Cornford 2D Transformations 13/14
Summary Having finished this lecture you should: be able to write down the transformation matrices in both Cartesian (normal) and homogeneous coordinates; understand the role of homogeneous coordinates in computer graphics; be able to multiply a vector (vertex) by a matrix (transformation). In the exam I often ask you to apply transformations to simple objects, typically in 2D, so you need to know how to do this! Dan Cornford 2D Transformations 14/14