# 3D Mathmatics

# Vectors

## Definition

The mathmatical definition of a vector is a list of numbers or ‘array’. Vectors can be any dimension (except 1 since it would just be a scalar). For example a 3D vector would look like this:

We can refer to a element in a vector in 2 ways. We can use letters to refer to a number in a vector but this is limited to 4D vectors. When refering to a number as a letter the first element is **x** the second **y** followed by **z** and finally **w** for the 4th element. We can also just use integers to refer to elements in a vector. For example *v*2 would refer to the second element.

Now we know the mathmatical definition of vectors lets get to the intresting stuff: geometry. The geometric interpretation of vectors is a directed line segment that has *magnitude* and *direction*.

- The
*magnitude*of a vector is the length of the vector. A vector may have any nonnegative length. - The
*direction*of a vector describes which way the vector is facing.

Lets start by visualizing the vector.

You might wonder what the coordinates are of this vector. This question is however wrong because vectors only have *magnitude* and *direction*. We can think of it like movement. Lets say I tell you to take 4 steps forward. Forward is the *direction* and 4 is the *magnitude* of the *displacement* vector I told you to move about. The same principal works for *velocity* vectors (Move at a speed of 20 km/h backwards).

## Points

You may have noticed that some programmers use vectors as a manner of specifying positions which is fine to do as long as its understandable whatever you are using it for positioning or displacement. Some people say that vectors only should be used to specify displacement and points only relative position. This is because you can add two vectors, you can add a vector to a point but you cannot add two points together. We programmers however experience that programmes who follow these ethics are always longer and never faster. Whatever it makes code easier to read is subjective.

## Scalar Multiplication

We cannot add a vector to a scalar but we can multiply a vector by a scalar. The result is a vector that is parallel to the original vector, with different magnitude and possibly oposite direction. Vector times scalar multiplication is straightforward. We can just multiply each component of the vector by the scalar. For example lets multiply a 3D vector by a scalar (s):

With real world values it would look like this:

Now you know the formula it can ofton help to understand multiplication of vectors by visualizing it. Here are some examples:

\begin{tikzpicture} \draw[step=1cm,gray,very thin] (0,0) grid (4,4);

\node label=below:$\boldsymbol{v}$ at (0,0) {}; \node label=below:$\boldsymbol{v} 2$ at (1,0) {}; \node label=below:$\boldsymbol{v} 1.5$ at (2,0) {}; \node label=below:$-\boldsymbol{v}$ at (3,0) {};

\draw [->, blue, thick] (0,0) – (1,2); \draw [->, blue, thick] (1,0) – (2,4); \draw [->, blue, thick] (2,0) – (3.5, 3); \draw [->, blue, thick] (4,2) – (3, 0); \end{tikzpicture}

## Addition and Subtraction

Again this is really straightforward as long as they have the same dimensions. Unlike multiplication you can’t add a scalar to a vector. Here is the example of vector addition:

In the following example we have a red vector ($\boldsymbol{a}$) with the values $\begin{bmatrix} 4 \ 1 \end{bmatrix}$ and a green vector ($\boldsymbol{b}$) with the values $\begin{bmatrix} -2 \ 3 \end{bmatrix}$.

\begin{tikzpicture} \draw[step=1cm,gray,very thin] (0,0) grid (4,4);

\draw [->, red, thick] (0,0) – (4,1); \draw [->, green, thick] (4,1) – (2, 4); \draw [->, blue, thick] (0,0) – (2, 4); \end{tikzpicture}

If we position the vectors so that the head of $\boldsymbol{a}$ toutches the tail of $\boldsymbol{b}$ and then draw a vector (the blue vector in the previous example) from the tail of $\boldsymbol{a}$ to the head of $\boldsymbol{b}$ we get the resulting vector of $\boldsymbol{a} + \boldsymbol{b}$. This is known as the *triangle rule*. If you would do draw the addition of more than 2 vectors it would still work.

## Displacement Vector From One Point To Another

It is often that you will need to find the displacement vector between to points. This can be done by using the triangle rule and vector subtraction. To find the displacement vector from point *b* to *a* can be computed by substracting *a* from *b*.

To compute the displacement vector we interpret the points as vectors from the origin, and then use the triangle rule.

## Magnitude

The magnitude of a vector is also known as the “length” or “norm”. The magnitude is not stored directly in the vector (Meaning *v*x nor *v*y is the magnitude in a 2D vector). We will need to calculate the magnitude ourselfs.

