# Vector operations

## Definition

Vectors are quantities that are defined by two things:

• their magnitude, and
• their direction.

A couple of examples of vectors are velocity and force.
In 2 dimensions, a Vector is described by two components:

• a horizontal component, x component
• a vertical component, y component

### Properties

All perpendicular vectors are orthogonal but not vice versa
A zero vector is orthogonal to anything including itself
The (non zero) vectors are collinear if one of them is scalar multiple of the other (same Vector of different length), the angle between collinear vectors is zero

### ijk vector representation

as a sum of its components multiplied by the corresponding unit vectors $\stackrel{\to }{i},\phantom{\rule{0.167em}{0ex}}\stackrel{\to }{k},\phantom{\rule{0.167em}{0ex}}\stackrel{\to }{k}$

$\stackrel{\to }{i}=\left(\begin{array}{c}1\\ 0\\ 0\end{array}\right),\phantom{\rule{0.278em}{0ex}}\stackrel{\to }{j}=\left(\begin{array}{c}0\\ 1\\ 0\end{array}\right),\phantom{\rule{0.278em}{0ex}}\stackrel{\to }{k}=\left(\begin{array}{c}0\\ 0\\ 1\end{array}\right)$$\stackrel{\to }{a}={a}_{1}\stackrel{\to }{i}+{a}_{2}\stackrel{\to }{j}+{a}_{3}\stackrel{\to }{k}$

### Vector length

Vector length is equal to the square root of the sums of its squared coordinates

$‖\stackrel{\to }{a}‖=\sqrt{{a}_{1}^{2}+{a}_{2}^{2}+\dots +{a}_{n}^{2}}$

and also follows that $$\color{orange}\lVert\vec a\rVert^2=\vec a\cdot\vec a$$

## Vector operations

$\stackrel{\to }{c}=\stackrel{\to }{a}+\stackrel{\to }{b}=\left(\begin{array}{c}{a}_{1}+{b}_{2}\\ {a}_{2}+{b}_{2}\\ \dots \\ {a}_{n}+{b}_{n}\end{array}\right)$

### Vector subtraction

$\stackrel{\to }{c}=\stackrel{\to }{a}-\stackrel{\to }{b}=\stackrel{\to }{a}+\left(-\stackrel{\to }{b}\right)=\left(\begin{array}{c}{a}_{1}-{b}_{2}\\ {a}_{2}-{b}_{2}\\ \dots \\ {a}_{n}-{b}_{n}\end{array}\right)$

The resulting vector $\stackrel{\to }{c}$ 's head is at the vector $\stackrel{\to }{a}$ 's head

### Vector to scalar multiplication (vector result)

$\stackrel{\to }{c}=b\cdot \stackrel{\to }{a}\left(\begin{array}{c}b{a}_{1}\\ b{a}_{2}\\ \dots \\ b{a}_{n}\end{array}\right)$

### Dot product (scalar product)

Defined for any dimensionality

$\stackrel{\to }{c}=\stackrel{\to }{a}\cdot \stackrel{\to }{b}=\left(\begin{array}{c}{a}_{1}\\ {a}_{2}\\ \dots \\ {a}_{n}\end{array}\right)\cdot \left(\begin{array}{c}{b}_{1}\\ {b}_{2}\\ \dots \\ {b}_{n}\end{array}\right)={a}_{1}{b}_{1}+{a}_{2}{b}_{2}+\dots +{a}_{n}{b}_{n}$

#### properties

• commutative property: $\stackrel{\to }{a}\cdot \stackrel{\to }{b}=\stackrel{\to }{b}\cdot \stackrel{\to }{a}$
• distributive property: $\left(\stackrel{\to }{a}+\stackrel{\to }{b}\right)\cdot \stackrel{\to }{c}=\stackrel{\to }{a}\cdot \stackrel{\to }{c}+\stackrel{\to }{b}\cdot \stackrel{\to }{c}$
• associative property: $\left(c\cdot \stackrel{\to }{a}\right)\cdot \stackrel{\to }{b}=c\cdot \left(\stackrel{\to }{a}\cdot \stackrel{\to }{b}\right)$
• $\stackrel{\to }{a}\cdot \stackrel{\to }{a}=‖\stackrel{\to }{a}{‖}^{2}$
• $\stackrel{\to }{a}$ and $\stackrel{\to }{b}$ are orthogonal (and perpendicular if not zero) when the dot product $\stackrel{\to }{a}\cdot \stackrel{\to }{b}=0$
• law of cosine: $\stackrel{\to }{a}\cdot \stackrel{\to }{b}=‖\stackrel{\to }{a}‖\cdot ‖\stackrel{\to }{b}‖\cdot \mathrm{cos}\left(\varphi \right)$ where $\varphi$ is the angle between $\stackrel{\to }{a}$ and $\stackrel{\to }{b}$
• in geometric sense the dot product gives how much of the vectors are going in the same direction, or a projection (shadow) of one vector on another

