Find angle to projected point on axis and angle to projected point I am working on a robotics project and all i have to do is to make  a 3 wheeled robot to detect a tag (chessboard pattern for example) with it's camera, go to it, and stops facing it exactly, that means the X axis of the camera should be parallel to the Tag's X' axis, also the distance between the center of the camera and the X' axis of the tag should be 0. all I have is the Qauternions and the position in 3d of the target (tag) and the camera of the robot is the origin (carrier of the cartesian coordinate system). 
Graphical explanation of the Situation
I figured out alpha by calculating the angle between X and X' but in some cases the rotation direction is misleading for the robot, here is an example:
example
Also the distance from the projected point is always wrong, this is how I am calculating it: I calculate the line l that passes from the point (xt , yt) of the target and makes an angle alpha with X' vector and then I calculate the disatnce between the origin (0,0) and the line l, unfortunately it is wrong in most of the cases.
I can't figure out what I am doing wrong, 
Looking forward for your help.
 A: So, the coordinate system of the robot is $X, Y, Z$ where the vectors are of unit length and orthogonal to each other. $R$ is the point of the robot, to which the coordinate system is attached. Assume your vector $X'$ is given by coordinates $(u,v)$, i.e. 
$$X' = u \, X + v \, Z$$ and that the location of the tag is at point $T$. Your goal is to execute motion that takes vector $X$ to vector $-X' = -u\, X - v\, Z$ and vector $Z$ to vector $Z' = v\, X - u \, Z$. So simply take the dot product $\big(X\cdot (- X')\big) = - (X \cdot X') = \cos(\alpha)$, assuming that $X
$ is unit length, i.e. $|X'|^2 = (X' \cdot X') = 1$. If not, then take 
$$\frac{\big(X\cdot (-X')\big)}{\sqrt{(X'\cdot X')}} = - \frac{\big(X\cdot (u\, X + v\,Z)\big)}{\sqrt{u^2 + v^2}} =  - \frac{u \big(X\cdot X\big) + v\big(X\cdot Z\big)}{\sqrt{u^2 + v^2}} =  - \frac{u \big(X\cdot X\big)}{\sqrt{u^2 + v^2}}= \frac{- u}{\sqrt{u^2 + v^2}}$$ because $\big(X\cdot Z\big) = 0$ due to the orthogonality between vector $X$ and $Z$, and $\big(X\cdot X\big) = 1$ because $X$ is the unit vector from the orthonormal frame of the robot. Then $$\cos(\alpha) = \frac{- u}{\sqrt{u^2 + v^2}}$$ To determine the angle, however, you want to take $$\alpha = \text{arccos}\left(\frac{- u}{\sqrt{u^2 + v^2}}\right)$$ which gives angles only between $[0,\pi]$. You, however, may want angles between $[-\pi,\pi]$ or equivalently between $[0,2\pi]$. Then the sign of a second dot product
$$\sin(\alpha) = \frac{\big(Z\cdot(- X')\big)}{\sqrt{(X'\cdot X')}} = - \frac{\big(Z\cdot (u\, X + v\,Z)\big)}{\sqrt{u^2 + v^2}} = - \frac{u \big(Z\cdot X\big) + v\big(Z\cdot Z\big)}{\sqrt{u^2 + v^2}} = - \frac{v \big(Z\cdot Z\big)}{\sqrt{u^2 + v^2}}= \frac{v}{\sqrt{u^2 + v^2}}$$ Thus the sign of the angle $\alpha$ should be $\text{sign}(\sin(\alpha)) = - \frac{v}{|v|}$ So finally, your angle should be 
$$\alpha = -\frac{v}{|v|} \text{arccos}\left(\frac{-u}{\sqrt{u^2 + v^2}}\right)$$
I am a bit in a hurry, so one may need to double check the signs.
From what I understand, you basically want the robot to first turn in a direction orthogonal to direction $X'$, move straight along the direction determined by the vector $Z'$ orthogonal to the vector $X'$ until it hits the line determined by the tag point $T$ and the vector $X'$, then turn nighty degrees and then go in the direction parallel to the vector $X'$ straight down to the tag. Am I right?
Then first, you have to calculate how far the the robot has to go perpendicularly to $X'$ to hit the line determined by $T$ and $X'$. Let the coordinates of $T$ in the frame of the robot be $(t_1,t_2)$, i.e. $T = t_1 \, X + t_2 \, Z$. The equation of the line  determined by $T$ and $X'$ can be written as $$v(x - t_1) - u(z - t_2) = 0$$ where $x,z$ denote coordinates in the plane. The robot has to go first along the line determined by a vector orthogonal to $X'$, which is $Z' = v \, X - u\, Z$. Then the trajectory along $Z'$ should be $sZ' = sv \, X - su \, Z$ and  $s$ should be chosen so that $sZ'$ actually lies on the line determined by $T$ and $X'$. In other words, $s$ satisfies the equation
$$v(sv - t_1) - u(-su - t_2) = 0$$ which transforms to 
$$sv^2 - t_1v + su^2 + t_2u = 0$$ Solving for $s$ leads to
$$s = \frac{t_1 v - t_2 u}{u^2 + v^2}$$ Finally, the oriented distance between the point $R$ of the robot and the point $P$ on the line determined by $T$ and $X'$ is
$$d = \text{sign}(s) |sZ'| = \text{sign}(s) |s|\, |Z'| = s \sqrt{u^2 + v^2} = \frac{t_1 v - t_2 u}{\sqrt{u^2 + v^2}}$$ 
So to guide the robot to the tag, the way you want, you have to decide that positively oriented rotation should be a counter-clockwise rotation (when looking at the plane from above), while negatively oriented should be clockwise rotation. Then, provided the vector $X' = u\, X + v\, Z$, calculate the oriented distance $$d = \frac{t_1 v - t_2 u}{\sqrt{u^2 + v^2}}$$ and the angle $$\alpha =  -\frac{v}{|v|} \text{arccos}\left(\frac{-u}{\sqrt{u^2 + v^2}}\right)$$ Then make the robot turn of angle $\frac{\pi}{2}-\alpha$ so that it is ready to travel in the  direction determined by vector $Z' \perp X'$ and if $d>0$ let the robot travel forward distance $d$ (if $d<0$ the robot has to go backwards), then let it stop, make it turn $90^{\circ}$ clockwise facing the tag and then let it go down to the tag along the direction $-X'$. If $d<0$ the robot has to go backwards after turning initially. The case when $v=0$ should be handled separately I think. 
