# Angle between two vectors

|Something rather simple but I keep forgetting how to do it: what is the angle between any two vectors? It’s simple when you know how (as well as being an opportunity to try LaTeX for WordPress!):

## Example

Find the angle between two vectors in 3D space:

This technique can be used for any number of dimensions. For 2D space (e.g. vectors on a graph on a piece of paper) **u** and **v** will each contain two values instead of three, and the calculation is then done in the same way.

## C# code example

//returns angle between two vectors //input two vectors u and v //for 'returndegrees' enter true for an answer in degrees, false for radians double AngleBetween(Vector3d u, Vector3d v, bool returndegrees) { double toppart = 0; for (int d = 0; d < 3; d++) toppart += u[d] * v[d]; double u2 = 0; //u squared double v2 = 0; //v squared for (int d = 0; d < 3; d++) { u2 += u[d] * u[d]; v2 += v[d] * v[d]; } double bottompart = 0; bottompart = Math.Sqrt(u2 * v2); double rtnval = Math.Acos(toppart / bottompart); if(returndegrees) rtnval *= 360.0 / (2 * Math.PI); return rtnval; }