Given a random point and a cube, how can I determine the distance from that point to the furthest possible point on the cube

3deuclidean-geometrygeometry

Given a random point in space ( $\vec{p}$ ), I am trying to figure out how to calculate the distance to the furthest point on an axis-aligned cube/cuboid from that other point. This cube is defined by two corners which we will call $\vec{c_{l}}$ and $\vec{c_{h}}$. It is guaranteed that $\vec{c_l}$ is the corner on the cube with the lowest values for each of its axes and $\vec{c_h}$ is the corner on the cube with the highest values for each of its axes.

For what it's worth, I've figured out (I think) how to calculate the closest point on the cube to $\vec{p}$. The equation I've figured out for that is: $$min(max(\vec{p}, \vec{c_l}), \vec{c_h})$$ Despite figuring this out, I cannot figure out how to calculate the furthest point (or the distance to it, which is my ultimate goal).

To help illustrate what I am trying to do, here is a diagram I rendered. The red sphere is the closest point on the cube to $\vec{p}$.

the image

Best Answer

Let the cube be defined by

$$x_1<x<x_3, \ \ \ \ y_1<y<y_3, \ \ \ \ z_1<z<z_3.$$

Let $M (x,y,z)$ be the coordinates of the given point.

The farthest point in the cube is necessarily a vertex.

  • First solution : compute the 8 distances to the 8 vertices $(x_i,y_j,z_k)$ and choose the vertex realizing the maximum.

  • Second solution (with much less computation) :

Let $$x_2=(x_1+x_3)/2, \ \ y_2=(y_1+y_3)/2, \ \ z_2=(z_1+z_3)/2$$

Algorithm : Just test the signs of ($(x-x_2),(y-y_2),(z-z_2)$).

Indeed, to each combination of signs $(-,-,-), (-,-,+), \cdots (+,+,+))$ is associated a precise vertex (an association that has to be computed beforehand, resulting in a simple "hash table").