Solution (source code)

= Solution

The distance from the origin to the incident straight <light ray> is $d_a$ (taking $d_a\geq0$). At its intersection with the sphere, let $i$ and $t$ be the <angle of incidence> and <angle of refraction>, measured from the radial <normal vector>. Elementary <right triangle> geometry gives $d_a=r_b\sin i$. By <Snell's law>,
$$
n_a\sin i=n_b\sin t.
$$
The refracted straight <light ray> inside the sphere has distance $d_b=r_b\sin t$ from the origin. Therefore
$$
\boxed{d_b=\frac{n_a}{n_b}d_a,\qquad r\cos(\theta-\theta_b)=\frac{n_a}{n_b}d_a.}
$$
The angle $\theta_b$ describes the <normal vector> of the new line; it need not equal the propagation angle. The existence of a crossing requires $|d_a|<r_b$. A signed convention for $d_a$ gives the same line equation after choosing the orientation of its <normal vector> consistently.