---
title: "Combine two AABB bounding boxes in C#"
url: https://recursiveai.org/onemath/cookbook/merge-axis-aligned-bounding-boxes/
description: "Combine two axis-aligned bounding boxes in C# with Bounds3D.Union. Print the enclosing box’s minimum and maximum and keep overlap tests separate."
version: "1.0.0"
---

# Combine two AABB bounding boxes 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/merge-axis-aligned-bounding-boxes/merge-axis-aligned-bounding-boxes.zip)

An axis-aligned bounding box (AABB) can enclose two child boxes even if they don’t touch.

```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 left = new Bounds3D(new(1, 2, 3),
                        new(2, 4, 5));
var right = new Bounds3D(new(5, 1, 4),
                         new(6, 3, 7));
var parent = Bounds3D.Union(left, right);
Console.WriteLine($"Min: {parent.Min}");
Console.WriteLine($"Max: {parent.Max}");
```

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

```text
Min: (1, 1, 3)
Max: (6, 4, 7)
```

## The full program

```csharp
using RecursiveAI.OneMath;
var left = new Bounds3D(new(1, 2, 3),
                        new(2, 4, 5));
var right = new Bounds3D(new(5, 1, 4),
                         new(6, 3, 7));
var parent = Bounds3D.Union(left, right);
Console.WriteLine($"Min: {parent.Min}");
Console.WriteLine($"Max: {parent.Max}");

Console.WriteLine($"Children overlap: {left.Intersects(right)}");
Console.WriteLine($"Parent contains both: {parent.Contains(left) && parent.Contains(right)}");
```

```text
Min: (1, 1, 3)
Max: (6, 4, 7)
Children overlap: False
Parent contains both: True
```

If an input’s `Min` is greater than its `Max` on any axis, the box is empty (`IsEmpty` is `true`). Fix or skip reversed bounds before calling `Union`: it uses the corners as given, so a reversed box can still enlarge the result.

## Variations

To combine a list of children, append this fold to the snippet at the top.

```csharp
var combined = Bounds3D.Empty;
foreach (var child in new[] { left, right })
    combined = Bounds3D.Union(combined, child);
Console.WriteLine($"Combined: {combined}");
```

```text
Combined: [(1, 1, 3) .. (6, 4, 7)]
```

## 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/)

## The short answer

Call `Bounds3D.Union(left, right)` on two axis-aligned boxes in the same coordinate system. For each axis, the parent’s minimum is the lesser of the two child minima, and its maximum is the greater of the two child maxima. The result is one enclosing box, even when the inputs are disjoint.

## How do you calculate a bounding box for multiple objects?

Compute each object’s bounds in the same coordinate system, then merge them one at a time. An AABB is three intervals, one each along X, Y, and Z. The X interval in this example starts at the left child’s minimum of 1 and ends at the right child’s maximum of 6. Y starts at the right child’s minimum of 1 and ends at the left child’s maximum of 4. The Z interval runs from 3 to 7.

Start an accumulator with [`Bounds3D.Empty`](https://github.com/recursive-ai-oss/onemath-csharp/blob/1986f32ecb9f5f3001c0121484f104c489720334/src/OneMath/Types/Bounds3D.cs#L25-L35) so the first box supplies all six limits. Starting with `default(Bounds3D)` makes both corners `(0, 0, 0)`: that’s a box containing the origin, so it drags the parent toward the origin even if all the objects are somewhere else. If there are no children, the accumulator remains empty.

## Does an AABB union tell you whether the boxes overlap?

No. In the example, neither child contains `(3, 2, 4)`, but the parent does. A single enclosing box spans the gap along X. It also encloses space elsewhere that isn’t part of either child. That’s useful for grouping objects in a spatial tree, but it isn’t a geometric union of their occupied regions or a test that they collide.

Use [`Intersects` or `Intersection`](https://github.com/recursive-ai-oss/onemath-csharp/blob/1986f32ecb9f5f3001c0121484f104c489720334/src/OneMath/Types/Bounds3D.cs#L99-L109) for different questions: `Intersects` tells you whether the boxes share a point, including contact at a boundary; `Intersection` gives you their shared box, empty when they’re separated. Use `Union` when what you need is the parent’s outer limits.
