# How to find the shortest distance between two regions?

I need to find the shortest distance between the two regions and the two points of the shortest distance, but the following code cannot achieve this requirement:

reg1 = ImplicitRegion[
x^2 + y^2 - 2 z^2 == 0 && x + y + 3 z == 5, {x, y, z}]
reg2 = ImplicitRegion[z == 0, {x, y, z}]
RegionNearest[reg1, reg2]


What can I do to find the shortest distance between two regions?

• Here's an idea, which does not want to finish evaluating so only as a comment: reg1df = RegionDistance@reg1; NMinimize[ reg1df[{x, y, z}], {x, y, z} \[Element] reg2]
– Kuba
Commented Aug 14, 2020 at 8:43

Lagrange multiplier method is a alternative solution which can be solve by hand.

    g = z + p (x^2 + y^2 - 2 z^2) + q (x + y + 3 z - 5);
Solve[Grad[g, {x, y, z, p, q}] == {0, 0, 0, 0, 0}]

(* {{p -> -(1/10), q -> -1, x -> -5, y -> -5, z -> 5}, {p -> 1/10,
q -> -(1/5), x -> 1, y -> 1, z -> 1}} *)

• Up to en.wikipedia.org/wiki/Lagrange_multiplier , "The fact that solutions of the Lagrangian are not necessarily extrema also poses difficulties for numerical optimization.". The Karush–Kuhn–Tucker conditions en.wikipedia.org/wiki/Karush–Kuhn–Tucker_conditions are more appropriate to this end. Commented Aug 14, 2020 at 14:37

This can be done as follows.

reg1=ImplicitRegion[x^2+y^2-2 z^2==0&&x+y+3 z==5,{x,y,z}];
reg2=ImplicitRegion[z==0,{x,y,z}];Minimize[{Norm[{a,b,c}-{s,t,r}],{a,b,c}\[Element]reg1&&
{s,t,r}\[Element]reg2},{a,b,c,s,t,r}]
(*{1, {a -> 1, b -> 1, c -> 1, s -> 1, t -> 1, r -> 0}}*)