---
title: "Calculate an axis-aligned bounding box in C#"
url: https://recursiveai.org/onemath/cookbook/calculate-axis-aligned-bounding-box/
description: "Calculate an axis-aligned bounding box in C# with OneMath: runnable .NET examples, real output and a downloadable project. Free and open source."
version: "1.0.0"
---

# Calculate an axis-aligned bounding box 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/calculate-axis-aligned-bounding-box/calculate-axis-aligned-bounding-box.zip)

An axis-aligned bounding box encloses points with faces parallel to the coordinate axes.

```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 = Bounds3D.CreateFromPoints([
    new(-2, 1, 0), new(4, 5, 6)]);
Console.WriteLine(box.Center);
Console.WriteLine(box.Extents);
```

```text
(1, 3, 3)
(3, 2, 3)
```

`Extents` are the half-sizes along X, Y and Z, not the full side lengths.

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

## The full program

```csharp
using RecursiveAI.OneMath;

Vector3D[] points = [new(-2, 1, 0), new(4, 5, 6), new(0, 3, 2)];
var box = Bounds3D.CreateFromPoints(points);
Console.WriteLine($"Min: {box.Min}");
Console.WriteLine($"Max: {box.Max}");
Console.WriteLine($"Center: {box.Center}");
Console.WriteLine($"Extents: {box.Extents}");
Console.WriteLine($"Volume: {box.Volume()}");
var empty = Bounds3D.CreateFromPoints([]);
Console.WriteLine($"Empty input: {empty.IsEmpty}");
Console.WriteLine($"Empty volume: {empty.Volume()}");
```

`dotnet run` prints:

```text
Min: (-2, 1, 0)
Max: (4, 5, 6)
Center: (1, 3, 3)
Extents: (3, 2, 3)
Volume: 144
Empty input: True
Empty volume: 0
```

Empty input returns `Bounds3D.Empty`, not an exception; check `IsEmpty` before reading the center or extents.

## Variations

Accumulate points without storing the entire point set:

```csharp
using RecursiveAI.OneMath;

var box = Bounds3D.Empty;
foreach (var point in new Vector3D[] {
    new(-2, 1, 0), new(4, 5, 6) })
    box = box.Encapsulate(point);
Console.WriteLine(box.Min);
Console.WriteLine(box.Max);
```

```text
(-2, 1, 0)
(4, 5, 6)
```

## Related recipes

- [Invert a matrix](https://recursiveai.org/onemath/cookbook/invert-matrix/)
- [Vector and quaternion math](https://recursiveai.org/onemath/cookbook/vector-and-quaternion-math/)
- [Convert System.Numerics conventions](https://recursiveai.org/onemath/cookbook/port-from-system-numerics/)

## The short answer

Use `Bounds3D.CreateFromPoints(points)` to calculate an axis-aligned bounding box (AABB) from 3D points in C#. The result stores the smallest and largest coordinates in `Min` and `Max`. Read `Center` for the midpoint and `Extents` for the half-size along each axis. For single-precision input, use `Bounds3F` and `Vector3F` instead.

The example’s third point lies inside the box, so adding it does not change the corners. The [bounds source](https://github.com/recursive-ai-oss/onemath-csharp/blob/1986f32ecb9f5f3001c0121484f104c489720334/src/OneMath/Types/Bounds3D.cs) defines the construction and empty-input behavior.

## How do you calculate an axis-aligned bounding box from points in C#?

For each axis, keep the minimum and maximum coordinate seen so far. OneMath does this in input order, starting with its empty box and calling `Encapsulate` for each point. The method accepts a `ReadOnlySpan<Vector3D>`, so an array can be passed directly without building a separate collection of corners.

For the program’s points, the X range is −2 to 4, Y is 1 to 5, and Z is 0 to 6. The full size is therefore `(6, 4, 6)`. Extents are half that size: `(3, 2, 3)`. Center is computed as `Min + (Max - Min) * 0.5`, giving `(1, 3, 3)`. Extents are not full side lengths; confusing the two doubles the size when reconstructing a box.

If you already know a center and half-sizes, `CreateFromCenterExtents` is the corresponding factory. The two-corner constructor is different: it keeps the supplied corners as given and does not sort them. Use `CreateFromPoints` for unordered corners rather than assuming the constructor will repair their order.

## How do you grow a bounding box one point at a time?

Use the variation above: start from `Bounds3D.Empty` and assign the result of `Encapsulate` back to the variable. Bounds are immutable values; calling the method without keeping its return value does not grow the box. This also lets you accumulate bounds without retaining every input point.

Do not start with `default(Bounds3D)` unless you intend to include the origin. Default fields are zero, so that value represents a zero-size box at the origin, not an empty box. A single-point input similarly produces a nonempty, zero-size box located at that point.

### What happens if there are no points?

`CreateFromPoints` returns `Empty`: every minimum component is positive infinity and every maximum component is negative infinity. `IsEmpty` checks whether any minimum exceeds its matching maximum. `Volume()` and `SurfaceArea()` explicitly return zero for an empty box, but the center and half-sizes are arithmetic expressions over those infinite corners, not usable geometry.

Branch on `IsEmpty` before placing a camera or sizing an object from the result. Keep finite coordinates in point data meant for geometry; the factory is an accumulation operation, not an input-validation layer. The [evaluation-order documentation](https://recursiveai.org/onemath/docs/evaluation-order/) specifies the component operations, including their handling of NaN. If you later serialize bounds to ordinary JSON, handle emptiness separately because its infinite corners cannot be written with default JSON options.
