TITLE: Tangent to sphere in a given plane
QUESTION [2 upvotes]: I have a sphere centered on the origin $O$. I have a point in space $Cam$ and a vector $Dir$.
How do I find a point $X$ and a Vector Hor where:

The line starting at Cam and of direction Hor, passing through X, is tangent to the sphere in X
Hor, Dir and O-Cam are on the same plane

Illustration
And if possible, with the shortest calculation possible. Technically I will be implementing this in a shader, for every given direction I need to find the horizon line on the planet. I tried to abstract the problem as much as possible.

REPLY [0 votes]: Create a local coordinate system on the plane with the center of the circle as origin, and local x direction along $\vec{OC_{am}}$. The 3×3 rotation matrix is 
$$ \begin{align} 
  E = & \begin{vmatrix} \hat{i} & \hat{j} & \hat{k} \end{vmatrix} \\
  \hat{i} &= [ \vec{O C_{am}} ] \\
  \hat{k} & = [\hat{i} \times \vec{Dir}] \\
  \hat{j} &= \hat{k} \times \hat{i}
\end{align} $$
where the $[\vec{v}] = \frac{\vec{v}}{\| \vec{v} \|}$ notation is for unit vectors, and $\times$ is the vector cross product.
Now you take the distance between the center of the circle and point $O_{am}$ along the $\hat{i}$ direction as $\ell = \| \vec{O C_{am} } \|$
The $(x,y)$ coordinate of the tangent point X on the plane are
$$ (x,y) = \left( \frac{r^2}{\ell}, \frac{r \sqrt{\ell^2-r^2}}{\ell} \right) $$
where $r$ is the radius of the circle.
The 3D coordinates of X are
$$ \vec{X} = \frac{r^2}{\ell} \hat{i} + \frac{r \sqrt{\ell^2-r^2}}{\ell} \hat{j} $$
The direction of X is $$ \vec{H_{or}} = [\vec{O_{am} X}] $$
NOTE: The equation of the tangent line on the plane is $\left(\frac{r}{\ell}\right)x + \left( \frac{\sqrt{\ell^2-r^2}}{\ell} \right)y = r$. 
Example
I have verified the calculation with a GeoGebra model:
{"set_name": "stack_exchange", "score": 2, "question_id": 1658672}