private static boolean calculateGap(Rectangle a,Rectangle b,Rectangle gap){
  if (a.intersects(b)) {
    gap.width=0;
    return false;
  }
  int ax1=a.x;
  int ax2=a.x + a.width;
  int ay1=a.y;
  int ay2=a.y + a.height;
  int bx1=b.x;
  int bx2=b.x + b.width;
  int by1=b.y;
  int by2=b.y + b.height;
  int xOverlap=Math.min(ax2,bx2) - Math.max(ax1,bx1);
  int yOverlap=Math.min(ay2,by2) - Math.max(ay1,by1);
  if (xOverlap <= 0 && yOverlap <= 0) {
    gap.width=0;
    return false;
  }
  if (xOverlap > 0) {
    gap.x=Math.max(ax1,bx1);
    gap.y=(ay1 > by1) ? by2 : ay2;
    gap.width=xOverlap;
    gap.height=-yOverlap;
  }
  if (yOverlap > 0) {
    gap.x=(ax1 > bx1) ? bx2 : ax2;
    gap.y=Math.max(ay1,by1);
    gap.width=-xOverlap;
    gap.height=yOverlap;
  }
  return true;
}

private static boolean calculateGap ( Rectangle a , Rectangle b , Rectangle gap ) { if ( a . intersects ( b ) ) { gap . width = 0 ; return false ; } int ax1 = a . x ; int ax2 = a . x + a . width ; int ay1 = a . y ; int ay2 = a . y + a . height ; int bx1 = b . x ; int bx2 = b . x + b . width ; int by1 = b . y ; int by2 = b . y + b . height ; int xOverlap = Math . min ( ax2 , bx2 ) - Math . max ( ax1 , bx1 ) ; int yOverlap = Math . min ( ay2 , by2 ) - Math . max ( ay1 , by1 ) ; if ( xOverlap <= 0 && yOverlap <= 0 ) { gap . width = 0 ; return false ; } if ( xOverlap > 0 ) { gap . x = Math . max ( ax1 , bx1 ) ; gap . y = ( ay1 > by1 ) ? by2 : ay2 ; gap . width = xOverlap ; gap . height = - yOverlap ; } if ( yOverlap > 0 ) { gap . x = ( ax1 > bx1 ) ? bx2 : ax2 ; gap . y = Math . max ( ay1 , by1 ) ; gap . width = - xOverlap ; gap . height = yOverlap ; } return true ; }
calculate the gap rectangle between two rectangles
