Finding point in 3D Space on line with fixed distance I tried to look for similar questions but couldn't find something relating so here goes:
I have 3 points in 3D Space: Point A, Point B and Point O.
Point O is just the origin at [0, 0, 0].
I need to define a point C, which is between A and B, and exactly at distance d from O.
How I tried to approach this at first was to construct a direction from A to B, which can be used to define the line between A and B, then using the formula f = A + t * (A - B) to define the line. But then I got stuck and used the very ugly brute force method of looping over possible values of t until the distance between the point at said t was d from O, but I want to know if it can be done in one single simple mathematical equation to calculate C.
Kind regards and thanks in advance.
 A: The points A and B determine a line AB.
The distance from O to the line AB is a real positive number.
The distance from O to any point on the line is greater than the perpendicular distance between O and the line.
If the given distance d is less than the perpendicular distance between O and the line determined by A and B, the problem has no solution.
If $d=min(OA, OB)\lor d=max(OA,OB)$ the problem has one solution.
If $min(OA,OB)\lt d\lt max(OA,OB)$ the problem has one or two distinct solutions where the resulting point falls between A and B.
Otherwise, the problem still has two distinct solutions where the point resides on the line outside of the segment AB.
Hope this helps.
Now the almost full solution:
You have three equations to write.
First, if the point is on the line then there is a zero cross product:
$D\epsilon AB\Rightarrow \vec{AD}\times \vec{AB}=\vec{0}\Leftrightarrow (\vec{OD}-\vec{OA})\times \vec{AB}=\vec{0}$
You further find out that $sin(\theta)=\frac{\vec{OA}\times \vec{AB}}{d\cdot ||\vec{AB}||}$
The second equation is this:
$\vec{OD}\cdot \vec{AB}=d\cdot||\vec{AB}|| \cdot cos(\theta)=||\vec{AD}||$
The third and last that gives you the coordinates of point D is:
$\frac{x_D-x_A}{x_B-x_A}=\frac{AD}{AB}$. Use a similar equation to find $y_D, z_D$.
There is one extra observation: solving for $\theta$ should lead to two possible solutions. I have not mentioned the constraints for that since I have presented the case analysis at the beginning.
A: If point $p(\lambda)$ in the segment $\lambda A+(1-\lambda)B,\ \ 0\le \lambda\le 1$ is located at a distance $d$ from $O$ then
$$
\|\lambda A+(1-\lambda)B\|^2= \|B+\lambda(A-B)\|^2 = \|B\|^2+2\lambda B\cdot (A-B)+\lambda^2\|A-B\|^2 = d^2
$$
or solving for $\lambda$
$$
\lambda^* = \frac{B\cdot(B-A)\pm\sqrt{(B\cdot(B-A))^2 -\|A-B\|^2(\|B\|^2-d^2) }}{\|A-B\|^2}
$$
Here if $\ 0\le \lambda^*\le 1$ then $C = \lambda^* A+(1-\lambda^*)B$
