Shortest distance between rectangular cuboid and point in 3d space

1.1k Views Asked by At

I was wondering about how to determine the shortest distance between a point and a rectangular cuboid.

1

There are 1 best solutions below

0
On BEST ANSWER

If necessary, start with a rotation so the cuboid is oriented parallel to the coordinate axes. Say the cuboid is $a_1 \le x \le a_2$, $b_1 \le y \le b_2$, $c_1 \le z \le c_2$, and your point is $(u,v,w)$. You want to find the point $(X,Y,Z)$ in the cuboid closest to your point. There are three cases for each of the coordinates.

  1. If $u < a_1$, then $X = a_1$.
  2. If $a_1 \le u \le a_2$, then $X = u$.
  3. If $u > a_2$, then $X = a_2$.

Similarly for $Y$ and $Z$. Then the distance is $\sqrt{(X-u)^2 + (Y-v)^2 + (Z-w)^2}$.