line - sphere intersection

Equation for line (cartesian)

Equation for sphere (cartesian)

Assumed to be centred at 0, the coordinate system origin.

0=-1 + \frac{x^{2}}{R^{2}} + \frac{y^{2}}{R^{2}} + \frac{z^{2}}{R^{2}}

Intersection solutions

Parametric solution (t). Solutions were derived automatically using sympy.

Points in cartesian coordinates (x, y, z)

C Code
x = x0 + (-k*y0 - l*z0 - x0 + sqrt(pow(R, 2)*pow(k, 2) + pow(R, 2)*pow(l, 2) + pow(R, 2) - pow(k, 2)*pow(x0, 2) - pow(k, 2)*pow(z0, 2) + 2*k*l*y0*z0 + 2*k*x0*y0 - pow(l, 2)*pow(x0, 2) - pow(l, 2)*pow(y0, 2) + 2*l*x0*z0 - pow(y0, 2) - pow(z0, 2)))/(pow(k, 2) + pow(l, 2) + 1);
y = k*(-k*y0 - l*z0 - x0 + sqrt(pow(R, 2)*pow(k, 2) + pow(R, 2)*pow(l, 2) + pow(R, 2) - pow(k, 2)*pow(x0, 2) - pow(k, 2)*pow(z0, 2) + 2*k*l*y0*z0 + 2*k*x0*y0 - pow(l, 2)*pow(x0, 2) - pow(l, 2)*pow(y0, 2) + 2*l*x0*z0 - pow(y0, 2) - pow(z0, 2)))/(pow(k, 2) + pow(l, 2) + 1) + y0;
z = l*(-k*y0 - l*z0 - x0 + sqrt(pow(R, 2)*pow(k, 2) + pow(R, 2)*pow(l, 2) + pow(R, 2) - pow(k, 2)*pow(x0, 2) - pow(k, 2)*pow(z0, 2) + 2*k*l*y0*z0 + 2*k*x0*y0 - pow(l, 2)*pow(x0, 2) - pow(l, 2)*pow(y0, 2) + 2*l*x0*z0 - pow(y0, 2) - pow(z0, 2)))/(pow(k, 2) + pow(l, 2) + 1) + z0;
x = x0 - (k*y0 + l*z0 + x0 + sqrt(pow(R, 2)*pow(k, 2) + pow(R, 2)*pow(l, 2) + pow(R, 2) - pow(k, 2)*pow(x0, 2) - pow(k, 2)*pow(z0, 2) + 2*k*l*y0*z0 + 2*k*x0*y0 - pow(l, 2)*pow(x0, 2) - pow(l, 2)*pow(y0, 2) + 2*l*x0*z0 - pow(y0, 2) - pow(z0, 2)))/(pow(k, 2) + pow(l, 2) + 1);
y = -k*(k*y0 + l*z0 + x0 + sqrt(pow(R, 2)*pow(k, 2) + pow(R, 2)*pow(l, 2) + pow(R, 2) - pow(k, 2)*pow(x0, 2) - pow(k, 2)*pow(z0, 2) + 2*k*l*y0*z0 + 2*k*x0*y0 - pow(l, 2)*pow(x0, 2) - pow(l, 2)*pow(y0, 2) + 2*l*x0*z0 - pow(y0, 2) - pow(z0, 2)))/(pow(k, 2) + pow(l, 2) + 1) + y0;
z = -l*(k*y0 + l*z0 + x0 + sqrt(pow(R, 2)*pow(k, 2) + pow(R, 2)*pow(l, 2) + pow(R, 2) - pow(k, 2)*pow(x0, 2) - pow(k, 2)*pow(z0, 2) + 2*k*l*y0*z0 + 2*k*x0*y0 - pow(l, 2)*pow(x0, 2) - pow(l, 2)*pow(y0, 2) + 2*l*x0*z0 - pow(y0, 2) - pow(z0, 2)))/(pow(k, 2) + pow(l, 2) + 1) + z0;

Distance inside

Distance between crossing points.

2 \left\lvert{\frac{1}{k^{2} + l^{2} + 1} \sqrt{R^{2} k^{2} + R^{2} l^{2} + R^{2} - k^{2} x_{0}^{2} - k^{2} z_{0}^{2} + 2 k l y_{0} z_{0} + 2 k x_{0} y_{0} - l^{2} x_{0}^{2} - l^{2} y_{0}^{2} + 2 l x_{0} z_{0} - y_{0}^{2} - z_{0}^{2}}}\right\rvert

C Code

