Consider the regular hexagon
of side size equal to $1$, in the $xy$-plane and centered at the origin $(0,0)$.
For a given point $P=(x,y,z)$, how do I find the minimum distance to the boundary of the hexagon? It seems I might need a branching function, any ideas are appreciated.


Iterative solution
In Wolfram Mathematica, defining:
and executing the following simple algorithm:
it's soon achieved as desired:
If then, again by way of example, we re-run the same code with:
we obtain:
while, if we impose:
we obtain:
Of course, if the numerical results are sufficient, in the output phase replace
FullSimplify[]withN[]; the code will be executed more quickly.Analytical solution
In Wolfram Mathematica, defining:
and executing the following simple algorithm:
it's soon achieved as desired:
Of course, if the numerical results are sufficient, in the output phase replace
FullSimplify[]withN[], while in the body of the algorithm replaceSolve[]andIntegrate[]respectively withNSolve[]andNIntegrate[]; the code will be executed more quickly.