I have a sphere with values center,radius and I need to convert the sphere to a bounding box with values min,max.

How do I convert a sphere into a bounding box?

  • \$\begingroup\$ Downvoter, please can you explain \$\endgroup\$ – user116458 Jun 12 '18 at 10:41
  • 2
    \$\begingroup\$ Doesn't seem very useful to me. \$\endgroup\$ – Tyyppi_77 Jun 12 '18 at 10:43
  • \$\begingroup\$ @Tyyppi_77 I found it hard at first and when I found out I wanted to share my knowlege with someone else \$\endgroup\$ – user116458 Jun 12 '18 at 10:49
  • 2
    \$\begingroup\$ @TheMaskedRebel Upvoted for you:), people should appreciate even small things.. \$\endgroup\$ – isammour Jun 12 '18 at 11:12

Calculating the bounding box of a sphere is pretty trivial given the simplicity of sphere geometry.

Let's assume we have the radius of the sphere defined as a scalar (float or integer) value \$r\$, and the centre of the sphere defined as a vector \$\overrightarrow c\$ like this:

$$ \overrightarrow c = \begin{pmatrix}x \\ y \\ z\end{pmatrix} $$

We can calculate the outer bound coordinate vectors \$\overrightarrow{min}\$ and \$\overrightarrow{max}\$ by doing the following:

$$ \begin{align} \overrightarrow{min} &= \begin{pmatrix} c_x - r \\ c_y - r \\ c_z - r \end{pmatrix} \\ \\ \overrightarrow{max} &= \begin{pmatrix} c_x + r \\ c_y + r \\ c_z + r \end{pmatrix} \end{align} $$

In code, that means:

// given
Vector3 center = new Vector3(10, 20, 30);
int radius = 5;

// then
Vector3 boundingBoxMin = new Vector3(
    center.x - radius,
    center.y - radius,
    center.z - radius
Vector3 boundingBoxMax = new Vector3(
    center.x + radius,
    center.y + radius,
    center.z + radius

If we'd prefer, another way to calculate this same thing is to define a vector \$\overrightarrow {r_{\text{offset}}}\$ for doing that addition, which simply represents the offset from the center to a corner of the bounding box:

$$ \text{given} \; \overrightarrow c = \begin{pmatrix}x \\ y \\ z\end{pmatrix} \; \text{and} \; \overrightarrow {r_{\text{offset}}} = \begin{pmatrix} r \\ r \\ r \end{pmatrix}, \\ \begin{align} \overrightarrow {min} &= \overrightarrow c - \overrightarrow {r_{\text{offset}}} \\ \overrightarrow {max} &= \overrightarrow c + \overrightarrow {r_{\text{offset}}} \end{align} $$

// given
Vector3 center = new Vector3(10, 20, 30);
int radius = 5;
Vector3 radiusOffset = new Vector3(radius, radius, radius);

// then
Vector3 boundingBoxMin = center - radiusOffset;
Vector3 boundingBoxMax = center + radiusOffset;

Handling the 2D case

For the 2D case, calculating the rectangular bounding box of a circle, we omit the Z values like normal: \$\overrightarrow c\$, \$\overrightarrow{r_{\text{offset}}}\$ (if you're using that), \$\overrightarrow{max}\$, and \$\overrightarrow{min}\$ will just be 2D vectors and we'll just do only the x and y calculations.


The following diagram of the 2D scenario might help visualise what's going on here:

enter image description here

One's first instinct might be to calculate a hypotenuse using the Pythagorean theorem (\$\sqrt{x^2 + y^2}\$) and use that as the magnitude of a vector \$\vec h\$, but that's more computationally expensive than necessary: \$\overrightarrow{r_{\text{offset}}}\$ will give us the same result.


Here is something that helped me.

min = (center.x - radius,center.y - radius,center.z - radius); max = (center.x + radius,center.y + radius,center.z + radius)

Hope it helps someone else.

  • \$\begingroup\$ What does vector - scalar mean? \$\endgroup\$ – Tyyppi_77 Jun 12 '18 at 10:43
  • \$\begingroup\$ @Tyyppi_77 Sorry for the confusion. I edited the post \$\endgroup\$ – user116458 Jun 12 '18 at 10:49
  • \$\begingroup\$ You box is contained within the sphere. If you want the bounding box that contains the sphere you need to multiply your radius by 1 / .707 or basically use Pythagoras theorem to get the correct sized bounding box. Hope that helps. Sin of 45 degrees basically. \$\endgroup\$ – ErnieDingo Jun 12 '18 at 11:19
  • \$\begingroup\$ @ErnieDingo so I create a variable which contains 1 / .7071 and then multiply it by the radius before performing the code above \$\endgroup\$ – user116458 Jun 12 '18 at 11:25
  • 1
    \$\begingroup\$ first point: center - (radius, radius, radius) * sqrt(1/3), last point: center + (radius, radius, radius) * sqrt(1/3). \$\endgroup\$ – Luis Masuelli Jun 12 '18 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy