# nearest point on box, - Graphics

This is a discussion on nearest point on box, - Graphics ; I have found an algorithm for determining the nearest point in an AABB to a given point. The problem is the algorithm treats the AABB as a solid, so if a point is inside the box, the nearest point is ...

1. ## nearest point on box,

I have found an algorithm for determining the nearest point in an AABB to a
given point. The problem is the algorithm treats the AABB as a solid, so if
a point is inside the box, the nearest point is just that point.

I would like the nearest point to the AABB boundary for both inside and
outside points. Is there a way to do this other than 6 plane tests?

2. ## Re: nearest point on box,

> I would like the nearest point to the AABB boundary for both inside and
> outside points. Is there a way to do this other than 6 plane tests?

Why another way? Inside the box the smallest distance is measured
orthogonally from the axis aligned bounding planes and as such are very
fast to compute. Measure the distances, choose the plane to orthogonally
project to, and do it (just an assignment to one coordinate). If some
distances are equal, consistently prefer other planes over the other.

Note the mapping is not continuous. Consider a unit cube [-2,2]^3 and
your point in (1,1,1). A small change in each coordinate makes the

It is piecewise-continuous, however, continuous in each octant.

--
Kalle Rutanen
http://kaba.hilvi.org

3. ## Re: nearest point on box,

vsgdp <hello@null.com> wrote:

> I would like the nearest point to the AABB boundary for both inside and
> outside points. Is there a way to do this other than 6 plane tests?

That depends on how strictly you interpret the term "plane test". For
an axis-aligned box, the plane tests simplify to single-coordinate
tests.

Next you can exploit the symmetry of an AABB to reduce the actual
number of tests a bit. Move your coordinate's origin to the center of
the box, and a pair of parallel planes can be tested in one shot. The
comparisons to run will be between box dimensions divided by two, and
the absolute difference between your point's coordinates and those of
the center of the box, e.g.:

abs(point.x - box.center.x) - box.size.x / 2

If this is zero, the query point is already on one of the yz-planes of
the box. If it's positive, the pointer is outside, so the closest
point will be somewhere on it. If it's negative, delay the decision
until you've seen all three comparisons.

If none of the comparisons found the point outside, it's on the
boundary or inside. If all returned negative values, it's inside. In
this case, you have to compare the three negative numbers among
themselves to find the smallest. That gives the direction to the
nearest border point.

--
Hans-Bernhard Broeker (broeker@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.