private static double distance(RegionInfo r1, RegionInfo r2) {
      double dist = 10000.0;
      int maxX = (r1.maxX <= r2.maxX ? r1.maxX : r2.maxX);
      int minX = (r1.minX >= r2.minX ? r1.minX : r2.minX);
      int maxY = (r1.maxY <= r2.maxY ? r1.maxY : r2.maxY);
      int minY = (r1.minY >= r2.minY ? r1.minY : r2.minY);
      if (maxX - minX > 0) { // the two overlap horizontally
         if (r1.maxY < r2.minY) // r2 is above r1
            dist = r2.minY - r1.maxY;
         else                   // r2 is below r1
            dist = r1.minY - r2.maxY;
      }
      else if (maxY - minY > 0) { // the two overlap vertically
         if (r1.maxX < r2.minX) // r2 is right of r1
            dist = r2.minX - r1.maxX;
         else                   // r2 is left of r1
            dist = r1.minX - r2.maxX;
      }
      else { // no overlap at all
         dist = Math.sqrt(dist); // default distance is 100
      }

      return dist;
   }