sol1 = (-k*y0 - l*z0 - x0 + sqrt(pow(R, 2)*pow(k, 2) + pow(R, 2)*pow(l, 2) + pow(R, 2) - pow(k, 2)*pow(x0, 2) - pow(k, 2)*pow(z0, 2) + 2*k*l*y0*z0 + 2*k*x0*y0 - pow(l, 2)*pow(x0, 2) - pow(l, 2)*pow(y0, 2) + 2*l*x0*z0 - pow(y0, 2) - pow(z0, 2)))/(pow(k, 2) + pow(l, 2) + 1);
sol2 = -(k*y0 + l*z0 + x0 + sqrt(pow(R, 2)*pow(k, 2) + pow(R, 2)*pow(l, 2) + pow(R, 2) - pow(k, 2)*pow(x0, 2) - pow(k, 2)*pow(z0, 2) + 2*k*l*y0*z0 + 2*k*x0*y0 - pow(l, 2)*pow(x0, 2) - pow(l, 2)*pow(y0, 2) + 2*l*x0*z0 - pow(y0, 2) - pow(z0, 2)))/(pow(k, 2) + pow(l, 2) + 1);
distance = 2*fabs(sqrt(pow(R, 2)*pow(k, 2) + pow(R, 2)*pow(l, 2) + pow(R, 2) - pow(k, 2)*pow(x0, 2) - pow(k, 2)*pow(z0, 2) + 2*k*l*y0*z0 + 2*k*x0*y0 - pow(l, 2)*pow(x0, 2) - pow(l, 2)*pow(y0, 2) + 2*l*x0*z0 - pow(y0, 2) - pow(z0, 2))/(pow(k, 2) + pow(l, 2) + 1));

Equation for line (spherical)

Equation for sphere (cartesian)

Assumed to be centred at 0, the coordinate system origin.

0=-1 + \frac{x^{2}}{R^{2}} + \frac{y^{2}}{R^{2}} + \frac{z^{2}}{R^{2}}

Intersection solutions

Parametric solution (t). Solutions were derived automatically using sympy.

Points in cartesian coordinates (x, y, z)

C Code
x = x0 + (-x0*sin(theta)*cos(phi) - y0*sin(phi)*sin(theta) - z0*cos(theta) - sqrt(pow(R, 2) - pow(x0, 2)*pow(sin(phi), 2)*pow(sin(theta), 2) + pow(x0, 2)*pow(sin(theta), 2) - pow(x0, 2) + 2*x0*y0*sin(phi)*pow(sin(theta), 2)*cos(phi) + 2*x0*z0*sin(theta)*cos(phi)*cos(theta) + pow(y0, 2)*pow(sin(phi), 2)*pow(sin(theta), 2) - pow(y0, 2) + 2*y0*z0*sin(phi)*sin(theta)*cos(theta) - pow(z0, 2)*pow(sin(theta), 2)))*sin(theta)*cos(phi);
y = y0 + (-x0*sin(theta)*cos(phi) - y0*sin(phi)*sin(theta) - z0*cos(theta) - sqrt(pow(R, 2) - pow(x0, 2)*pow(sin(phi), 2)*pow(sin(theta), 2) + pow(x0, 2)*pow(sin(theta), 2) - pow(x0, 2) + 2*x0*y0*sin(phi)*pow(sin(theta), 2)*cos(phi) + 2*x0*z0*sin(theta)*cos(phi)*cos(theta) + pow(y0, 2)*pow(sin(phi), 2)*pow(sin(theta), 2) - pow(y0, 2) + 2*y0*z0*sin(phi)*sin(theta)*cos(theta) - pow(z0, 2)*pow(sin(theta), 2)))*sin(phi)*sin(theta);
z = z0 + (-x0*sin(theta)*cos(phi) - y0*sin(phi)*sin(theta) - z0*cos(theta) - sqrt(pow(R, 2) - pow(x0, 2)*pow(sin(phi), 2)*pow(sin(theta), 2) + pow(x0, 2)*pow(sin(theta), 2) - pow(x0, 2) + 2*x0*y0*sin(phi)*pow(sin(theta), 2)*cos(phi) + 2*x0*z0*sin(theta)*cos(phi)*cos(theta) + pow(y0, 2)*pow(sin(phi), 2)*pow(sin(theta), 2) - pow(y0, 2) + 2*y0*z0*sin(phi)*sin(theta)*cos(theta) - pow(z0, 2)*pow(sin(theta), 2)))*cos(theta);
x = x0 + (-x0*sin(theta)*cos(phi) - y0*sin(phi)*sin(theta) - z0*cos(theta) + sqrt(pow(R, 2) - pow(x0, 2)*pow(sin(phi), 2)*pow(sin(theta), 2) + pow(x0, 2)*pow(sin(theta), 2) - pow(x0, 2) + 2*x0*y0*sin(phi)*pow(sin(theta), 2)*cos(phi) + 2*x0*z0*sin(theta)*cos(phi)*cos(theta) + pow(y0, 2)*pow(sin(phi), 2)*pow(sin(theta), 2) - pow(y0, 2) + 2*y0*z0*sin(phi)*sin(theta)*cos(theta) - pow(z0, 2)*pow(sin(theta), 2)))*sin(theta)*cos(phi);
y = y0 + (-x0*sin(theta)*cos(phi) - y0*sin(phi)*sin(theta) - z0*cos(theta) + sqrt(pow(R, 2) - pow(x0, 2)*pow(sin(phi), 2)*pow(sin(theta), 2) + pow(x0, 2)*pow(sin(theta), 2) - pow(x0, 2) + 2*x0*y0*sin(phi)*pow(sin(theta), 2)*cos(phi) + 2*x0*z0*sin(theta)*cos(phi)*cos(theta) + pow(y0, 2)*pow(sin(phi), 2)*pow(sin(theta), 2) - pow(y0, 2) + 2*y0*z0*sin(phi)*sin(theta)*cos(theta) - pow(z0, 2)*pow(sin(theta), 2)))*sin(phi)*sin(theta);
z = z0 + (-x0*sin(theta)*cos(phi) - y0*sin(phi)*sin(theta) - z0*cos(theta) + sqrt(pow(R, 2) - pow(x0, 2)*pow(sin(phi), 2)*pow(sin(theta), 2) + pow(x0, 2)*pow(sin(theta), 2) - pow(x0, 2) + 2*x0*y0*sin(phi)*pow(sin(theta), 2)*cos(phi) + 2*x0*z0*sin(theta)*cos(phi)*cos(theta) + pow(y0, 2)*pow(sin(phi), 2)*pow(sin(theta), 2) - pow(y0, 2) + 2*y0*z0*sin(phi)*sin(theta)*cos(theta) - pow(z0, 2)*pow(sin(theta), 2)))*cos(theta);

