---
title: "Expand a 3D bounding box by a margin in C#"
url: https://recursiveai.org/onemath/cookbook/inflate-axis-aligned-bounding-box/
description: "Use OneMath to pad all six faces of a 3D axis-aligned bounding box by the same margin in C#. See the new corners and verify the center stays put."
version: "1.0.0"
---

# Expand a 3D bounding box by a margin in C#

**OneMath** is a free, open-source deterministic math library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneMath) · [GitHub](https://github.com/recursive-ai-oss/onemath-csharp) · [Docs](https://recursiveai.org/onemath/docs/) · [Download example (.zip)](https://recursiveai.org/onemath/cookbook/inflate-axis-aligned-bounding-box/inflate-axis-aligned-bounding-box.zip)

Use `Inflate` to pad all six faces of an axis-aligned box without moving its center. Pass the minimum corner first and the maximum corner second to `Bounds3D`.

```shell
dotnet add package RecursiveAI.OneMath
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneMath;

var box = new Bounds3D(new(1, 2, 3),
                       new(5, 8, 11));
var padded = box.Inflate(0.5);
Console.WriteLine(padded);
```

Unzip [the example](https://recursiveai.org/onemath/cookbook/inflate-axis-aligned-bounding-box/inflate-axis-aligned-bounding-box.zip), then `dotnet run`.

Prints the new minimum and maximum corners.

```text
[(0.5, 1.5, 2.5) .. (5.5, 8.5, 11.5)]
```

## The full program

```csharp
using RecursiveAI.OneMath;

var box = new Bounds3D(new(1, 2, 3),
                       new(5, 8, 11));
var padded = box.Inflate(0.5);
Console.WriteLine(padded);
Console.WriteLine($"Original center: {box.Center}");
Console.WriteLine($"Padded center: {padded.Center}");
Console.WriteLine($"Padded size: {padded.Size}");
```

```text
[(0.5, 1.5, 2.5) .. (5.5, 8.5, 11.5)]
Original center: (3, 5, 7)
Padded center: (3, 5, 7)
Padded size: (5, 7, 9)
```

`Inflate` doesn’t reorder reversed corners. Check `IsEmpty` if the input may be invalid or the margin is negative.

## Variations

### Shrink the box instead

Append this to inset every face by one unit. It also shows what happens when the inset exceeds half the original width.

```csharp
var inset = box.Inflate(-1);
Console.WriteLine(inset);
Console.WriteLine($"Empty after -3: {box.Inflate(-3).IsEmpty}");
```

```text
[(2, 3, 4) .. (4, 7, 10)]
Empty after -3: True
```

## Related recipes

- [Calculate an axis-aligned bounding box](https://recursiveai.org/onemath/cookbook/calculate-axis-aligned-bounding-box/)
- [Test 3D AABB overlap](https://recursiveai.org/onemath/cookbook/test-3d-aabb-overlap/)
- [Merge axis-aligned bounding boxes](https://recursiveai.org/onemath/cookbook/merge-axis-aligned-bounding-boxes/)

## The short answer

Call `box.Inflate(margin)` on a `Bounds3D` and keep the returned box. A margin of `0.5` subtracts `0.5` from each minimum coordinate and adds `0.5` to each maximum coordinate. That gives half a unit of padding **per face**, so width, height, and depth each grow by one unit. The original box is unchanged.

## What is an axis-aligned bounding box?

An [axis-aligned bounding box](https://developer.mozilla.org/en-US/docs/Games/Techniques/3D_collision_detection#axis-aligned_bounding_boxes) is three ranges, one for each coordinate axis. `Min` gives the lower endpoint of each range; `Max` gives its upper endpoint. The box’s faces stay parallel to the coordinate planes even if the enclosed object is rotated. Padding the box changes those endpoints, not the object’s geometry. Use the same coordinate units for the margin and corners, and compare boxes only after putting them in a common coordinate system.

An expanded box can include nearby objects in a first-pass overlap check. A padded overlap does not establish that the objects themselves touch, or that they’re within that distance in every direction. Padding a corner extends the box in multiple axes at once; it does not round the corner into a sphere.

## How do you expand or shrink a bounding box by a margin?

[`Inflate`](https://github.com/recursive-ai-oss/onemath-csharp/blob/1986f32ecb9f5f3001c0121484f104c489720334/src/OneMath/Types/Bounds3D.cs#L114-L119) moves opposing faces equal distances in opposite directions. On X, the example’s range goes from `[1, 5]` to `[0.5, 5.5]`. Its midpoint is 3 in both cases. Y and Z work the same way. If what you have is a requested *total* increase in size, divide it by two before passing it in. The center is unchanged mathematically; arbitrary floating-point coordinates may still round during the arithmetic.

A negative margin moves the faces inward. With a shrink of three units, the sample’s X endpoints become 4 and 2, so `IsEmpty` is true even though the Y endpoints only meet and the Z range still has room. [`IsEmpty`](https://github.com/recursive-ai-oss/onemath-csharp/blob/1986f32ecb9f5f3001c0121484f104c489720334/src/OneMath/Types/Bounds3D.cs#L9-L12) treats equal endpoints as nonempty: they leave a face, line, or point rather than reversing the interval. If your application needs a box with nonzero volume, check that each dimension is positive as well.
