# Vector Calculations

## Addition of 2 vectors

Given vectors

**A**and**B**, they can be added together by adding their individual components:

`C = A + B`

C.x = A.x + B.x

C.y = A.y + B.y## Subtraction of 2 vectors

Given vectors

**A**and**B**, they can be subtracted from one another by subtracting their individual components:

`C = A - B`

C.x = A.x - B.x

C.y = A.y - B.y## Length of a vector

This uses a simple distance formula, or Pythagorean theorem. Take the square root of the sum of the vector's components squared.

`length = ||A||`

length = √(A.x^{2}+ A.y^{2}+ A.z^{2})

## Normalized vector (Unit vector)

A unit vector is a vector with a length of 1. Take each of the vector's components and divide them by the vector's length.

`â = A / ||A||`

â.x = A.x / ||A||

â.y = A.y / ||A||

â.z = A.z / ||A||## Dot product of 2 vectors

The dot product measures the difference between directions of two vectors, or the cosine of the angle between the two. To calculate the dot product of two vectors, take the sum of the product of each vector component and you will get a scalar value.

`d = A · B`

d = A.x*B.x + A.y*B.y + A.z*B.z

**or**

`d = ||A|| * ||B|| * cos Ѳ`

## Cross product of 2 vectors

The cross product of two vectors returns a vector perpendicular to the two vectors. This is useful for calculating the surface normal of a plane. To calculate the cross product of two vectors is as follows:

`C = A x B`

C.x = [A.y * B.z] - [A.z * B.y]

C.y = [A.z * B.x] - [A.x * B.z]

C.z = [A.x * B.y] - [A.y * B.x]## Angle between two 2D vectors

By looking at secondary equation above for calculating the dot product, one can easily use it for calculating the angle between two vectors. Use the simpler method of calculating the dot product, adding the product of the vectors components together. Take the inverse cosine, or "acos", of the dot product divided by the product of the two vector lengths.

`d = A.x*B.x + A.y*B.y`

angle = acos(d / ||A|| * ||B||)