Initializing 3D Canvas...

The Box

1 min read1 page

Axis Aligned Bounding Box

In computational geometry, a Box is one of the simplest ways to represent the volume occupied by an object. It becomes most useful when we are not interested in the complex geometry of the object, but we only need to know its extent. In these cases, it is a 3D rectangular prism whose faces are perfectly aligned with the X, Y, and Z axes.

It is the primary tool for performance optimization. Computers can check if a ray hits a box millions of times faster than checking if it hits a complex mesh.

The Quick Filter: Before a computer does expensive math, it first checks: "Does the light even hit the Box enclosing the mesh?" If the answer is No, the computer skips the expensive math entirely.
1 min read1 page

Data Structure

A Box is incredibly memory-efficient. You do not need to store 8 vertices or 6 faces. Mathematically, it is defined entirely by just two data points. There are two common ways to store this in memory:

python
1class Box_MinMax:
2 # Stored as two opposite corners
3 def __init__(self, min_pt: Point3d, max_pt: Point3d):
4 self.Min = min_pt
5 self.Max = max_pt
6
7class Box_CenterExtents:
8 # Stored as center point and half-size vector
9 def __init__(self, center: Point3d, extents: Vector3d):
10 self.Center = center
11 self.Extents = extents
Min/Max is great for combining boxes (union) and UI layouts. Center/Extents is standard in Physics Engines (Collision Detection) because it makes symmetry math and sphere-intersection much simpler.
Switch Representation
2 min read1 page

Diagonal / Center:

The Diagonal is a vector from the Min corner to the Max corner — its X, Y, Z components give the width, height, and depth of the box. The Center is simply (Min + Max) / 2. These are the most commonly used properties in parametric scripts for sizing, spacing, and centering geometry.
python
1def GetBoxExtents(box: BoundingBox) -> tuple:
2 """Returns the width, height, and depth of the bounding box."""
3
4 diagonal = box.Diagonal # Vector3d from Min to Max
5
6 width = diagonal.X # Extent in X
7 height = diagonal.Y # Extent in Y
8 depth = diagonal.Z # Extent in Z
9 center = box.Center # Midpoint between Min and Max
10
11 return (width, height, depth, center)
Width (X)
3.00
Height (Y)
2.00
Depth (Z)
4.00
2 min read1 page

Box Metrics:

Volume and Surface Area are basic properties of a bounding box. Surface Area is particularly important when constructing Bounding Volume Hierarchies (BVH), where it is used in the Surface Area Heuristic (SAH) to determine optimal splits.
python
1def SurfaceArea(box: BoundingBox) -> float:
2 dx = box.Max.X - box.Min.X
3 dy = box.Max.Y - box.Min.Y
4 dz = box.Max.Z - box.Min.Z
5 return 2.0 * (dx*dy + dy*dz + dx*dz)
6
7def Volume(box: BoundingBox) -> float:
8 dx = box.Max.X - box.Min.X
9 dy = box.Max.Y - box.Min.Y
10 dz = box.Max.Z - box.Min.Z
11 return dx * dy * dz
The math is extremely fast since it only relies on the differences between the Min and Max coordinates.
Scale Box
1.00
3 min read1 page

BoxCorners:

Expands the abstract Min/Max values into the actual 8 physical points in 3D space.
python
1def BoxCorners(bbox: 'BoundingBox') -> list['Point3d']:
2 """Returns the 8 corner points of the bounding box in canonical order."""
3
4 # Bottom face (Z = Min.Z), Counter-Clockwise from (Min.X, Min.Y)
5 c0 = Point3d(bbox.Min.X, bbox.Min.Y, bbox.Min.Z)
6 c1 = Point3d(bbox.Max.X, bbox.Min.Y, bbox.Min.Z)
7 c2 = Point3d(bbox.Max.X, bbox.Max.Y, bbox.Min.Z)
8 c3 = Point3d(bbox.Min.X, bbox.Max.Y, bbox.Min.Z)
9
10 # Top face (Z = Max.Z), Counter-Clockwise from (Min.X, Min.Y)
11 c4 = Point3d(bbox.Min.X, bbox.Min.Y, bbox.Max.Z)
12 c5 = Point3d(bbox.Max.X, bbox.Min.Y, bbox.Max.Z)
13 c6 = Point3d(bbox.Max.X, bbox.Max.Y, bbox.Max.Z)
14 c7 = Point3d(bbox.Min.X, bbox.Max.Y, bbox.Max.Z)
15
16 return [c0, c1, c2, c3, c4, c5, c6, c7]
While a bounding box is stored as just two points (`Min` and `Max`), you often need all 8 corners to actually draw the wireframe box, attach structural nodes, or test if a specific corner collides with something else.

