Welcome to the second lecture of this online sequence. This is a review of the basic math you need for computer graphics and in particular, this first segment deals with vectors and dot products. Let me first say a few words about the next steps for the course.
First, you should complete homework 0. And being able to compile homework 0 will ensure you have your compilation set up, OpenGL. Second, by actually submitting homework 0, you will ensure that you are able to use our feedback and grading servers properly, you are able to generate the images that they require.
The first few lectures in this course talk about some of the core mathematical and technical ideas in computer graphics. They're some of the most beautiful ideas in computer graphics. Homework 1 has only a few lines of code, so it's relatively simple compared to homeworks 2 and 3. But I should mention that you should still start early on homework 1, because it is the first homework, and moreover, even though it only has a few lines of code, there is quite some thinking to be done.
The idea of homework 1 is to use some of the ideas in the first few lectures to generate your first images.
As far as textbooks for the course, there is none required. This course is intended to be free and available to anyone. If you are, however, interested in getting a reference, then a reference for OpenGL and the GL shading language are useful. The standard red book for OpenGL, and the orange book for the GL shading language. But, I want to emphasize that there are no required textbooks and there's nothing that you need to purchase for this course.
We start with the overall motivation of this section.
Many computer graphics concepts need basic mathematics like linear algebra. We are interested in vectors, dot products, cross products. We are interested in matrices, matrix-vector and matrix-matrix multiplication. How does this come up? Assume for example that you have a point that you want to translate to a different region. This comes up all the time, you have a character, you want to move it somewhere else in the scene. You can regard the point as a vector and we'll see how an operation like translation, rotation can be written as a matrix-vector mulitiplication.
The contents of this lecture on vectors and matrices should be very basic for most of you. It does not go beyond what you would learn in a basic high school mathematical education. We'll start by talking about vectors. Vectors are usually written with the vector symbol, so this is the common vector symbol, an arrow on top. Or, if you look at the textbook, they are often written in bold. The magnitude is written in the standard way by putting a norm symbol around the vector. Vectors have a length and they have a direction.
The absolute position of a vector is not important. And so these 2 vectors are actually the same. So this vector moves here and I just translated the vector here, but they are actually the same vector. Vectors are used to store offsets, displacements and locations. But strictly speaking, there is a minor technical difference, that a position is not strictly speaking a vector and in fact you cannot add positions as vectors, because to have a positions such as 2 units on the X axis, we implicitly require an origin. Whereas, a vector is supposed to be independent of the origin, can be translated with respect to itself. Nevertheless, it is very convenient to also represent positions as vectors, and to multiply them as matrices to do various transformations.
The first vector operation we are going to consider is vector addition. And so you have 2 vectors a and b, and you want to find what is the vector, a + b. This can be done geometrically with what is known as, the parallelogram rule. So, here you have the vector a. I take the vector b and I translate it, so that the tail of vector b is aligned with the head of vector a.
And I complete the parallelogram and the diagonal of the parallelogram is the addition. If you don't want to think in terms of parallelograms, just think about it in terms of a triangle. So, I am considering the vector a + b from the tail of a to the head of b.
Vector addition is commutative, so a + b is equal to b + a and if you are not very much into parallelograms, then we can consider it as simply adding coordinates in Cartesian components.
So let's consider Cartesian coordinates, your familiar X and Y coordinates. I've shown the X axis and the Y axis. The vector I've shown here can be moved to the origin of the Cartesian coordinate system.
And you can see in this case, it has 4 units along the X axis and 3 units along the Y axis. So the vector can be written as 4 times the unit X vector plus 3 times unit Y vector. X and Y, in this case are the coordinates of the axis. But, in general they can be any orthogonal unit vector. So, I write the vector A as the components X and Y.
It's also interesting, sometimes, to write the transpose of A. So I'm writing a vector as a column vector. It transposes a row vector, x and y. And the norm of A is going to be square root of x^2 + y^2. Which, in this case, is the square root of 4^2 + 3^2 which will be equal to 5. That's just the Pythagorean relationship. It's important to note that x and y can be any unit vectors.
Vectors can also be multiplied and in fact there are 2 multiplications of vector, the dot or the scalar product and the cross or the vector product. Note that in this lecture we will use right-handed coordinates, which is a standard coordinate system found in most mathematics textbooks.
What do right-handed coordinates mean? If you take your right hand and your X axis is pointed here, your Y axis is pointed here. If you take your hand and you curl them around the X and Y axes, the thumb on the right hand will point towards the positive Z axis. With left-handed coordinates it would of course be the opposite and so we assume right-handed coordinate system with obvious due apologies to those of you who are left-handed.
First, let's consider the dot or the scalar product and that is written as a dot b. It's commutative. It's also equal to b dot a. We want to consider the norm of a, so of course the product involves the product of the two vectors a and b, so we do want to consider this norm of a.
And now of course it is symmetric, so we will also want to consider the norm of vector b. How does it depend on the angle between a and b? That's the crucial question. And its intended to be the product of their magnitudes when the vectors are aligned. But, when they are not aligned, we will have to multiply it, by the cosine of the angle between them.
My handwriting is not the best and throughout this course when you need an actual equation, I will put it on the slide as well. As we see here, a dot b is the norm of a times norm of b times the cosine of the angle between them. From that equation, you can calculate the angle between two vectors, as the inverse cosine of a dot b divided by each of their individual magnitudes.
This is in fact is one of the most common uses of the dot product, because the dot product can be calculated very efficiently in Cartesian components, but the angle between two vectors not that easily. So, what you do is, you take the dot product, and then by taking the inverse cosine, you get the angle between the 2 vectors. The dot or scalar products has the associativity and the multiplication properties. So a dot (b + c) is equal to a dot b + a dot c.
And if you multiply a scalar through any of the vectors k dot b or uh, a dot kb that just multiplies the dot product by a scalar.
The question now is, what is the dot product in Cartesian components? And this is something we can derive. So I write the dot product as x_a. And now, this is in a particular direction. So it's in the direction of the X vector. And the next coordinate is y_a, which is in the direction of the Y vector.
Okay, so I am going to take dot product of this with the same thing for b and b will be written as x_b times the X vector plus y_b times the Y vector.
If we now consider and expand these things out, we have the following. So we have x_a * X times x_b * X and I can write that. I can also include the y_a * y_b term and that will multiply Y dot Y. Finally, I can include the X dot Y terms. X dot Y terms and so I can write plus X dot Y. And that will be multiplied by x_a * y_b + x_b * y_a. But what you can immediately realize is that, because the cosine of the angle between X and Y is 0, the angles X and Y are away by 90 degrees. Cosine of 90 degrees is 0. So this quantity is actually equal to 0. And therefore, we only care about this term here.
And, of course, if you could not quite follow my handwriting, I, again, have the equation on the next slide. Where a dot b is equal to the dot product of [x_a, y_a] and [x_b, y_b]. And this is simply equal to x_a * x_b plus y_a * y_b. Very Simple.
Let's look at some applications in computer graphics. The obvious one is to find the angle between two vectors, I already talked about it. The cosine of the angle between the light source and the surface is very important for shading. It's also important to find the projection of one vector on another. Example, if we want a co-ordinates of a point in new co-ordinate system. And the advantage of the dot product is that it is easily computed in Cartesian coordinates.
So let's now as a final step look at the projection of vector b on a. So very often you want to know what fraction of the vector b lies on a.
And this can be done easily with the dot product. Essentially it's equal to the norm of the vector b, times the cosine. So we can say that the projection of the vector b on a is equal to the norm of the vector b times the cosine of the angle between them. Because that's the definition, the component of b along vector a is given by the cosine. And this is simply a dot product of the vector a and the vector b, which is what the cosine is, divided by the norm of a and the norm of b.
Now the norm of b cancels and what you are left with is the dot product of a and b divided by the magnitude of the vector a. And so the question is what is the component of b along a given by? We will have to multiply by the unit vector along the a direction. We already have the length in the first formula but now you need to multiply by this term. So this term here is the unit vector. So it is the unit vector which has a length of 1. And you multiply by that.
Now, b projection on a is a dot b / norm of a, multiplying by the vector a, and dividing by norm of a, makes norm of a squared. And so what you are left with, is the projection of b on the vector a, is this quantity, which involves the dot product between a and b. And the projection is of course, along the direction of the vector a, which is why the final result involves the vector a.