This is kind of a geometrical question. For my program I want to compute the minimal distance $r$ from a given point to the cube. Here is a drawing which shows what I mean:
I have two vectors $\vec{p}$ and $\vec{q}$ which indicate the position of my two points. Point $p$ can be anywhere outside the cube. Point $q$ is exactly in the middle of the cube. The distance from point $q$ to the cubes surface is always $d$. I can easily compute $R$ which is the distance from point $q$ to point $p$. But what I need is the minimal distance $r$ from point $p$ to the cube. I am sure that I have to distinguish several cases depending on where point $p$ is located. I think there are three cases:
1) The minimal distance $r$ from point $p$ is to the edge of the cube (as drawn in the picture)
2) The minimal distance $r$ from point $p$ is to the corner of the cube
3) The minimal distance $r$ from point $p$ is to the surface of the cube
After hours of trying to find a nice solution I hope someone can give me a hint.
