CSC 47 Introduction to Computer Graphics Mathematical Foundations Part 2
Vector Magnitude and Unit Vectors The magnitude (length, size) of n-vector w is written w 2 2 2 w = w + w2 + + w n Example: the magnitude of w = (4, -2) is 2 and that of w = (, -3, 2) is 4 A unit vector has magnitude v = The unit vector pointing in the same direction as vector a is a (if a ) a ˆ = a Converting a to â is called normalizing vector a
Dot Product of Two Vectors The dot product of n-vectors n v and w is v w = v w + v 2 w 2 + + v n w n The dot product is commutative: v w = w v The dot product is distributive: (a( ± b) c = a c ± b c The dot product is associative over multiplication by a scalar: (sa) b( = s(a b) The dot product of a vector with itself is its magnitude squared: b b = b 2
Standard Unit Vectors The standard unit vectors in 3D are i = (,,), j = (,, ), and k = (,, ) k always points in the positive z direction In 2D, i = (,) and j = (, ) The unit vectors are orthogonal
Application of Projection: Reflections A reflection occurs when light hits a shiny surface (below) or when a billiard ball hits the wall edge of a table
Reflections cont d When light reflects from a mirror, the angle of reflection must equal the angle of incidence: θ = θ 2 Vectors and projections allow us to compute the new direction r,, in either two-dimensions or three dimensions a) n a r θ θ2 b) n a r -m m L e e
Vector Cross Product (3D Vectors Only) a x b = (a( y b z a z b y )i + (a z b x a x b z )j + (a x b y a y b x )k The determinant below also gives the result: a b = i j k a x a y a z b x b y b z
Properties of the Cross-Product i x j = k; j x k = i; k x i = j a x b = - b x a; a x (b( ± c) ) = a x b ± a x c; ; (sa)( ) x b = s(a x b) a x (b( x c) (a x b) ) x c for example, a = (a x, a y, ), b = (b( x, b y, ), c = (,, c z ) c = a x b is perpendicular to a and to b The direction of c is given by a right/left hand rule in a right/left-handed handed coordinate system
Properties cont d a (a x b) ) = a x b = a b sin θ,, where θ is the smaller angle between a and b a x b is also the area of the parallelogram formed by a and b a x b = if a and b point in the same or opposite directions, or if one or both has length
Geometric Interpretation of the Cross Product
Application: Finding the Normal to a Plane Given any 3 non-collinear points A, B, and C in a plane, we can find a normal to the plane: a = B A, b = C A, n = a x b The normal on the other side of the plane is n
Changing to and from Homogeneous Coordinates To: if the object is a vector, add a as the 4 th coordinate; if it is a point, add a From: simply remove the 4 th coordinate OpenGL uses 4D homogeneous coordinates for all its vertices If you send it a 3-tuple 3 in the form (x,( y, z), it converts it immediately to (x,( y, z,, ) If you send it a 2D point (x,( y), it first appends a for the z-component z and then a, to form (x,( y,,, ) All computations are done within OpenGL in 4D homogeneous coordinates
Combinations Linear combinations of vectors and points: The difference of 2 points is a vector: the fourth component is = The sum of a point and a vector is a point: the fourth component is + = The sum of 2 vectors is a vector: + = A vector multiplied by a scalar is still a vector: a x = Linear combinations of vectors are vectors
Point + Vector Suppose we add a point A and a vector that has been scaled by a factor t: the result is a point, P = A + tv Now suppose v = B A, the difference of 2 points: P = tb + (-t)a, an affine combination of points
Representing Planes: Point-Normal Form Point-normal normal form: n (P B) = ; where B is a given point on the plane, and P = (x, y, z, ) T
Finding A Circle through 3 Points We want to find the center and the radius of the circle The 3 points make a triangle, and the center S is where the perpendicular bisectors of two of the sides of the triangle meet The radius is r = A S
Clipping lines Clipping algorithm that clips off outlying parts of each line segment presented to it
Clipping a Polygon To clip a polygon against a window means to find which part of the polygon is inside the window and thus will be drawn
Clipping cont d The method we will use is called Cohen- Sutherland clipping There are 2 trivial cases: a line AB totally inside the window, which we draw all of, and a line CD totally outside the window, which we do not draw at all
Clipping polygons
Clip4 Clip5
Vectors and Matrices Matrix is an array of numbers with dimensions M (rows) by N (columns) 3 by 6 matrix element 2,3 is (3) Vector can be considered a x M matrix v = ( x y z ) 3 5 2 3 2 4 2
Types of Matrix Identity matrices - I Diagonal 2 4 Symmetric a b b d c e c e f Diagonal matrices are (of course) symmetric Identity matrices are (of course) diagonal
Operation on Matrices Operation on Matrices Addition Addition Done elementwise Done elementwise Transpose Transpose Flip (M by N becomes N by M) Flip (M by N becomes N by M) = 3 8 9 7 2 4 6 5 3 7 6 8 2 5 9 4 T
Operations on Matrices Multiplication Only possible to multiply of dimensions x by y and x 2 by y 2 iff y = x 2 resulting matrix is x by y 2 eg Matrix A is 2 by 3 and Matrix by 3 by 4 resulting matrix is 2 by 4 Just because A x B is possible doesn t mean B x A is possible!
Matrix Multiplication Order Matrix Multiplication Order A is n by k, B is k by m A is n by k, B is k by m C = A x B defined by C = A x B defined by BxA BxA not necessarily equal not necessarily equal to to AxB AxB = = k l lj il ij b a c = = =
Example Multiplications Example Multiplications = 2 3 2 = 3 3 2 2
Inverse Matrix If A x B = I and B x A = I then A = B - and B = A -
Vectors and Matrices Matrix is an array of numbers with dimensions M (rows) by N (columns) 3 by 6 matrix element 2,3 is (3) Vector can be considered a x M matrix v = ( x y z ) 3 5 2 3 2 4 2
Types of Matrix Identity matrices - I Diagonal 2 4 Symmetric a b b d c e c e f Diagonal matrices are (of course) symmetric Identity matrices are (of course) diagonal
Operation on Matrices Operation on Matrices Addition Addition Done elementwise Done elementwise Transpose Transpose Flip (M by N becomes N by M) Flip (M by N becomes N by M) = 3 8 9 7 2 4 6 5 3 7 6 8 2 5 9 4 T
Operations on Matrices Multiplication Only possible to multiply of dimensions x by y and x 2 by y 2 iff y = x 2 resulting matrix is x by y 2 eg Matrix A is 2 by 3 and Matrix by 3 by 4 resulting matrix is 2 by 4 Just because A x B is possible doesn t mean B x A is possible!
Matrix Multiplication Order Matrix Multiplication Order A is n by k, B is k by m A is n by k, B is k by m C = A x B defined by C = A x B defined by BxA BxA not necessarily equal not necessarily equal to to AxB AxB = = k l lj il ij b a c = = =
Example Multiplications Example Multiplications = 2 3 2 = 3 3 2 2
Inverse Matrix If A x B = I and B x A = I then A = B - and B = A -