# Nearest bounding box corner to the camera

I have three coordinates.

// bounding box corners
L3ModelBBoxMin = <-1280,-428,-960>
L3ModelBBoxMax = <1280,0,-160>

// camera location
L3Location = <2413.395250,-1636.367750,-2657.726250>


The "bounding box" is a cuboid that contains a 3D mesh. It has eight corners, like all cuboids. What you see above are the minimum and maximum extents of this box.

My question is, how do I determine the corner of the bounding box that is nearest the camera? Do I subtract the corners from the camera location, and compare the ABS of the results? Thanks.

The sides of the box are parallel to the x, y and z planes.

• You can compute the eight corners of the box, then use the distance formula David G. Stork provided below. Feb 27, 2018 at 1:17

$D(p_1,p_2) = \sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2 + (z_1 - z_2)^2}$.

Assuming the cuboid is aligned with the $x,y,z$-axes, you don't need to use the distance formula. Just check whether the $x$-coördinate of the camera is greater or lesser than the midpoint of the cuboid in the $x$ direction, then do the same for $y$ and $z$.

In your example, the $x$-midpoint is $\frac{-1280+1280}2 = 0$, so since the camera has a positive $x$-coördinate, it is closer to the right side of the cuboid, not the left. Similar calculations apply for $y$ and $z$.

This is much more efficient than using the distance formula, since it avoids three multiplications and a square root.

Since you only give the minimum and maximum corners, I'm going to assume that the edges of the bounding box are aligned with the $x,y,z$ axes of the simulation environment. This allows for a simpler and faster calculation that David G. Stork's correct answer.

Define the midpoint between the min and max corner: \begin{align} x_{mid} = &(x_{min} + x_{max})/2, \\ y_{mid} = &(y_{min} + y_{max})/2, \\ z_{mid} = &(z_{min} + z_{max})/2 \\ \end{align}

Now, you just need to do three comparison. If $x_{camera} > x_{mid}$, then the camera is closer to the box points with $x = x_{max}$. Otherwise, it is closer to the box points with $x = x_{min}$. Do similarly for $y$ and $z$.