Question: 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 script this out in Mathematica?

See below, my objective is length L:

[![enter image description here](https://i.stack.imgur.com/Vp4GS.png)](https://i.stack.imgur.com/Vp4GS.png)

Answer: 
You can use `RegionIntersection` and `RegionMeasure`. Using the examples from the other answers:

```
RegionIntersection[Ball[{0,1,0}, 2], InfiniteLine[{{0,0,0},{2,1,1}}]]
RegionMeasure@%
%//N

(* Line[{{1/3 (1-Sqrt[19]),1/6 (1-Sqrt[19]),1/6 \
(1-Sqrt[19])},{1/3 (1+Sqrt[19]),1/6 (1+Sqrt[19]),1/6 \
(1+Sqrt[19])}}] *)

(* Sqrt[38/3] *)

(* 3.55903 *)

SeedRandom[0]
RegionIntersection[Ball[{0,0,0}, 3], InfiniteLine@@RandomReal[{-9, 9}, {2, 3}]]
RegionMeasure@%

(* Line[{{2.11448,-1.73651,-1.23025},{2.49353,0.749673,1.4900\
7}}] *)

(* 3.70471 *)

```
Let

```
R = 2;
c = {0, 1, 0};
{p1, p2} = {{0, 0, 0}, {2, 1, 1}}

```

and

```
line = InfiniteLine[p1, p2];
sphere = Sphere[c, R];

pts = RegionIntersection[sphere, line]

```

[![enter image description here](https://i.stack.imgur.com/Bto2Y.png)](https://i.stack.imgur.com/Bto2Y.png)

```
Graphics3D[{Opacity[0.5], sphere, line, Darker@Red, PointSize[Large], pts}, Axes -> True]

```

[![enter image description here](https://i.stack.imgur.com/aShhu.png)](https://i.stack.imgur.com/aShhu.png)

Then

```
d = RegionDistance[line, c]

```

> 
> $\sqrt{\frac{5}{6}}$
> 
> 
> 

and

```
2 Sqrt[R^2 - d^2]

```

> 
> $\sqrt{\frac{38}{3}}$
> 
> 
> 

```
N@%

```

> 
> 3.55903
> 
> 
> 

or

```
EuclideanDistance @@ Catenate[List @@ pts] // FullSimplify

```

> 
> $\sqrt{\frac{38}{3}}$
> 
> 
>