The `GetCorners()` method generates these 8 points on demand. They are always returned in a specific order: the bottom 4 points first (counter-clockwise), then the top 4 points.
1 min read1 page

Inflate(dist):

Expands the box in all directions.
python
1class Box:
2 def Inflate(self, distance: float):
3 """Expands the box outward in all directions."""
4 self.Min.X -= distance
5 self.Min.Y -= distance
6 self.Min.Z -= distance
7 self.Max.X += distance
8 self.Max.Y += distance
9 self.Max.Z += distance
Inflate Dist
0.50
1 min read1 page

Move:

Moving a box simply adds a vector to both its Min and Max points. Because the box is axis-aligned, it remains parallel to the world axes.
python
1def Move(self, vector: 'Vector3d'):
2 """Translates the box by moving both Min and Max corners."""
3 self.Min += vector
4 self.Max += vector
Move X
1.60
Move Y
1.50
2 min read1 page

Scale:

Scaling a box multiplies its dimensions relative to a center point. Like translation, this is just simple multiplication of the diagonal extents while maintaining axis-alignment.
python
1def Scale(self, scale_factor: float):
2 """Scales the box relative to its center."""
3
4 # 1. Find the center
5 center = self.Center
6
7 # 2. Scale the distances from center to Min/Max
8 vector_min = (self.Min - center) * scale_factor
9 vector_max = (self.Max - center) * scale_factor
10
11 # 3. Apply new extents
12 self.Min = center + vector_min
13 self.Max = center + vector_max
Scale Factor
1.50
3 min read1 page

Rotate:

A "Pure" Box (AABB) cannot rotate. By definition, its faces must remain parallel to the world axes. If you rotate the geometry inside, you must calculate a new AABB that encloses the rotated shape. This new box is often larger than the original.
python
1def RotateZ(self, angle_rad: float):
2 """Calculate a new bounding box that encloses the rotated shape."""
3 import math
4 cos_a = math.cos(angle_rad)
5 sin_a = math.sin(angle_rad)
6
7 rotated = []
8 for pt in self.GetCorners():
9 # Rotate around Z axis
10 new_x = pt.X * cos_a - pt.Y * sin_a
11 new_y = pt.X * sin_a + pt.Y * cos_a
12 rotated.append((new_x, new_y, pt.Z))
13
14 self.Min.X = min(p[0] for p in rotated)
15 self.Min.Y = min(p[1] for p in rotated)
16 self.Min.Z = min(p[2] for p in rotated)
17
18 self.Max.X = max(p[0] for p in rotated)
19 self.Max.Y = max(p[1] for p in rotated)
20 self.Max.Z = max(p[2] for p in rotated)
This is why "Axis Aligned" is so important for fast collision math — it sacrifices spatial precision for extreme calculation speed.
Angle (°)
45.00
1 min read1 page

Intersection:

Intersection detection between two boxes is the "Hello World" of game physics. It requires only 6 simple comparisons. This is how your mouse knows you've clicked a button on a website, or how a character knows they've hit a wall.
python
1def Intersects(self, other: 'Box') -> bool:
2 """The fast AABB overlap test."""
3 return (self.Min.X <= other.Max.X and self.Max.X >= other.Min.X) and \
4 (self.Min.Y <= other.Max.Y and self.Max.Y >= other.Min.Y) and \
5 (self.Min.Z <= other.Max.Z and self.Max.Z >= other.Min.Z)
Move X
1.00
Move Y
0.50
2 min read1 page

Intersection(a, b):

Axis-Aligned Bounding Box (AABB) intersection is the fastest 3D collision check. By simply comparing the Minimum and Maximum (X, Y, Z) values of two boxes, we can instantly tell if they overlap.
python
1def Intersection(boxA: 'BoundingBox', boxB: 'BoundingBox') -> 'BoundingBox':
2 """Checks if two Axis-Aligned Bounding Boxes intersect."""
3
4 # 1. Simple fast check (AABB algorithm)
5 if (boxA.Max.X < boxB.Min.X or boxA.Min.X > boxB.Max.X or
6 boxA.Max.Y < boxB.Min.Y or boxA.Min.Y > boxB.Max.Y or
7 boxA.Max.Z < boxB.Min.Z or boxA.Min.Z > boxB.Max.Z):
8 return None # No intersection
9
10 # 2. Compute intersection box
11 resMin = Point3d(max(boxA.Min.X, boxB.Min.X),
12 max(boxA.Min.Y, boxB.Min.Y),
13 max(boxA.Min.Z, boxB.Min.Z))
14 resMax = Point3d(min(boxA.Max.X, boxB.Max.X),
15 min(boxA.Max.Y, boxB.Max.Y),
16 min(boxA.Max.Z, boxB.Max.Z))
17
18 return BoundingBox(resMin, resMax)
Move Box B (X)
1.00
Move Box B (Y)
1.00
2 min read1 page

