BoundsInt ClosestPoint

Bounds has ClosestPoint but BoundsInt does not?
What is the best way to implement this?
This seems to work… but maybe a better way…
Thanks.

    public static partial class BoundsIntExtensions
    {
        public static Vector3Int ClosestPoint(this BoundsInt bounds, Vector3Int p)
        {
            var closest = float.MaxValue;
            var closestPoint = Vector3Int.zero;
            foreach (var i in bounds.allPositionsWithin)
            {
                var test = (i - p).magnitude;
                if (test < closest)
                {
                    closest = test;
                    closestPoint = i;
                }
            }
            return closestPoint;
        }
    }

EDIT: Thanks.

p.Clamp(bounds.min, bounds.max);

It’s even simpler than that (for an AABB at least).

Just clamp your point between the min/max of the bounds and that’s the closest point. If the point is within the bounds it’s returned unchanged.

1 Like

or -1 maybe? else 10 gets through even though map only goes from 0-9?

like how contains has an inclusive flag to include max limits?
idk
thanks

3860194--326884--2018-11-05 (1).png