# How to Calculate the Direction of a Vector

Let's say I have points $a$, $b$, and $c$. We also have $\vec{ab}$ and $\vec{ac}$. Finally, we know neither vector's direction. (That is, the vector's angle on each axis as if the vector were translated so that it's tail is at (0,0,0). For example, $\overrightarrow{(0,0,0)(10,10,10)}$'s direction would be (45$\circ$, 45$\circ$, 45$\circ$). I'm not sure if that's how a vector's direction actually works, but that's how I'm using it in a program I'm writing.) So, how do I find $\vec{ab}$'s "direction?"

-
In my experience, it's rarely useful to compute the "direction" of a 3D vector as a separate value. What do you want to use these quantities for? There may be a simpler way to do it. – Rahul Jan 21 '13 at 20:31
@RahulNarain 3D projection... Or, more specifically, perspective projection. – Steven Fontaine Jan 21 '13 at 23:26
You most definitely do not need any notion of "direction" to perform perspective projection. – Rahul Jan 21 '13 at 23:29
"$\theta$$_{x,y,z}$ - The orientation of the camera." I'm using a vector to represent the camera. (Where the magnitude of the vector is the render distance.) – Steven Fontaine Jan 21 '13 at 23:40
For camera angles, what is more commonly used is elevation and azimuth. Given the origin, it takes two angles, not three, to specify a direction. – Ross Millikan Jan 22 '13 at 1:05

For A, maybe what you want are the direction cosines. In your example they will be $(\frac 1{\sqrt 3},\frac 1{\sqrt 3},\frac 1{\sqrt 3})$. The angle between your vector and each coordinate axis is not $45^\circ$, but $\arccos \frac 1{\sqrt 3} \approx54.74^\circ$. To find the direction cosines of $\vec{ab}$, you just take the $x,y,z$ components of $\vec {ab}$ divided by its length.

For B, you need to define the question better. One thing would be the angle between $\vec {ab}$ and $\vec {ac}$, which you can find using the dot product: the cosine of the angle is $\frac {\vec {ab}\cdot \vec {ac}}{|\vec {ab}|\cdot |\vec {ac}|}$. Another would be the direction cosines of $\vec {ac}-\vec {ab}$, calculated as above.

Added: the azimuth-elevation coordinate system is a natural one for camera pointing. It is especially useful if you have motors to drive in these two axes. Azimuth is the angle from some reference direction around a vertical axis, elevation is the angle up or down from the horizontal plane. If you have motors in those axes, the angles tell you how to move them from $(0,0)$ to get where you want to go. They also answer question B-to go from $(a_{az},a_{el})$ to $(b_{az},b_{el})$, you move by $(b_{az}-a_{az},b_{el}-a_{el})$

-
I'm not quite sure I understand directional cosines. And I realized that I could really simplify the question by actually telling you what it is that I'm doing, rather than trying to generalize it.. Basically, I'm trying to create a vector out of two points. I just need to get the vector's direction.. Since direction is a very broad and general term, I thought that I would (in my program that I am writing which has brought about this question) define direction in the simplest, yet, for my needs, most useful (while remaining simple) way. So, I defined direction as the angles from the origin, – Steven Fontaine Jan 22 '13 at 0:02
each angle being separate, and as if you were looking at a flattened, 2D image of it. So, back to the main point, if I had 2 points, and needed to find those angles, how would I? (In typing this, I've actually cleared-up what it is that I need, and think that I know the answer now. :P If you were to try to find the angle around the Z axis, you would just disregard the Z coordinates and solve just like in basic trig, right?) – Steven Fontaine Jan 22 '13 at 0:05
@StevenFontaine: The direction cosines are exactly the cosines of the angles from the axes. You can certainly project the vector onto the $xy, xz, yz$ planes and measure the angle from one axis-that is a well defined operation. The angles will depend on each other in ways that are hard to understand. Whether it is useful is another question. – Ross Millikan Jan 22 '13 at 1:04
I'm starting to feel overwhelmed. :P Ive done kind of what I'm trying to do a few times before, and I always get confused. One thing I'm going to use this for is perspective projection (literally just a rotation matrix), but I need a way to find the angles required. (On the wiki page, the camera's "orientation.") I made a stupid mistake in my last comment. Literally, what I'm using this for right now is, I'm given two vectors ($\vec a$ & $\vec b$) which share a tail point. I know $a$'s direction, but not $b$'s. I need $b$'s. – Steven Fontaine Jan 22 '13 at 1:24
@StevenFontaine: regarding the last comment, yes, if you are looking for the angle to move around $z$ you would disregard the $z$ coordinate and look in the $xy$ plane. That is what is usually called azimuth. It is very useful to have your coordinates match the axes of motion, but there are only two of those. The range is the third coordinate in space, and corresponds to the focus of your lens. You might also look at en.wikipedia.org/wiki/Spherical_coordinates – Ross Millikan Jan 22 '13 at 3:55