Getting the bounding box of a sphere

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?

• Downvoter, please can you explain – user116458 Jun 12 '18 at 10:41
• Doesn't seem very useful to me. – Tyyppi_77 Jun 12 '18 at 10:43
• @Tyyppi_77 I found it hard at first and when I found out I wanted to share my knowlege with someone else – user116458 Jun 12 '18 at 10:49
• @TheMaskedRebel Upvoted for you:), people should appreciate even small things.. – 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);

// then
Vector3 boundingBoxMin = new Vector3(
);
Vector3 boundingBoxMax = new Vector3(
);


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);

// 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.

Diagram!

The following diagram of the 2D scenario might help visualise what's going on 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.

• What does vector - scalar mean? – Tyyppi_77 Jun 12 '18 at 10:43
• @Tyyppi_77 Sorry for the confusion. I edited the post – user116458 Jun 12 '18 at 10:49
• 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. – ErnieDingo Jun 12 '18 at 11:19
• @ErnieDingo so I create a variable which contains 1 / .7071 and then multiply it by the radius before performing the code above – user116458 Jun 12 '18 at 11:25
• first point: center - (radius, radius, radius) * sqrt(1/3), last point: center + (radius, radius, radius) * sqrt(1/3). – Luis Masuelli Jun 12 '18 at 14:35