Distance inside

Distance between crossing points.

2 \left\lvert{\sqrt{R^{2} - x_{0}^{2} \sin^{2}{\left (\phi \right )} \sin^{2}{\left (\theta \right )} + x_{0}^{2} \sin^{2}{\left (\theta \right )} - x_{0}^{2} + 2 x_{0} y_{0} \sin{\left (\phi \right )} \sin^{2}{\left (\theta \right )} \cos{\left (\phi \right )} + 2 x_{0} z_{0} \sin{\left (\theta \right )} \cos{\left (\phi \right )} \cos{\left (\theta \right )} + y_{0}^{2} \sin^{2}{\left (\phi \right )} \sin^{2}{\left (\theta \right )} - y_{0}^{2} + 2 y_{0} z_{0} \sin{\left (\phi \right )} \sin{\left (\theta \right )} \cos{\left (\theta \right )} - z_{0}^{2} \sin^{2}{\left (\theta \right )}}}\right\rvert

C Code

sol1 = -x0*sin(theta)*cos(phi) - y0*sin(phi)*sin(theta) - z0*cos(theta) - sqrt(pow(R, 2) - pow(x0, 2)*pow(sin(phi), 2)*pow(sin(theta), 2) + pow(x0, 2)*pow(sin(theta), 2) - pow(x0, 2) + 2*x0*y0*sin(phi)*pow(sin(theta), 2)*cos(phi) + 2*x0*z0*sin(theta)*cos(phi)*cos(theta) + pow(y0, 2)*pow(sin(phi), 2)*pow(sin(theta), 2) - pow(y0, 2) + 2*y0*z0*sin(phi)*sin(theta)*cos(theta) - pow(z0, 2)*pow(sin(theta), 2));
sol2 = -x0*sin(theta)*cos(phi) - y0*sin(phi)*sin(theta) - z0*cos(theta) + sqrt(pow(R, 2) - pow(x0, 2)*pow(sin(phi), 2)*pow(sin(theta), 2) + pow(x0, 2)*pow(sin(theta), 2) - pow(x0, 2) + 2*x0*y0*sin(phi)*pow(sin(theta), 2)*cos(phi) + 2*x0*z0*sin(theta)*cos(phi)*cos(theta) + pow(y0, 2)*pow(sin(phi), 2)*pow(sin(theta), 2) - pow(y0, 2) + 2*y0*z0*sin(phi)*sin(theta)*cos(theta) - pow(z0, 2)*pow(sin(theta), 2));
distance = 2*fabs(sqrt(pow(R, 2) - pow(x0, 2)*pow(sin(phi), 2)*pow(sin(theta), 2) + pow(x0, 2)*pow(sin(theta), 2) - pow(x0, 2) + 2*x0*y0*sin(phi)*pow(sin(theta), 2)*cos(phi) + 2*x0*z0*sin(theta)*cos(phi)*cos(theta) + pow(y0, 2)*pow(sin(phi), 2)*pow(sin(theta), 2) - pow(y0, 2) + 2*y0*z0*sin(phi)*sin(theta)*cos(theta) - pow(z0, 2)*pow(sin(theta), 2)));


By Johannes Buchner | Source code: https://github.com/JohannesBuchner/intersection | Open a issue or pull request if you would like somthing added