# Finding minimal distance between two surfaces

This code will display two parametric surfaces:

ParametricPlot3D[{
{4 + (3 + Cos[v]) Sin[u], 4 + (3 + Cos[v]) Cos[u], 4 + Sin[v]},
{8 + (3 + Cos[v]) Cos[u], 3 + Sin[v], 4 + (3 + Cos[v]) Sin[u]}
},
{u, 0, 2 Pi},
{v, 0, 2 Pi},
PlotStyle -> {Red, Green}] How to find minimal distance between these surfaces? (u and v do not need to be the same for both surfaces at the point of minimum; points can be chosen independently on two surfaces; in other words, geometric minimum is needed)

I tried to setup NMinimize[], but didn't get meaningful results.

This is inspired by a couple of similar recent questions (here and here), I wanted to know how to apply similar techniques in different circumstances.

Naturally, I would like the method of the solution to be able to work on many other examples of two surfaces, this is just a fairly simple example... For example, if two surfaces intersect or touch, the method should return 0 as for minimal distance, etc... I appreciate your time and effort!

## 2 Answers

eq1 = {4 + (3 + Cos[q]) Sin[p], 4 + Cos[p] (3 + Cos[q]), 4 + Sin[q]};
eq2 = {8 + (3 + Cos[v]) Cos[u], 3 + Sin[v], 4 + (3 + Cos[v]) Sin[u]};
nm  = NMinimize[EuclideanDistance[eq1, eq2], {u, v, p, q}]

(* {2.20785*10^-8, {u -> 2.7672, v -> 3.04956, p -> 1.97302,  q -> 2.31892}} *)


In fact they intersect:

{x0, y0, z0} = eq1 /. nm[];
Show[ParametricPlot3D[eq1, {p, 0, 2 Pi}, {q, 0, 2 Pi}, PlotStyle -> Red,
RegionFunction -> (Norm[{#1, #2, #3} - {x0, y0, z0}] < 2 &)],
ParametricPlot3D[eq2, {u, 0, 2 Pi}, {v, 0, 2 Pi}, PlotStyle -> Green]] • Thanks! I assume that 6.13246*10^-8 means that the surfaces actually touch each other - i am little confused why NMinimize didn't return something closer to 0 or exact 0... – Adrian Nov 17 '14 at 0:39
• @Adrian yep, they intersect. See the plot. – Dr. belisarius Nov 17 '14 at 1:35

If these tori didn't intersect then we would be able to estimate the minimal distance between them with NMinimize, however this is not the case here although the plot in the question makes some kind of confusion. Nevertheless we can prove that they actually intersect with a kind of topological reasoning based on homotopy equivalence.

We define parametrizations of each tori:

torus1[u_, v_] := {4 + (3 + Cos[v]) Sin[u], 4 + (3 + Cos[v]) Cos[u], 4 + Sin[v]}
torus2[u_, v_] := {8 + (3 + Cos[v]) Cos[u], 3 + Sin[v], 4 + (3 + Cos[v]) Sin[u]}


## Continuity Argument

If the both tori were disjoint being entangled then all plane sections parallel to the big circles of the tori would be entangled too, otherwise there must exist certain sections of the tori which are not entangled. Here we provide clear examples of such sections:

   GraphicsRow[
ParametricPlot3D[{ torus1[u1, #1], torus2[u1, #2]}, { u1, 0, 2 π},
PlotStyle -> {{ Thickness[0.01], Darker @ Green},
{ Thickness[0.01], Red}},
ViewPoint -> #3, PlotLabel -> #4]& @@@ {
{ π, 3 π/2, {16, 4, 6}, "Unentangled"},
{ π/2, 3 π/2, {6, 4, 6}, "Entangled"}}, ImageSize -> 600] In fact this argument is a straightforward proof that the distance of the tori is zero since these two sections are homotopically equivalent thus there must exist a parameter when the both sections intersect.

Working with NMinimize we can estimate reliably an upper bound for the minimum when the both tori are disjoint, however if they are not then NMinimize will only suggest it yielding a very small number such as below.

We can use NMinimize with Norm:

NMinimize[ Join[ { Norm[torus1[u1, v1] - torus2[u2, v2]],
Thread[0 <= # <= 2 π &[{u1, v1, u2, v2}]]}],
{u1, v1, u2, v2}]


or with EuclideanDistance:

NMinimize[ Join[{ EuclideanDistance[ torus1[u1, v1], torus2[u2, v2]],
Thread[0 <= # <= 2 π &[{u1, v1, u2, v2}]]}],
{u1, v1, u2, v2}]

{1.30702*10^-7, {u1 -> 2.43975, v1 -> 4.16231, u2 -> 3.48281, v2 -> 4.24283}}

• Thanks for the answer, Artes. I believe, at the first sight, that your solution assumes that u and v are the same on both surfaces. However, I removed that condition in the statement, so the result should be different. However, +1 anyway! Please keep the answer, since it is useful, even if I am right in this comment. – Adrian Nov 17 '14 at 0:34
• @Adrian Now this is not only useful but mathematically correct reasoning unlike in the other answer. – Artes Nov 17 '14 at 14:04
• Right, my first comment referred to the first version of your answer, where only u and v were used, but now u1, v1, u2, v2 are used, which is correct, so the comment is now obsolete, but the reader would understand that from the whole discussion. – Adrian Nov 17 '14 at 14:13
• @Artes this is a good exposition. Note however, that the "entanglement" depends on the embedding. It's pointless to talk about tori that are "entagled" unless embedded in $\mathbb{R}^3$ much like it's pointless (no pun intended) to talk about a point being "inside" a circle unless you embed said point and circle in $\mathbb{R} ^2.$ – gpap Nov 21 '14 at 11:11
• @gpap Thanks, I guess it it clear from the context however it would be more precise to use the term "entanglment" as a metaphore. Sometimes one has to use inaccurate terms expressing simple facts. – Artes Nov 21 '14 at 11:38