Find the Center Coordinate of a Sphere Given the Tangent Point I'm struggling with a problem I'm coding on for days now. Really appreciate your inputs.
Suppose you have a line segment in 3D space with endpoints (x1, y1, z1) and (x2, y2, z2). Along the segment is a "bend coordinate" (xb, yb, zb). The goal here is to find the centre coordinate of a sphere (xc, yc, zc) that is r units from the bend coordinate (or tangent point) and is at an angle theta from a vertical line perpendicular to the segment. See image below.
So far, my strategy consists of the following steps:

*

*Find an equation of a plane perpendicular to the bend coordinate and passing through the centre coordinate.

*Find a sphere equation with radius r. Its centre is at the bend coordinate.

*Equating these two equations and solving one variable will give a solution set of a circle on said plane (which can be interpreted as the intersection of the sphere and the plane or the locus of all points of the centre coordinate).

The problem I have right now is to find the specific point on the circle intersection that matches the theta criteria.
I'm not entirely sure whether my solution is also a good approach in coding. The expressions I have derived from equating the sphere and the plane is a long one and may be computationally expensive. If someone can introduce a better approach, I'm all ears.

 A: Start by finding the unit vector along the segment.  It is given by
$ \mathbf{u_1} = \dfrac{\mathbf{P_2} - \mathbf{P_1}}{\| \mathbf{P_2} - \mathbf{P_1} \|} $
where $\mathbf{P_1} = (x_1, y_1, z_1), \mathbf{P_2} = (x_2, y_2, z_2) $
Next, you need to determine the vector that is perpendicular to the line segment $\mathbf{P_1 P_2}$, i.e. perpendicular to the unit vector $\mathbf{u_1}$.  This perpendicular vector must be supplied, otherwise it is impossible to locate the center of the sphere.
So, now we can assume that we have a unit vector $\mathbf{u_2} \perp \mathbf{u_1}$.  Finally compute the unit vector that is perpendicular to both $\mathbf{u_1}$ and $\mathbf{u_2}$, it is simply
$ \mathbf{u_3} = \mathbf{u_1} \times \mathbf{u_2} $
(The cross product of $\mathbf{u_1}$ and $\mathbf{u_2}$ )
Now the center of the sphere $\mathbf{C}= (x_C, y_C, z_C)$ lies in the plane that passes through the point $\mathbf{P_{bend}} = (x_b, y_b, z_b)$ , and whose normal vector is $\mathbf{u_1}$ (hence, it is spanned by $\mathbf{u_2}$ and $\mathbf{u_3} $).  Further the vector $\mathbf{P_{bend} C }$ has a magnitude $R$ (given), and makes a given angle $\theta$ with $\mathbf{u_2}$, therefore
$\mathbf{C} = \mathbf{P_{bend}} + R \left(\cos( \theta) \mathbf{u_2} \pm \sin (\theta) \mathbf{u_3}\right) $
