0% found this document useful (0 votes)
160 views5 pages

A Simple Method For Box-Sphere Intersection Testing

This document describes algorithms for determining if a box intersects a sphere in n-dimensional space. It presents algorithms to test the intersection of solid and hollow versions of boxes and spheres. The algorithms work by finding the closest and farthest points from the sphere center to the box boundaries and comparing the distances to the sphere radius. The methods generalize to axis-aligned ellipsoids by replacing the radius with scaled values along each axis.

Uploaded by

yy y
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
160 views5 pages

A Simple Method For Box-Sphere Intersection Testing

This document describes algorithms for determining if a box intersects a sphere in n-dimensional space. It presents algorithms to test the intersection of solid and hollow versions of boxes and spheres. The algorithms work by finding the closest and farthest points from the sphere center to the box boundaries and comparing the distances to the sphere radius. The methods generalize to axis-aligned ellipsoids by replacing the radius with scaled values along each axis.

Uploaded by

yy y
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

V.

8 A SIMPLE METHOD FOR BOX-SPHERE INTERSECTION TESTING

V.8
A SIMPLE METHOD FOR
B OX – SPHERE
I NTERSECTION TESTING
James Arvo
Apollo Systems Division of Hewlett-Packard
Chelmsford, Massachusetts

Introduction
There are a number of computer graphics applications in which it is
necessary to determine whether a sphere intersects an axis-aligned paral-
lelepiped, or “box.” In two dimensions this arises when rasterizing
circles, which are two-dimensional spheres, into rectangular viewports,
which are two-dimensional boxes. The intersection test is used to deter-
mine whether any portion of the circle is indeed visible before rasterizing.
In three dimensions this operation may arise in spatial subdivision tech-
niques that identify “voxels,” or 3D boxes, pierced by the surfaces of
various objects. If spheres are among the objects, or are used as bounding
volumes for other objects, we need to test for 3D box-sphere intersec-
tion.
This note describes a simple method for detecting intersections of
n-dimensional boxes with n-dimensional spheres taken as either surfaces
or solids. Applications for n > 3 are not addressed here, though the
algorithm works in any dimension. For greatest generality all algorithms
shown below take the dimension of the space, n, as an input parameter,
though typical implementations would be optimized for either two or
three dimensions.

Solid Objects
Suppose we wish to determine whether a solid n-dimensional box, B,
intersects a solid n-dimensional sphere with center C and radius r. Here

GRAPHICS GEMS I Edited by ANDREW S. GLASSNER 335


V.8 A SIMPLE METHOD FOR BOX-SPHERE INTERSECTION TESTING

“solid” means that we include the interior as well as the boundary.


