public double BorderDistance(float x, float y, float z)
{
double nsdistance;
double ewdistance;
double fbdistance;
if (Left <= x && x <= Right)
ewdistance = 0;
else
ewdistance = Math.Min((Math.Abs(x - Right)), (Math.Abs(x - Left)));
if (Front <= y && y <= Back)
fbdistance = 0;
else
fbdistance = Math.Min(Math.Abs(y - Back), Math.Abs(y - Front));
if (Bottom <= z && z <= Top)
nsdistance = 0;
else
nsdistance = Math.Min(Math.Abs(z - Top), Math.Abs(z - Bottom));
return Math.Sqrt(nsdistance*nsdistance +
ewdistance*ewdistance +
fbdistance*fbdistance);
}