public static boolean rectCrossesPoly(final double rMinX,final double rMinY,final double rMaxX,final double rMaxY,final double[] shapeX,final double[] shapeY,final double sMinX,final double sMinY,final double sMaxX,final double sMaxY){
  if (rectDisjoint(rMinX,rMinY,rMaxX,rMaxY,sMinX,sMinY,sMaxX,sMaxY)) {
    return false;
  }
  final double[][] bbox=new double[][]{{rMinX,rMinY},{rMaxX,rMinY},{rMaxX,rMaxY},{rMinX,rMaxY},{rMinX,rMinY}};
  final int polyLength=shapeX.length - 1;
  double d, s, t, a1, b1, c1, a2, b2, c2;
  double x00, y00, x01, y01, x10, y10, x11, y11;
  for (short b=0; b < 4; ++b) {
    a1=bbox[b + 1][1] - bbox[b][1];
    b1=bbox[b][0] - bbox[b + 1][0];
    c1=a1 * bbox[b + 1][0] + b1 * bbox[b + 1][1];
    for (int p=0; p < polyLength; ++p) {
      a2=shapeY[p + 1] - shapeY[p];
      b2=shapeX[p] - shapeX[p + 1];
      d=a1 * b2 - a2 * b1;
      if (d != 0) {
        c2=a2 * shapeX[p + 1] + b2 * shapeY[p + 1];
        s=(1 / d) * (b2 * c1 - b1 * c2);
        t=(1 / d) * (a1 * c2 - a2 * c1);
        x00=StrictMath.min(bbox[b][0],bbox[b + 1][0]) - TOLERANCE;
        x01=StrictMath.max(bbox[b][0],bbox[b + 1][0]) + TOLERANCE;
        y00=StrictMath.min(bbox[b][1],bbox[b + 1][1]) - TOLERANCE;
        y01=StrictMath.max(bbox[b][1],bbox[b + 1][1]) + TOLERANCE;
        x10=StrictMath.min(shapeX[p],shapeX[p + 1]) - TOLERANCE;
        x11=StrictMath.max(shapeX[p],shapeX[p + 1]) + TOLERANCE;
        y10=StrictMath.min(shapeY[p],shapeY[p + 1]) - TOLERANCE;
        y11=StrictMath.max(shapeY[p],shapeY[p + 1]) + TOLERANCE;
        boolean touching=((x00 == s && y00 == t) || (x01 == s && y01 == t)) || ((x10 == s && y10 == t) || (x11 == s && y11 == t));
        if (!(touching || x00 > s || x01 < s || y00 > t || y01 < t || x10 > s || x11 < s || y10 > t || y11 < t)) {
          return true;
        }
      }
    }
  }
  return false;
}

public static boolean rectCrossesPoly ( final double rMinX , final double rMinY , final double rMaxX , final double rMaxY , final double [ ] shapeX , final double [ ] shapeY , final double sMinX , final double sMinY , final double sMaxX , final double sMaxY ) { if ( rectDisjoint ( rMinX , rMinY , rMaxX , rMaxY , sMinX , sMinY , sMaxX , sMaxY ) ) { return false ; } final double [ ] [ ] bbox = new double [ ] [ ] { { rMinX , rMinY } , { rMaxX , rMinY } , { rMaxX , rMaxY } , { rMinX , rMaxY } , { rMinX , rMinY } } ; final int polyLength = shapeX . length - 1 ; double d , s , t , a1 , b1 , c1 , a2 , b2 , c2 ; double x00 , y00 , x01 , y01 , x10 , y10 , x11 , y11 ; for ( short b = 0 ; b < 4 ; ++ b ) { a1 = bbox [ b + 1 ] [ 1 ] - bbox [ b ] [ 1 ] ; b1 = bbox [ b ] [ 0 ] - bbox [ b + 1 ] [ 0 ] ; c1 = a1 * bbox [ b + 1 ] [ 0 ] + b1 * bbox [ b + 1 ] [ 1 ] ; for ( int p = 0 ; p < polyLength ; ++ p ) { a2 = shapeY [ p + 1 ] - shapeY [ p ] ; b2 = shapeX [ p ] - shapeX [ p + 1 ] ; d = a1 * b2 - a2 * b1 ; if ( d != 0 ) { c2 = a2 * shapeX [ p + 1 ] + b2 * shapeY [ p + 1 ] ; s = ( 1 / d ) * ( b2 * c1 - b1 * c2 ) ; t = ( 1 / d ) * ( a1 * c2 - a2 * c1 ) ; x00 = StrictMath . min ( bbox [ b ] [ 0 ] , bbox [ b + 1 ] [ 0 ] ) - TOLERANCE ; x01 = StrictMath . max ( bbox [ b ] [ 0 ] , bbox [ b + 1 ] [ 0 ] ) + TOLERANCE ; y00 = StrictMath . min ( bbox [ b ] [ 1 ] , bbox [ b + 1 ] [ 1 ] ) - TOLERANCE ; y01 = StrictMath . max ( bbox [ b ] [ 1 ] , bbox [ b + 1 ] [ 1 ] ) + TOLERANCE ; x10 = StrictMath . min ( shapeX [ p ] , shapeX [ p + 1 ] ) - TOLERANCE ; x11 = StrictMath . max ( shapeX [ p ] , shapeX [ p + 1 ] ) + TOLERANCE ; y10 = StrictMath . min ( shapeY [ p ] , shapeY [ p + 1 ] ) - TOLERANCE ; y11 = StrictMath . max ( shapeY [ p ] , shapeY [ p + 1 ] ) + TOLERANCE ; boolean touching = ( ( x00 == s && y00 == t ) || ( x01 == s && y01 == t ) ) || ( ( x10 == s && y10 == t ) || ( x11 == s && y11 == t ) ) ; if ( ! ( touching || x00 > s || x01 < s || y00 > t || y01 < t || x10 > s || x11 < s || y10 > t || y11 < t ) ) { return true ; } } } } return false ; }
computes whether a rectangle crosses a shape . ( touching not allowed )
