# Math Stuff

## Get angle between two 3D vectors

See here:

```Another short post related to a question I was asked, that I figure might be helpful to others.
Getting the angle between two vectors in 2D is as simple as:

var angle = Math.atan2(vectorA.y - vectorB.y, vectorA.x - vectorB.x)

However that does not work in 3D space, however the angle
between any two vectors (2D or 3D) is defined as the

cosine theta = (A dot B) / Normalized-A * Normalized-B

Where theta is the angle between them. To find theta, we can inverse the equation:

theta = acos( (A dot B) / Normalized-A * Normalized-B )

In code getting the angle between two vectors in 3D space translates to:

// Make up two vectors
var vectorA = new Vector3(5, -20, -14);
var vectorB = new Vector3(-1, 3, 2);

// Store some information about them for below
var dot = vectorA.dot(vectorB);
var lengthA = vectorA.length();
var lengthB = vectorB.length();

// Now to find the angle
var theta = Math.acos( dot / (lengthA * lengthB) ); // Theta = 3.06 radians or 175.87 degrees```

## How to project a 3D point (x,y,z) onto a 2D plane in 3D space?

here they suggest the following answer:

```Lets say I have point (x,y,z) and plane with point (a,b,c) and normal (d,e,f).
I want to find the point that is the result of the orthogonal projection of the
first point onto the plane. I am using this in 3d graphics programming. I want to
achieve some sort of clipping onto the plane.

The projection of a point q = (x, y, z) onto a plane given by a point p = (a, b, c)
and a normal n = (d, e, f) is

q_proj = q - dot(q - p, n) * n

This calculation assumes that n is a unit vector.
``` 