# Vectors

### 1. What is a vector?

When you want to learn 3D programming you should always start with geometry math. Why? Because sooner or later you ‘ll have to use it in your games and applications. Think like this, you can’t eat without  your mouth, you can’t play guitar without guitar (I’m also a guitar player :D), you can’t do 3D programming without math.

Now, the first thing we should know are Vectors. What are Vectors and why we need them?

Well you can use vectors to represent forces, directions, velocities,normals etc. So they are used in many scientific fields.  Ok, but why we need them in 3D graphics? Maybe I want to show the direction I want to go in a First Person Shoter(FPS) or I want to set the direction of  a light or polygon normals and so on.

A vector has two components :   Magnitude(or length) and   Direction

We can represent vectors like this :

We can say that for vector AB , A is the tail and B is the head of the vector. If tail coincides with the oringin then we say that the vector is in standard position. The origin is set in O(0,0) for 2D coords or in O(0,0,0) for 3D coord.In the image above , W is in standard position.

### 2.Equality of vectors

Two or more vectors are equal if and only if they have the same magnitude and same direction. So the vectors in the next figure are equal:

If you overlap w over u,you will not going to see any more they will be indistinguishable.

### 3.Basic Vector operations

Ok , so there are 3 simple operations we need to know between vectors. There are other operations that we need to know but we will see them later or in another tutorial very soon.

First let me define 2 vectors in 3D space, that means we have  vector $w=(w_x,w_y,w_z)$ and  $u=(u_x,u_y,u_z)$, of course we could define this 2 vectors in 2D space without z component.

1.Addition is very simple we just add their components  $w+u=(w_x+u_x,w_y+u_y,w_z+u_z)$ and get another vector much longer. For example in Physics if you add 2 forces(same direction) you get a much stronger force(called net force). Of course $w+u = u+w$

2.Multiplication with a scalar. If m is our scalar then $mw=(mw_x,mw_y,mw_z)$.

3. Subtraction is like addition $w-u=(w_x-u_x,w_y-u_y,w_z-u_z)$. You can think subtraction like a  addition and a -1 scalar multiplication $w-u=w + (-1)u$

You can represent subtraction geometrically with parallelogram method or head to tail method.  You should know that you can translate a vector everywhere you want and you also need to know how to find the opposite direction vector. In the images below you ll see the parallelogram method , how to translate and find opposite direction of a vector. Just follow the steps, it should be easy.

Step 1:  Find the opposite direction

Step2:  Make the addition between vectors u and -w and get the new vector

Step3:   Translate  the new vector in the initial position

Let’s see another example :

### 4. Left Handed and Right Handed Coordinate Systems

We should know in which coordinate system we are going to work .For example in DirectX uses Left Handed coordinate system but OpenGL uses Right Handed coordinate system. The whole idea is to find out the positive z-axis and obviously the negative z-axis.

To find this we are going to use our hands. Take your left hand place it along +x-axis then lift your fingers on +y-axis,you should see your palm by now :).  Make a 90°  angle between your palm and your thumb. Now the thumb will show you the +z-axis. For the right hand is the same procedure but the trick is to put your right hand to the right of your body,just like the left hand, on +x-axis, don t make the mistake and put it on the left of your body because it will point on – x-axis.

In DirectX +z-axis will goes into your  computer screen, -z -axis will be towards you. In OpenGL/WebGL, +z-axis will point to you and -z-axis will go into your computer screen.

### 5. Magnitude

The magnitude is the length of a vector. The standard notation is between 2 bars $||u||$ but you can also  find it written with single bar $|u|$.

The magnitude of a 2D vector is computed with Pythagorean thorem, therefore we get a single value.

The magnitude of a 3D vector is computed also with Pythagorean thorem but we have to apply it twice to get the answer.

We will use magnitude to normalize the vector.

### 6. Dot Product

Dot Product is a 2 vector multiplication that returns a single value. The standard notation is ” $\cdot$ “and you can also find it under the name of scalar product.

We got 2 formulae: the algebric formula and geometric formula .

Algebric formula:

So for  2 3D vectors we will have :$w\cdot u= w_x u_x+w_y u_y+ w_z u_z$. Pretty simple.

Geometric formula:

We can write Dot Product geometrically using the angle between the two vectors.

$u \cdot v = || u || ||v|| cos \theta$

There are some properties that we need to know before we go on:

1)  if $\theta = 90 , cos \theta=0$, $u , v$ are  orthogonal so  $u \cdot v = || u || ||v|| cos \theta = 0$

2) if  $u = v$ then $\theta =0 ,cos \theta=1$  so  $u \cdot v = || u || ||v|| cos \theta =u \cdot u$

3)  $u \cdot u = u_x u_x+u_y u_y+ u_z u_z = ||u||^{ 2}$, magnitude squared.

4)  $w \cdot( u + v) = w \cdot u + w \cdot v$.

Let’s demonstrate  $u \cdot v = || u || ||v|| cos \theta$. First we need triangle  build from vectors, to see  the Low of Cosines and properties from above.

If we write Low of Cosines:  $|| v-u ||^{2}= ||v||^{2}+ ||u||^{2}-2*||u||||v||cos \theta$

then we move first 2 right  terms to the left of the equation:

$|| v-u ||^{2}-||v||^{2}- ||u||^{2}= -2*||u||||v||cos \theta$

Now we can write $|| v-u ||^{2}$ as $(v-u) \cdot (v-u)$,

$(v-u) \cdot (v-u) = v \cdot v + u \cdot u -2*( v \cdot u)$

$v \cdot v + u \cdot u -2*( v \cdot u) = ||v||^{2}+ ||u||^{2}-2*( v \cdot u)$

Now we go in second eqution and substitute $|| v-u ||^{2}$ with the above result

$||v||^{2}+ ||u||^{2}-2*( v \cdot u)-||v||^{2}- ||u||^{2}= -2*||u||||v||cos \theta$

We can see terms reduction and , -2 goes away as well and voilà 🙂

$( v \cdot u)= ||u||||v||cos \theta$.

We will demonstrate in another tutorial that algebric definition and geometry definition are equivalent but now lets see the cross product.

### 7. Cross Product

Cross product is another form of multiplication. The result is a vector this time, unlike dot product which the result is a scalar.The standard notation is ” x “

Given two 3D vectors $u , v$ ,making the cross product will result vector $w$ which is orthogonal on $u$ and orthogonal on $v$. Remember that cross product works only for 3D vectors.

The tail of the vector $w$ is in the same point with the $u,v$ vectors and the direction of $w$ is given by the Right Hand Thumb Rule or Left Hand Thumb Rule.

Always keep the smallest angle $\theta$ between the vectors to find the cross product. $0\leq\theta\geq\pi$. The index finger will always point to the first vector from cross product.

The formula is $u\phantom xx\phantom vv = ||u||||v||sin \theta n$ where $n$ is the unit vector and points out the direction.

Or you can compute like this:

$w=u\phantom xx\phantom vv=(u_y v_z-u_z v_y,u_z v_x-u_x v_z,u_x v_y-u_yv_x)$

The above formula is computed using Sarrus rule on the determinant below where $i, j, k$ are unit vectors

I will explain Sarrus rule or Cofactor rule in another post for determinants

Here are some Cross Product properties:

1) anticommuative  $u\phantom xx\phantom vv =-v\phantom xx\phantom uu$

2) distributive  $u\phantom xx\phantom v(v+w) =( u\phantom xx\phantom vv) + ( u\phantom xx\phantom ww)$

3) scalar multiplication  $(m u)\phantom xx\phantom vv =m( u\phantom xx\phantom vv)$