Introduction Trig & triangles Transformations Vectors Matrices Transformation matrices CE325: 3D Computer Graphics 3. Coordinate Geometry Adrian F. Clark alien@essex.ac.uk VASE Laboratory, School of Computer Science and Electronic Engineering University of Essex 2011–12 /home/alien/environment/tex/ces- Introduction Trig & triangles Transformations Vectors Matrices Transformation matrices Outline 1 Introduction 2 Trig & triangles 3 Transformations 4 Vectors 5 Matrices 6 Transformation matrices /home/alien/environment/tex/ces- Introduction Trig & triangles Transformations Vectors Matrices Transformation matrices Introduction It’s time for us to get to grips with the mathematical part of CE325. We’ll start from basics and go through things fairly slowly. To read up on the mathematics, find a good maths book — there are plenty in the library. You might find the summaries and examples at http://www.mathcentre.ac.uk/ useful too. If you find you cannot handle the maths, do make an appointment to see me — and bring along whatever attempts you have made to understand things yourself. /home/alien/environment/tex/ces- Introduction Trig & triangles Transformations Vectors Matrices Transformation matrices Right-Angled Triangles: Notation Consider the right-angled triangle with sides: h: hypotenuse a: adjacent o: opposite then Pythagoras’s theorem tells us h 2 = a2 + o 2 . /home/alien/environment/tex/ces- Introduction Trig & triangles Transformations Vectors Matrices Transformation matrices Sines, cosines and tangents We define the cosine and sine from the relationships o sin θ = h a cos θ = h and we define the tangent as sin θ o tan θ = = cos θ a The last is obtained by substituting from above. From Pythagoras’ theorem cos2 θ + sin2 θ = 1 /home/alien/environment/tex/ces- Introduction Trig & triangles Transformations Vectors Matrices Transformation matrices Sums and differences of angles It is possible to show that sin(θ ± φ) = sin θ cos φ ± cos θ sin φ cos(θ ± φ) = cos θ cos φ ∓ sin θ sin φ tan θ ± tan φ tan(θ ± φ) = 1 ∓ tan θ tan φ The tan relationship is true providing θ + φ 6= (k + 12 )π, k ∈ Z. /home/alien/environment/tex/ces- Introduction Trig & triangles Transformations Vectors Matrices Transformation matrices From these sum-and-difference equations, one can show that sin 2θ = 2 sin θ cos θ cos 2θ = cos2 θ − sin2 θ 2 tan θ tan 2θ = 1 − tan2 θ The last of these requires that θ 6= (k + 12 )π, k ∈ Z. /home/alien/environment/tex/ces- Introduction Trig & triangles Transformations Vectors Matrices Transformation matrices It also follows that 2 cos2 θ = 1 + cos 2θ 2 sin2 θ = 1 − cos 2θ 2 sin θ cos φ = sin(θ − φ) + sin(θ + φ) 2 cos θ cos φ = cos(θ − φ) + cos(θ + φ) 2 sin θ sin φ = cos(θ − φ) − cos(θ + φ) /home/alien/environment/tex/ces- Introduction Trig & triangles Transformations Vectors Matrices Transformation matrices 2D geometric transformations There are three basic types of transformation: translation scaling rotation As we shall see, other types of transformation are obtained by combinations of these primitive operations. /home/alien/environment/tex/ces- Introduction Trig & triangles Transformations Vectors Matrices Transformation matrices 2D translation y Consider the movement of the point P = (x, y )0 to P0 = (x 0 , y 0 )0 . P dy We have the two equations P’ x 0 = x + dx y 0 = y + dy dx x /home/alien/environment/tex/ces- Introduction Trig & triangles Transformations Vectors Matrices Transformation matrices 2D scaling y If we have x 0 = xSx and y 0 = ySy , we have scaled the point P to P0 . Note that the scale factor may be different in x and y : this would turn a circle into an ellipse, for example. P’ P x /home/alien/environment/tex/ces- Introduction Trig & triangles Transformations Vectors Matrices Transformation matrices 2D rotation y P’ y’ r P y r θ φ x’ x x /home/alien/environment/tex/ces- Introduction Trig & triangles Transformations Vectors Matrices Transformation matrices We have x = r cos φ and y = r sin φ and we can see that x 0 = r cos(θ + φ) = r cos θ cos φ − r sin θ sin φ y 0 = r sin(θ + φ) = r cos θ sin φ + r sin θ cos φ Substituting for x and y , we get x 0 = x cos θ − y sin θ y 0 = x sin θ + y cos θ /home/alien/environment/tex/ces- Introduction Trig & triangles Transformations Vectors Matrices Transformation matrices Properties of transformations Points on a straight line remain on a straight line after transformation Consequently, we need transform only the end-points of vectors Parallel lines remain parallel (also apparent from our example) Intersecting lines remain intersecting — we can show this by drawing a line across the diagonal of a square and then rotating the square /home/alien/environment/tex/ces- Introduction Trig & triangles Transformations Vectors Matrices Transformation matrices Vectors Computer graphics uses vector notation extensively because it is compact The basic idea is that a point with the coordinates (x, y , z) is written in the form x P = y z i.e., as a 3 × 1 vector /home/alien/environment/tex/ces(a “column vector”) Introduction Trig & triangles Transformations Vectors Matrices Transformation matrices Where there isn’t room to write a column vector, people write (say) P = (x, y , z)0 , the 0 denoting transpose You can think of this as being the same as a line drawn from the origin to (x, y , z)0 — in other words, a vector has a magnitude (the length of the line) and a direction Vectors (and matrices) are usually handwritten as underlined but printed as bold /home/alien/environment/tex/ces- Introduction Trig & triangles Transformations Vectors Matrices Transformation matrices Unit vectors The length of the vector P = (x, y , z)0 is simply q P = x 2 + y 2 + z 2. (Pythagoras’s theorem in 3D). A vector of unit length pointing in the same direction as P is x P̂ = P y P z P The ‘hat’ notation to identify unit vectors is widely used /home/alien/environment/tex/ces- Introduction Trig & triangles Transformations Vectors Matrices Transformation matrices Basis vectors The transformations we have looked at have comparatively simple forms only because the x- y - and z-axes are orthogonal (at right angles) to each other Any point in space can be represented by a combination of the basis vectors 1 0 0 x̂ = 0 ŷ = 1 ẑ = 0 0 0 1 It is possible to derive equivalents to all the above equations for non-orthogonal axes /home/alien/environment/tex/ces- Introduction Trig & triangles Transformations Vectors Matrices Transformation matrices Matrices Matrices are similar to, but more general than, vectors: the dimensions may take any value, one does not have to be unity For example, the following matrix is of dimension 2 × 3: a11 a12 a13 a21 a22 a23 As you’ll see, most matrices in 3D graphics are 4 × 4, where they are used to represent transformations /home/alien/environment/tex/ces- Introduction Trig & triangles Transformations Vectors Matrices Transformation matrices Adding Vectors and Matrices You can only add or subtract vectors and matrices if are the same size, e.g. both 3 × 1. The addition or subtraction is done element by element. For example x1 x2 x1 + x2 P1 = y1 ; P2 = y2 ; P1 + P2 = y1 + y2 z1 z2 z1 + z2 /home/alien/environment/tex/ces- Introduction Trig & triangles Transformations Vectors Matrices Transformation matrices Multiplying Matrices You can multiply matrices only if the second dimension of the first matrix is the same as the first dimension of the second one; so you can multiply a 1 × 3 and a 3 × 2 matrix but not two 3 × 1 ones. For example, if we have Q11 Q12 P11 P12 P13 P= Q = Q21 Q22 P21 P22 P23 Q31 Q32 then their product is the 2 × 2 matrix P11 Q11 + P12 Q21 + P13 Q31 P11 Q12 + P12 Q22 + P13 Q32 PQ = P21 Q11 + P22 Q21 + P23 Q31 P21 Q12 + P22 Q22 + P23 Q32 It is important to remember that matrix multiplication is not /home/alien/environment/tex/cescommutative: PQ 6= QP Introduction Trig & triangles Transformations Vectors Matrices Transformation matrices The scalar or ‘dot’ product If we have two vectors, P and Q, of lengths P and Q respectively, then we define P.Q = PQ cos θ where θ is the angle between P and Q This is known as the scalar, dot or inner product and is a number, not a vector or matrix It is also possible to show that we can calculate P.Q as Px Qx + Py Qy + Pz Qz The physical interpretation of the scalar product is the projection of P on Q or vice versa /home/alien/environment/tex/ces- Introduction Trig & triangles Transformations Vectors Matrices Transformation matrices The vector or ‘cross’ product The vector, cross or outer product of P and Q is written as P × Q or P ∧ Q It calculates a vector which is at right angles to both P and Q, something which is important when working out the shading of surfaces We calculate it using Py Qz − Pz Qy P × Q = Pz Qx − Px Qz Px Qy − Py Qx assuming a right-angled coordinate system /home/alien/environment/tex/ces- Introduction Trig & triangles Transformations Vectors Matrices Transformation matrices 2D Translation We can write our translation formulae x 0 = x + dx y 0 = y + dy in matrix notation as x0 y0 = x y + dx dy or as P0 = P + T where T represents the translation /home/alien/environment/tex/ces- Introduction Trig & triangles Transformations Vectors Matrices Transformation matrices 2D Scaling We can write scaling as 0 x Sx = y0 0 0 Sy x y or as P0 = SP where S is the matrix that represents the scaling /home/alien/environment/tex/ces- Introduction Trig & triangles Transformations Vectors Matrices Transformation matrices 2D Rotation We can write the equations x 0 = x cos θ − y sin θ y 0 = x sin θ + y cos θ in matrix form as 0 x cos θ − sin θ x = y0 sin θ cos θ y or as P0 = RP where R is the matrix that represents the rotation /home/alien/environment/tex/ces-