It's easier (I think) to do the whole thing using vectors and dot products. At least it's more compact, which makes it a little less prone to error.

First, the sphere. p is a point at (x,y,z), r is the sphere radius, p_{c}
is the sphere center at (x_{c}, y_{c}, z_{c}).
The equation for a sphere centered at the origin is

xThe equation for a sphere centered at p^{2}+ y^{2}+ z^{2}= p^{.}p = r^{2}or p^{.}p - r^{2}= 0

(p - pMeanwhile, we've also got a ray, in direction d = (d_{c})^{.}(p - p_{c}) - r^{2}= 0

p = t d + pPlugging the ray equation into the sphere equation, we get_{0}

(t d + pTo solve, we'll need to get this into a t_{0}- p_{c})^{.}(t d + p_{0}- p_{c}) - r^{2}= 0

dOr, to make it even more explicit:^{.}d t^{2}+ 2 d^{.}(p_{0}- p_{c}) t + (p_{0}- p_{c})^{.}(p_{0}- p_{c}) - r^{2}= 0

a = dAs a reminder of one thing I said in class, the sign of the discriminant, b^{.}d

b = 2 d^{.}(p_{0}- p_{c})

c = (p_{0}- p_{c})^{.}(p_{0}- p_{c}) - r^{2}