Shortest distance between a point inside a cube and the surface of the cube

geometry

Given an axis aligned 3D rectangle with vertex (x1, y1, z1) and opposite vertex (x2, y2, z2), how do I find the shortest distance between a point inside the prism and the surface of the prism?

Best Answer

It sounds as if you're describing a prism whose set of vertices is $$\{x_1,x_2\}\times\{y_1,y_2\}\times\{z_1,z_2\}.$$ Put another way, the vertices are the eight points $(x,y,z)$ such that $x=x_i,$ $y=y_j,$ and $z=z_k$ for some $i,j,k$ chosen from the set $\{1,2\}.$ I assume further that $x_1\neq x_2,$ $y_1\neq y_2,$ and $z_1\neq z_2,$ or there will be no "inside" to speak of.

If that's correct, then its faces are rectangles in one of the six following planes:

  1. $x=x_1,$
  2. $x=x_2,$
  3. $y=y_1,$
  4. $y=y_2,$
  5. $z=z_1,$
  6. $z=z_2.$

Choosing an arbitrary point $(x,y,z)$ inside the prism, the distance to the surface will necessarily be along a line segment from $(x,y,z)$ to the nearest face (or one of the nearest faces, in case of a tie), and so to (one of) the nearest of the six planes above. Such a line segment will be orthogonal to said plane, so for example, the line segment orthogonal to plane number 5 goes from $(x,y,z)$ to $(x,y,z_1),$ so the distance to that plane is $|z-z_1|.$

Thus, to determine the shortest distance to the surface of the prism, we need only determine the shortest distance to one of the six planes from $(x,y,z),$ that is: $$\min\bigl(|x-x_1|,|x-x_2|,|y-y_1|,|y-y_2|,|z-z_1|,|z-z_2|\bigr).$$