The magnitude of a vector is the square root of the sum of the squares of the components of the vector. This sounds complicated but is infact very simple. I’ll demonstrate it with a 2D and a 3D vector:

## Unit Vectors

Sometimes you don’t want to bother with the magnitude of a vector and just want to use the direction. A vector with a magnitude of 1 is called a Unit Vector. This means multiplying a unit (directional) vector by a displacement vector won’t affect the speed of movement only the direction.

We can compute a unit vector from a non-unit vector. To normalize a vector we divide the vector by its magnitude. Here is a example of normalizing a 3D vector called **v** split into 2 steps for readability.

## Dot Product

I talked about multiplying a vector by a scalar previously in this paper but I never mentioned multiplying a vector by a vector. This is because you can multiply vectors with 2 methods: cross and dot. The cross product can for example be used to calculate the angle between 2 vectors.

The dot product is defined by a *.* in maths. To calculate the product of a vector is equal to the product of every corresponding component of the vectors added together. So here we go with another example:

## Cross Product

This is the other vector product. The cross product can only be applied in 3D sinec instead of the formula yielding a scalar like in the dot product the cross product will yield a 3D vector.

For the dot product we used a *.* to specify a dot product operation. For the cross product we will use *X*. The equation for the cross product is:

Geometrically the cross product yields a vector that is perpendicular to the original two vectors. The length of $\boldsymbol{a} \times \boldsymbol{b}$ is equal to the product of the magnitudes of **a** and **b** and the sine of the angle between **a** and **b**.

If **A** and **B** are parallel or if **a** or **b** is the zero vector, then . So the cross product interprets the zero vector as being parallel to every other vector. See how this is different from the dot product which interprets the zero vector as being perpendicular to every other vector.

# Matrices

## Dimensions and Notation

We define the size of a matrix by counting the amount of collumns and rows. We say that a matrix with r rows and c collumns is an $r \times c$ (Pronounced as r by c) matrix. So a $2 \times 3$ matrix has 2 collumns and 3 rows.

This is a $3 \times 2$ matrix demonstration one of the standard notiations for writing matrices. I will keep using this notation going forward and will represenent a matrix by a bold uppercase letter. for example: **M**. And I will reference positions of a matrix with the bold uppercase letter follewed by the row and the collumn in itallic. Like this: **M***12* (Read as M one two not twelve).

In this arary **M***12* referes to 7. This manner of indexing is different from arrays in programming languages. This is why its common for classes that store fixed sized matrices to give each element its own variable instead of using a 2D array like this: `m[3][4]`

. For example: `float m34`

which means the variable in row 3 collumn 4.

## Square Matrices

When a matrix has the same amound of rows as it has collumns we call it a square matrix. These matrices are particularly important in 3D Maths. The diagonal elements in a matrix are the elements with the same row and collumn (Highlighted in blue in matrix M). In a $3 \times 3$ matrix are **M***11* **M***22* and **M***33* the diagonal elements *not***M***13* and **M***31*

If all nondiagonal elements are zero as demonstrated in matrix **D** it means the matrix is a diagonal matrix. A special diagonal matrix is the identity matrix. A identity matrix is a square matrix with all diagonals elements defined as 1 and all nondiagonal elements as 0. For example a $3 \times 3$ matrix:

The basic idea is that if you multiply a matrix by a identity matrix you get the original matrix.

## Transposition

Transposition is the act of flipping a matrix diagonally. This means that the new matrix his collumns are formed from the rows of the original matrix. For example:

## Multiplying with a scalar

When multiplieing a matrix with a scaler the result is a matrix with the same dimensions. The multiplication is quite simple. Just multiply each element of the matrix by the scalar. Adding, substracting and dividing is very similair.

## Multiplying with a matrix

Multiplying by a matrix is a bit more complicated than multiplying with a sclalar. Lets say we want to do the following calculation:

Matrix by matrix multiplication only works if the number of collumns of **A** is equal to **B**’s number of rows. Our result will be a square matrix with the dimension of said number. To find the value of **R***11* we need to calculate the dot product of **A**’s first row and **B**’s first collumn.

**R***12* is the dot product of **A**’s first row and **B**’s second collumn.

Now you can probably guess what we need to do to calculate **R***21*. Get the cross product of the second collumn of **A** by the first collumn of **B**

And finally the value of **R***22* is the dot product of **A**’s second collumn and **B**’s second row.

Now we have calculated **R** a observation can be made: Order of multiplication matters greatly.