Esempio n. 1
0
        public double GetSquared()
        {
            if (DistanceSquared >= 0)
            {
                return(DistanceSquared);
            }

            // Work in the box's coordinate system.
            Vector2D diff = point - box.Center;

            // Compute squared distance and closest point on box.
            double   sqrDistance = (double)0;
            double   delta;
            Vector2D closest = Vector2D.Zero;
            int      i;

            for (i = 0; i < 2; ++i)
            {
                closest[i] = diff.Dot(box.Axis(i));
                if (closest[i] < -box.Extent[i])
                {
                    delta        = closest[i] + box.Extent[i];
                    sqrDistance += delta * delta;
                    closest[i]   = -box.Extent[i];
                }
                else if (closest[i] > box.Extent[i])
                {
                    delta        = closest[i] - box.Extent[i];
                    sqrDistance += delta * delta;
                    closest[i]   = box.Extent[i];
                }
            }

            BoxClosest = box.Center;
            for (i = 0; i < 2; ++i)
            {
                BoxClosest += closest[i] * box.Axis(i);
            }

            DistanceSquared = sqrDistance;
            return(sqrDistance);
        }