<s>[INST] Find length of intersection between 2 points and a sphere I have a sphere and 2 points. The points have (x,y,z) coordinates and the sphere is defined by its centre (0,0,0) and radius R. I am trying to find the length between the 2 points which intersects the sphere. How can I obtain the equation to describe this length?
See below, my objective is Length, L:

 [/INST] The sphere is: $ x^2 + y^2 + z^2 =R^2 $ . The line is: $ x=x \\ y=x\cdot\frac{y_2-y_1}{x_2-x_1} -\frac{x_1(y_2-y_1)}{x_2-x_1} +y_1 =:r_yx+y_0 \\ z=x\cdot\frac{ z_2-z_1 }{x_2-x_1} -\frac{x_1(z_2-z_1)}{x_2-x_1} +z_1  =:r_zx+z_0$.
Sphere and line intersect when:
$ x^2 + (r_yx+y_0)^2 + (r_zx+z_0)^2 =R^2 \implies \\
x^2\cdot(1+r_y^2+ r_z^2) + x\cdot(2r_yy_0+2r_zz_0)+(y_0^2+z_0^2-R^2)=0\\
\\
 $
Solving for $x$ :
$$
x=\frac{ -(2r_yy_0+2r_zz_0) \pm \sqrt{(2r_yy_0+2r_zz_0)^2-4(1+r_y^2+ r_z^2)(y_0^2+z_0^2-R^2)}}{2(1+r_y^2+ r_z^2)}
$$
So for the intersection:
$$
(\Delta x)^2 = \frac{(2r_yy_0+2r_zz_0)^2-4(1+r_y^2+ r_z^2)(y_0^2+z_0^2-R^2)}{(1+r_y^2+ r_z^2)^2} \\
(\Delta y)^2 =(\Delta x)^2(\frac{y_2-y_1}{x_2-x_1})^2\\
(\Delta z)^2 =(\Delta x)^2(\frac{z_2-z_1}{x_2-x_1})^2\\
$$
Length of the line segment is:
$$
\Delta x \cdot \sqrt{1 +(\frac{y_2-y_1}{x_2-x_1})^2 + (\frac{z_2-z_1}{x_2-x_1})^2} = \\
\sqrt{ \frac{(2r_yy_0+2r_zz_0)^2-4(1+r_y^2+ r_z^2)(y_0^2+z_0^2-R^2)}{(1+r_y^2+ r_z^2)^2}}\cdot\sqrt{1 +r_y^2 + r_z^2}=
$$

$$
2\cdot\sqrt{ \frac{(r_yy_0+r_zz_0)^2-(1+r_y^2+ r_z^2)(y_0^2+z_0^2-R^2)}{(1+r_y^2+ r_z^2)}}
$$

</s>