I'm working on a programming project. In this project, a ray is fired from a point in 4-space. I need to find the distance from this ray to a number of other points in 4-space.
I attempted to solve this myself, but failed. The ray, $r$, can be written as $a + bt$, where $a$ and $b$ are vectors and t is the distance along the ray. The distance from this ray to a point $p$ can be written as $\lvert p - (a + bt)\rvert$. Dropping the square root (minimizing squared distance will also minimize distance), this is
$$((p_x - a_x) - b_xt) ^ 2 + \cdots$$
Differentiating with respect to $t$ yields
$$2(p_x - a_x - b_xt)(-b_x) + \cdots = 0$$
Simplify…
$$-p_xb_x + a_xb_x + b_x^2t + \cdots = 0$$
Solve for $t$…
$$t(b_x^2 + b_y^2 + \cdots) = p_xb_x - a_xb_x + \cdots \\ t = (p_xb_x - a_xb_x + \cdots) / (b_x^2 + \cdots)$$
In my case, $b$ is a unit vector, so the bottom simplifies.
$$t = (p_xb_x - a_xb_x + \cdots) \\ t = b\cdot(p - a)$$
So the distance should be
$$\lvert p - a - b(b \cdot (p - a))\rvert = d$$
Is that correct? I'm not sure if the problems I'm having are a result of my distance formula or are a problem with other aspects of my program.