Union(a, b):

Box Union merges two bounding boxes into one larger box that encloses both. This operation is the core primitive for building Bounding Volume Hierarchies (BVH) — the spatial acceleration structure used in ray tracers and game engines to skip millions of intersection tests per frame.
python
1def MergeBoxes(a: 'BoundingBox', b: 'BoundingBox') -> 'BoundingBox':
2 """Returns the smallest box that encloses both A and B."""
3
4 # 1. Find the lowest coordinate on each axis
5 min_pt = Point3d(
6 min(a.Min.X, b.Min.X),
7 min(a.Min.Y, b.Min.Y),
8 min(a.Min.Z, b.Min.Z)
9 )
10
11 # 2. Find the highest coordinate on each axis
12 max_pt = Point3d(
13 max(a.Max.X, b.Max.X),
14 max(a.Max.Y, b.Max.Y),
15 max(a.Max.Z, b.Max.Z)
16 )
17
18 return BoundingBox(min_pt, max_pt)
2 min read1 page

Encapsulate (Grow):

Encapsulate (also known as Extend or Grow) dynamically expands the bounding box to envelop a newly added point.
python
1def Encapsulate(self, pt: Point3d):
2 """Mutates the box by expanding it to include the given point."""
3
4 if pt.X < self.Min.X: self.Min.X = pt.X
5 if pt.Y < self.Min.Y: self.Min.Y = pt.Y
6 if pt.Z < self.Min.Z: self.Min.Z = pt.Z
7
8 if pt.X > self.Max.X: self.Max.X = pt.X
9 if pt.Y > self.Max.Y: self.Max.Y = pt.Y
10 if pt.Z > self.Max.Z: self.Max.Z = pt.Z
This is heavily used when constructing a Bounding Box around a complex mesh or point cloud. You start with an empty or initial box, and iteratively loop through all vertices, calling Encapsulate(vertex) on each one.
Point X
3.00
Point Y
2.50
Point Z
4.00
2 min read1 page

Contains(Point3d):

Checking if a point is contained within an Axis-Aligned Bounding Box is extremely fast because it only requires simple greater-than and less-than comparisons along the X, Y, and Z axes.
python
1def BoxContains(box: 'BoundingBox', pt: 'Point3d', strict: bool) -> bool:
2 """Checks if a point is inside an Axis-Aligned Bounding Box."""
3
4 if strict:
5 # Point must be strictly inside (not on boundaries)
6 return (box.Min.X < pt.X < box.Max.X and
7 box.Min.Y < pt.Y < box.Max.Y and
8 box.Min.Z < pt.Z < box.Max.Z)
9 else:
10 # Point can be on the boundary
11 return (box.Min.X <= pt.X <= box.Max.X and
12 box.Min.Y <= pt.Y <= box.Max.Y and
13 box.Min.Z <= pt.Z <= box.Max.Z)
Point X
-0.30
Point Y
2.50
1 min read1 page

ClosestPoint:

Finding the closest point on a box to an external point is the most efficient search in geometry. It is done by "clamping" the coordinates of the test point to theMin and Max bounds of the box.
python
1def ClosestPoint(box: Box, test_pt: Point3d) -> Point3d:
2 """Clamps a point to the axis-aligned bounds."""
3 x = max(box.Min.X, min(test_pt.X, box.Max.X))
4 y = max(box.Min.Y, min(test_pt.Y, box.Max.Y))
5 z = max(box.Min.Z, min(test_pt.Z, box.Max.Z))
6 return Point3d(x, y, z)
Search Pt X
-3.50
Search Pt Y
1.50
2 min read1 page

DistanceTo:

Calculates the shortest distance from a test point to the surface of the bounding box. If the point is inside the box, the distance is typically considered to be 0.
python
1def DistanceTo(box: BoundingBox, test_pt: Point3d) -> float:
2 """Returns the distance from the point to the closest surface of the box."""
3
4 # 1. First find the closest point on the box
5 closest = box.ClosestPoint(test_pt)
6
7 # 2. Return distance between test point and closest point
8 return test_pt.DistanceTo(closest)
Because we already know how to find the Closest Point efficiently by clamping coordinates, calculating the distance is simply a matter of measuring the length between the test point and that clamped point.
Point X
3.50
Point Y
2.50
4 min read1 page

Ray Intersection:

Ray-Box intersection is typically computed using the Slab method. The algorithm considers the bounding box as the intersection of three pairs of parallel planes (slabs) and finds the interval along the ray where it is inside all three slabs.
python
1def RayIntersectsBox(ray_origin: Point3d, ray_dir: Vector3d, box_min: Point3d, box_max: Point3d) -> bool:
2 """Uses the Slab method to test if a ray intersects an AABB."""
3 t_min = float('-inf')
4 t_max = float('inf')
5
6 # Check X axis
7 if abs(ray_dir.X) < 1e-6:
8 if ray_origin.X < box_min.X or ray_origin.X > box_max.X: return False
9 else:
10 t1 = (box_min.X - ray_origin.X) / ray_dir.X
11 t2 = (box_max.X - ray_origin.X) / ray_dir.X
12 t_min = max(t_min, min(t1, t2))
13 t_max = min(t_max, max(t1, t2))
14
15 # Check Y axis
16 if abs(ray_dir.Y) < 1e-6:
17 if ray_origin.Y < box_min.Y or ray_origin.Y > box_max.Y: return False
18 else:
19 t1 = (box_min.Y - ray_origin.Y) / ray_dir.Y
20 t2 = (box_max.Y - ray_origin.Y) / ray_dir.Y
21 t_min = max(t_min, min(t1, t2))
22 t_max = min(t_max, max(t1, t2))
23
24 # Check Z axis
25 if abs(ray_dir.Z) < 1e-6:
26 if ray_origin.Z < box_min.Z or ray_origin.Z > box_max.Z: return False
27 else:
28 t1 = (box_min.Z - ray_origin.Z) / ray_dir.Z
29 t2 = (box_max.Z - ray_origin.Z) / ray_dir.Z
30 t_min = max(t_min, min(t1, t2))
31 t_max = min(t_max, max(t1, t2))
32
33 return t_max >= t_min and t_max >= 0
Ray Target X
0.50
Ray Target Z
0.00
3 min read1 page

Plane Intersection:

To check if an infinite plane slices through a bounding box, we can calculate the box's projection onto the plane's normal vector. If the distance from the box's center to the plane is less than or equal to this projected radius, they intersect.
python
1def PlaneIntersectsBox(plane_origin: Point3d, plane_normal: Vector3d, box_min: Point3d, box_max: Point3d) -> bool:
2 """Checks if a plane intersects an AABB by projecting box extents onto the plane normal."""
3 center = Point3d((box_min.X + box_max.X)*0.5, (box_min.Y + box_max.Y)*0.5, (box_min.Z + box_max.Z)*0.5)
4 extents = Vector3d((box_max.X - box_min.X)*0.5, (box_max.Y - box_min.Y)*0.5, (box_max.Z - box_min.Z)*0.5)
5
6 # Project the half-diagonal onto the normal
7 r = extents.X * abs(plane_normal.X) + extents.Y * abs(plane_normal.Y) + extents.Z * abs(plane_normal.Z)
8
9 # Distance from box center to plane
10 s = (center.X - plane_origin.X) * plane_normal.X + (center.Y - plane_origin.Y) * plane_normal.Y + (center.Z - plane_origin.Z) * plane_normal.Z
11
12 # Intersection occurs if the center is closer to the plane than the projected extents
13 return abs(s) <= r
Move Plane
0.00
1 min read1 page

Contains Box:

To check if one bounding box fully encapsulates another, we simply verify that the inner box's minimum is greater than or equal to the outer box's minimum, and its maximum is less than or equal to the outer box's maximum across all axes.
python
1def ContainsBox(outer: 'BoundingBox', inner: 'BoundingBox') -> bool:
2 """Checks if the outer box completely encapsulates the inner box."""
3 return (outer.Min.X <= inner.Min.X and
4 outer.Min.Y <= inner.Min.Y and
5 outer.Min.Z <= inner.Min.Z and
6 outer.Max.X >= inner.Max.X and
7 outer.Max.Y >= inner.Max.Y and
8 outer.Max.Z >= inner.Max.Z)
Move Inner Box X
0.00
Move Inner Box Y
0.00