### Cross product (vector product)

Defined only for 3D space, order dependent

$\stackrel{\to }{c}=\stackrel{\to }{a}×\stackrel{\to }{b}=\left(\begin{array}{c}{a}_{2}{b}_{3}-{a}_{3}{b}_{2}\\ {a}_{3}{b}_{1}-{a}_{1}{b}_{3}\\ {a}_{1}{b}_{2}-{a}_{2}{b}_{2}\end{array}\right)$

#### properties

• $‖\stackrel{\to }{a}×\stackrel{\to }{b}‖=‖\stackrel{\to }{a}‖\cdot ‖\stackrel{\to }{b}‖\cdot \mathrm{cos}\left(\varphi \right)$, where $\varphi$ is the angle between the vectors
• triple product: $\stackrel{\to }{a}×\left(\stackrel{\to }{b}×\stackrel{\to }{c}\right)=\stackrel{\to }{b}\cdot \left(\stackrel{\to }{a}\cdot \stackrel{\to }{c}\right)-\stackrel{\to }{c}\cdot \left(\stackrel{\to }{a}\cdot \stackrel{\to }{b}\right)$
• resulting $\stackrel{\to }{c}$ is orthogonal to $\stackrel{\to }{a}$ and $\stackrel{\to }{b}$ (and perpendicular, if is not zero vectors)
• #right_hand_rule: $\stackrel{\to }{a}$ is the index finger, $\stackrel{\to }{b}$ is the middle finger, the thumb is the resulting $\stackrel{\to }{c}$

#### cross product to calculate the area of a

• parallelogram
Given the parallelogram with sides of $\stackrel{\to }{a}$ and $\stackrel{\to }{b}$, its area is equal to the magnitude of the cross product:
$S=‖\stackrel{\to }{a}×\stackrel{\to }{b}‖$
• triangle
Given a triangle whose side are defined by $\stackrel{\to }{a}$ and $\stackrel{\to }{b}$, its are is given by:
$S=\frac{‖\stackrel{\to }{a}×\stackrel{\to }{b}‖}{2}$

The triangle's area is half of the parallelogram's area (see above)

### Angle between vectors

Given that $\stackrel{\to }{a}$ and $\stackrel{\to }{b}$ are not zero

• if $|\stackrel{\to }{a}\cdot \stackrel{\to }{b}|=‖\stackrel{\to }{a}‖\cdot ‖\stackrel{\to }{b}‖$ then $\stackrel{\to }{a}$ and $\stackrel{\to }{b}$ are collinear (if non zero vectors), the angle between them is zero, i.e. $\stackrel{\to }{a}=c\cdot \stackrel{\to }{b}$
• law of cosine:
$\stackrel{\to }{a}\cdot \stackrel{\to }{b}=‖\stackrel{\to }{a}‖\cdot ‖\stackrel{\to }{b}‖\cdot \mathrm{cos}\left(\varphi \right)$

where $\varphi$ is the angle between the $\stackrel{\to }{a}$ and $\stackrel{\to }{b}$, then (?to check?):

$\varphi =\mathrm{arccos}\left(\frac{\stackrel{\to }{a}\cdot \stackrel{\to }{b}}{‖\stackrel{\to }{a}‖\cdot ‖\stackrel{\to }{b}‖}\right)$

this means that if the vectors are collinear the fraction is = 1 and the arccos(1) = 0 - zero angle between the vectors

### A line crossing 2 points

Given 2 position vectors, and parametric $t\in R$ the line $L$ is given by:

$L=\stackrel{\to }{a}+t\left(\stackrel{\to }{a}-\stackrel{\to }{b}\right)$

the order is not important here, it can be $=\stackrel{\to }{b}+\dots$ or even $\dots +\left(\stackrel{\to }{b}-\stackrel{\to }{a}\right)$

### Vectors blend (unversal for no unit vectors)

Source

Vector3 Slerp(Vector3 start, Vector3 end, float percent)
{
// Dot product - the cosine of the angle between 2 vectors.
float dot = Vector3.Dot(start, end);

// Clamp it to be in the range of Acos()
// This may be unnecessary, but floating point
// precision can be a fickle mistress.
Mathf.Clamp(dot, -1.0f, 1.0f);

// Acos(dot) returns the angle between start and end,
// And multiplying that by percent returns the angle between
// start and the final result.
float theta = Mathf.Acos(dot) * percent;
Vector3 RelativeVec = end - start * dot;
RelativeVec.Normalize();

// Orthonormal basis
// The final result.
return ((start*Mathf.Cos(theta)) + (RelativeVec * Mathf.Sin(theta)));
}