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!):

cos\theta=\frac{\sum{uv}}{\left\{(\sum u^2)(\sum v^2)\right\}^{0.5}}=\frac{\textbf{u'v}}{\left\{(\textbf{u'u})(\textbf{v'v})\right\}^{0.5}}


Find the angle between two vectors in 3D space:


cos\theta=\frac{\sum{uv}}{\left\{(\sum u^2)(\sum v^2)\right\}^{0.5}}=\frac{5\times10+0\times2+0\times5}{\left\{(5^2+0^2+0^2)(10^2+2^2+5^2)\right\}^{0.5}}=\frac{50}{\left\{25\times129\right\}^{0.5}}=0.880 \therefore\theta=28.3\textdegree

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;

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.