TITLE: L1 distance to L2 sphere
QUESTION [8 upvotes]: This may seem like a strange question, but I have been spending quite some time trying to find the answer to the following problem:

Given a Euclidean (L2) unit sphere and a point $p$, what is the minimum L1 distance of $p$ to that sphere?

I have had quite some success for circles, and the formulas tend to be elegant and simple. For instance, for points whose $x$ and $y$ coordinates are both outside of the range $[-1,1]$, the distance is simply $|x|+|y|-\sqrt2$. I am wondering if there are similarly elegant formulas in 3D. However, this is where my math skills are reaching their limits.
(To arrive at the 2D formulas, I minimized $|\cos\theta-x|+|\sin\theta-y|$ for $\theta$ to varying degrees of success. Depending on $x$ and $y$, the curve can become quite scary).
If you are wondering what this is for: I am writing a somewhat unconventional ray tracer that is using a discrete space. The L1 distance makes things a lot easier for many things, but as you may imagine circles and spheres take some extra tinkering.

REPLY [4 votes]: It helps to look at the general case in two dimensions first, where the L1 circle $\Gamma_1$ is a diamond.

Suppose $|x|,|y|\ge\frac{\sqrt2}2$, i.e. $p=(x,y)$ is in the shaded area above. Then $\Gamma_1$ around $p$ can be enlarged until it is tangent to the L2 circle $\Gamma_2$ at $\left(\frac{\sqrt2}2,\frac{\sqrt2}2\right)$, which makes the minimal L1 distance $d=|x|+|y|-\sqrt2$. Otherwise, $\Gamma_1$ can only meet $\Gamma_2$ at a point, and simple geometry shows that $d=z_+-\sqrt{1-z_-^2}$ where $z_+=\max(|x|,|y|)$ and $z_-=\min(|x|,|y|)$.
For three dimensions, $\Gamma_1$ is an octahedron, but a similar idea applies. If $|x|,|y|,|z|\ge\frac1{\sqrt3}$, $\Gamma_1$ can be enlarged until one of its faces is tangent to $\Gamma_2$ at $\left(\frac1{\sqrt3},\frac1{\sqrt3},\frac1{\sqrt3}\right)$. Then
$$d=|x|+|y|+|z|-\sqrt3$$
Now suppose $|z|\le\frac1{\sqrt3}$. $\Gamma_1$ can now only meet $\Gamma_2$ at an edge or a point. If we slice the entire set-up perpendicular to the $z$-axis at the given $z$, we get a scaled version of the two-dimensional case!

In conclusion, to find the minimal L1 distance $d$, flip and swap the coordinate axes so that $x\ge y\ge z\ge0$. Then
$$d=\begin{cases}
x+y+z-\sqrt3&\text{if }z\ge\frac1{\sqrt3}\\
x+y-\sqrt2\sqrt{1-z^2}&\text{if }z<\frac1{\sqrt3}\text{ and }y\ge\frac{\sqrt2}2\sqrt{1-z^2}\\
x-\sqrt{1-y^2-z^2}&\text{otherwise}
\end{cases}$$
This result naturally generalises to higher dimensions.