min max
Denote C by (C 1 , . . . , C n ) and B by the closed intervals [B1 , B1 ],
. . . , [ Bnmin , Bnmax ] . We can perform this test by finding the point P on or in
the box that is closest to the center of the sphere. If the distance between
this point and C is no greater than r, then the box intersects the sphere.
Thus, if the point P [ R n minimizes the distance function

dist(P) = (C1 − P)2 + ...+(Cn − Pn )2 , (1)

subject to the constraints (2)


Bi min ≤ Pi ≤ Bi max for i = 1,...,n,
then the solids intersect if and only if dist(P) # r. As a simplification we
will eliminate the square root by computing squared distances and com-
paring with r 2 . It is a simple matter to find the P that minimizes the
square of Eq. 1 because each term of the sum is nonnegative and can be
minimized independently. If the ith coordinate of the sphere center
satisfies the ith constraint, that is, if Bi min ≤ Ci ≤ Bi max , then setting
P i = C i reduces this term to zero. Otherwise, we set Pi equal to either
Bi min or Bi max depending on which is closer. Summing the squares of the
distances produces the minimum total squared distance. The algorithm in
Fig. 1 uses this principle to determine whether a solid box intersects a
solid sphere. The input parameters are the dimension n, box B, sphere

boolean function SolidBox_SolidSphere(n, B, C, r)


begin
dmin ← 0;
for i ← 1 . . . n do
if Ci > Bi max then dmin ← dmin +(Ci − Bi max )2 ;
else
if
endloop; Ci < Bi min then dmin ← dmin +(Ci − Bi min )2 ;
if dmin ≤ r 2 then return [True]
else return [False];

end;

Figure 1. An algorithm for intersecting a solid n-dimensional box with a solid n–dimen-
sional sphere.

GRAPHICS GEMS I Edited by ANDREW S. GLASSNER 336


V.8 A SIMPLE METHOD FOR BOX-SPHERE INTERSECTION TESTING

center C, and sphere radius r. The function value is returned as “True” if


the objects intersect and “False” otherwise.

Hollow Objects
If we wish to make either or both of the objects hollow and test only their
surfaces, we can do so by regarding total inclusion of one object inside
the other as nonintersection. For instance, if we wish to test whether the
surface of the sphere intersects the solid box, we can add a test for
whether the box is entirely contained within the sphere and regard this as
nonintersection. This is shown in the algorithm of Fig. 2, in which we’ve
added the computation of the square distance from C to the farthest
point of B. If this value, denoted dmax , is less than r 2 , the entire box is
inside the sphere and there is no intersection.
The approach is different if we wish to disregard the interior of the box.
Here the constraints of Eq. 2 still hold but in order for the point P to be
on the boundary of the box we require the additional constraint that
Pi = Bi min or Pi = Bi max for some i. In the algorithm of Fig. 1 we see that
this holds unless Ci ∈(Bi ,Bi ) for all i. In this case we need to
min max

determine whether moving P to the nearest face of the box places it

boolean function SolidBox_HollowSphere(n, B, C, r)


begin
d max ← 0;
d min ← 0;

for i ← 1 . . . n do
a ← (C i – Bmin
i
)2
b ← (C i – Bmaxi
) 2;
d max ← d max + max(a, b);
[
if C i Ò Bmin
i ]
, Bmax
i
then d min ← d min + min(a,b);
endloop;

if r 2 [ [d min, d max] then return [True]


else return [False];

end;
Figure 2. An algorithm for intersecting a solid n-dimensional box with a hollow
n-dimensional sphere.

GRAPHICS GEMS I Edited by ANDREW S. GLASSNER 337


V.8 A SIMPLE METHOD FOR BOX-SPHERE INTERSECTION TESTING

boolean function HollowBox_SolidSphere(n, B, C, r)


begin
d min ← 0
d face ← infinity
for i ← 1 . . . n do
t ← min((C i – Bmin
i
) 2,(C i – Bmax
i
) 2);
if C i [ [ B min
i , B max
i
] then d face ← min(d face ,t);
else begin
d face = 0;
d min ← d min + t;
end
endloop;
if d min + d face ≤ r 2 then return [True]
else return [False];

end;

Figure 3. An algorithm for intersecting a hollow n-dimensional box with a solid n-


dimensional sphere.

outside the sphere. In Fig. 3 we have modified the algorithm to compute


the smallest such adjustment, denoted d face.We add this to the minimum
distance between the box and C, and if the result remains less than r 2,
the surface of the box intersects the solid sphere. The approaches in Fig.
2 and Fig. 3 can be combined to test for intersection between a hollow
box and a hollow sphere.

Generalizing to Ellipsoids
We can easily generalize this idea to work with axis-aligned ellipsoids,
that is, ellipsoids that result from scaling a sphere differently along the
coordinate axes. We can specify such a ellipsoid in n-space by its center,
C[R n , and a “radius” for each axis, a 1 , . . . , a n . The point P ∈ R n
is on or in such an ellipsoid if and only if
is on or in such an ellipsoid if and only if

 C1 − P1  + ⋅ ⋅ ⋅ +  Cn − Pn  ≤ 1.
2 2

(3)
 α 1   α n 

GRAPHICS GEMS I Edited by ANDREW S. GLASSNER 338


V.8 A SIMPLE METHOD FOR BOX-SPHERE INTERSECTION TESTING

boolean function SolidBox_SolidEllipsoid(n, B, C, α)


begin
d min ← 0;
for i ← 1 ... n do
2
 C i − B min
i 
if C i < B min
then d min ← d min +  ;
i
 αi 
else

− B max
2
 
if C i > B max
then d min ← d min +  Ci α
i  ;
i  i 

endloop;

if d min ≤ 1 then return [True]


else return [False];
end;

Figure 4. An algorithm for intersecting a solid n-dimensional box with a solid n-dimen-
sional axis-aligned ellipsoid.

Modifying the algorithm in Fig. 1 to handle this type of ellipsoid results


in the algorithm shown in Fig. 4. Here the scalar input parameter r has
been changed to the array α . Modifications for either hollow boxes or
hollow ellipsoids are analogous to those previously described.

See Appendix 2 for C Implementation (730)

GRAPHICS GEMS I Edited by ANDREW S. GLASSNER 339

You might also like