Vector and Matrix Algebra
Vector and Matrix Algebra
22b
The Ultimate 3D Coding Tutorial (C) Ica /Hubris 1996,1997,1998
Over 150k of pure sh...er, 3d coding power !
1.1.1 General
Vectors are lines which have a constant direction and length (or
magnitude), and can be moved arbitrarily in a given coordinate system. A vector
is drawn as in the following picture (a short perpendicular line (not necessary) at
the starting point, and an arrowhead at the ending point) :
Vectors are usually marked the same way as the coordinates of a point :
a=(x1,y1,z1), where x1 is the x-axis vector coefficient, y1 and z1 respectively.
This vector a can be drawn so that we draw a point into the coordinates
(x1,y1,z1), and a line from the origin to that point.
The x-, y-, and z-axis unit vectors (vectors that have a length of one) have
their own special symbols : i, j, and k, respectively. So the x-axis unit vector is
i=(1,0,0), etc.
Vectors are marked as alphabetical symbols with a line over them, or the
symbol being written with a bold font. I’m naturally using here the latter way.
The length of the vector AB is marked as the vector’s absolute value and
calculated by adding the squares of its terms together and taking a square root of
the result :
This is the same equation as the distance between the point (X,Y,Z) and
the origin.
a + b = (Ax+Bx,Ay+By,Az+Bz).
Solution:
2a = (2*2,2*(-6)) = (4,-12).
2a-b = (4-1,-12-3) = (3,-15).
|2a-b| = sqrt( 3^2 + (-15)^2 ) = sqrt(9+225) = sqrt(234).
1.1.5 Dot product
One vector can be multiplied by another by two ways, and the first one is
called the dot product (or scalar product if you wish). A dot product is read like
"a dot b" and it is calculated by multiplying the length of a by the length of b
and by the cosine of the angle between the two vectors. Another way to
calculate it is to multiply the terms of the two vectors together and to add the
products together. The geometric meaning of dot product is the length of a
projected to b. Note ! The angle between the two vectors means always the
smaller one between them when the two vectors start from the same point.
Dot product is needed for example in phong illuminating and many other
light manipulation operations.
Problem: Calculate the angle between the vectors a+b and a-b, where a =
(3,1) and b = (1,-7).
Solution:
a+b = (3+1 , 1-7) = (4,-6)
a-b = (3-1 , 1+7) = (2,8)
(a+b) dot (a-b) = |a+b| * |a-b| * cos(a+b,a-b), on the other hand
(a+b) dot (a-b) = 4*2 + (-6)*8 = 8-48 = -40.
|a+b| = sqrt( 4^2 + (-6)^2 ) = sqrt(16+36) = sqrt(52),
|a-b| = sqrt( 2^2 + 8^2 ) = sqrt(4+64) = sqrt(68). From the equation
sqrt(52) * sqrt(68) * cos(a+b,a-b) = -40 we get
cos(a+b,a-b) = -40 / sqrt(52*68).
The angle is thus 132.3 degrees.
1.1.6 Vector projection
To clarify, all the vectors in the picture start from the same point, and the
projection vector of a on b is the short vector which is parallel to b.
In the 3-space, we define the cross product (or vector product) axb ("a
cross b") of two vectors a and b as a vector which is perpendicular to each of the
vectors a and b and whose length |a x b| tells the area of the parallelogram
defined by the two vectors :
In English, we pick each of the i, j, and k, hide the column and row going
through it, create a two-row determinant from the remaining part of the table,
and solve it normally. Note ! Every second sign is a +, every second a - !
REMEMBER this ! (the signs go like +-+-+...)
Cross product is needed for example when calculating normals and plane
equations.
Problem: Find the area of the parallelogram determined by the vectors (1,-1,2)
and (2,3,-1).
A scalar triple product is marked as (axb) dot c, and its absolute value
tells the volume of the parallelogramic object determined by the three vectors
(cf. parallelogram in cross product). Note ! If this volume is zero, the three
vectors are of course on the same plane !
Problem: Find a constant a so that the vectors (1,-1,2), (1,3,-1), and (a,-4,1)
are on the same plane.
Solution: On the same plane -> scalar triple product = 0. We first calculate the
cross product of arbitrary two of the vectors and then the
dot product of this vector and the third vector.
This ought to be zero, -5a - 15 = 0 <=> -5a = 15 <=> a = -3.
1.2 Matrices
1.2.1 General
So, matrices are marked as a table of numbers in great brackets (or round
brackets). Determinants are different from matrices in that they have only
absolute value symbol -like vertical lines around the table (see 1.1.6).
The numbers a(y,x) are matrix elements. They form p rows and n
columns. The type of the matrix is p x n; if needed, it can also be written
together with the matrix symbol: A(pxn). The first index j of a matrix element
a(j,k) shows the row on which the element is located; the latter index k shows
respectively the column (the indices go thus like in C’s tables: compared to the
representation of a point (x,y), in matrices and C language tables the indices are
(y,x)).
If the amounts of rows and columns in a matrix are equal, we call the
matrix a square matrix (cf. 3x3 or 4x4 matrices in 3D operations). Matrix
elements whose indices are equal form the diagonal of a square matrix; in other
words this is the line from the upper left corner to the lower right corner of a
matrix. If all the other elements than the ones on the diagonal are equal to zero,
we call the matrix a diagonal matrix :
A matrix whose all elements are zero, is called a zero matrix. The type of
a zero matrix is not restricted. The symbol is O (or o if the matrix is a vector).
A matrix is called orthogonal if its row or column vectors (that is, the
vectors formed by the rows or columns of the matrix) are perpendicular to each
other and they are unit vectors.
1.2.2.1 Addition
(A scalar = a constant) This goes like addition, but instead of the +’s we
put *’s, and instead of b(j,k) we put the scalar with which we want to multiply
the matrix. Multiplying by a scalar scales a matrix.
1.2.2.3 Multiplication
Xi+Yj+Zk = [ X Y Z ].
1.2.2.4 Transposition
C(nxp) = At,