public static int rectCrossingsForCubic(int crossings,double rxmin,double rymin,double rxmax,double rymax,double x0,double y0,double xc0,double yc0,double xc1,double yc1,double x1,double y1,int level){
  if (y0 >= rymax && yc0 >= rymax && yc1 >= rymax && y1 >= rymax) {
    return crossings;
  }
  if (y0 <= rymin && yc0 <= rymin && yc1 <= rymin && y1 <= rymin) {
    return crossings;
  }
  if (x0 <= rxmin && xc0 <= rxmin && xc1 <= rxmin && x1 <= rxmin) {
    return crossings;
  }
  if (x0 >= rxmax && xc0 >= rxmax && xc1 >= rxmax && x1 >= rxmax) {
    if (y0 < y1) {
      if (y0 <= rymin && y1 > rymin)       crossings++;
      if (y0 < rymax && y1 >= rymax)       crossings++;
    }
 else     if (y1 < y0) {
      if (y1 <= rymin && y0 > rymin)       crossings--;
      if (y1 < rymax && y0 >= rymax)       crossings--;
    }
    return crossings;
  }
  if ((x0 > rxmin && x0 < rxmax && y0 > rymin && y0 < rymax) || (x1 > rxmin && x1 < rxmax && y1 > rymin && y1 < rymax)) {
    return RECT_INTERSECTS;
  }
  if (level > 52) {
    return rectCrossingsForLine(crossings,rxmin,rymin,rxmax,rymax,x0,y0,x1,y1);
  }
  double xmid=(xc0 + xc1) / 2;
  double ymid=(yc0 + yc1) / 2;
  xc0=(x0 + xc0) / 2;
  yc0=(y0 + yc0) / 2;
  xc1=(xc1 + x1) / 2;
  yc1=(yc1 + y1) / 2;
  double xc0m=(xc0 + xmid) / 2;
  double yc0m=(yc0 + ymid) / 2;
  double xmc1=(xmid + xc1) / 2;
  double ymc1=(ymid + yc1) / 2;
  xmid=(xc0m + xmc1) / 2;
  ymid=(yc0m + ymc1) / 2;
  if (Double.isNaN(xmid) || Double.isNaN(ymid)) {
    return 0;
  }
  crossings=rectCrossingsForCubic(crossings,rxmin,rymin,rxmax,rymax,x0,y0,xc0,yc0,xc0m,yc0m,xmid,ymid,level + 1);
  if (crossings != RECT_INTERSECTS) {
    crossings=rectCrossingsForCubic(crossings,rxmin,rymin,rxmax,rymax,xmid,ymid,xmc1,ymc1,xc1,yc1,x1,y1,level + 1);
  }
  return crossings;
}

public static int rectCrossingsForCubic ( int crossings , double rxmin , double rymin , double rxmax , double rymax , double x0 , double y0 , double xc0 , double yc0 , double xc1 , double yc1 , double x1 , double y1 , int level ) { if ( y0 >= rymax && yc0 >= rymax && yc1 >= rymax && y1 >= rymax ) { return crossings ; } if ( y0 <= rymin && yc0 <= rymin && yc1 <= rymin && y1 <= rymin ) { return crossings ; } if ( x0 <= rxmin && xc0 <= rxmin && xc1 <= rxmin && x1 <= rxmin ) { return crossings ; } if ( x0 >= rxmax && xc0 >= rxmax && xc1 >= rxmax && x1 >= rxmax ) { if ( y0 < y1 ) { if ( y0 <= rymin && y1 > rymin ) crossings ++ ; if ( y0 < rymax && y1 >= rymax ) crossings ++ ; } else if ( y1 < y0 ) { if ( y1 <= rymin && y0 > rymin ) crossings -- ; if ( y1 < rymax && y0 >= rymax ) crossings -- ; } return crossings ; } if ( ( x0 > rxmin && x0 < rxmax && y0 > rymin && y0 < rymax ) || ( x1 > rxmin && x1 < rxmax && y1 > rymin && y1 < rymax ) ) { return RECT_INTERSECTS ; } if ( level > 52 ) { return rectCrossingsForLine ( crossings , rxmin , rymin , rxmax , rymax , x0 , y0 , x1 , y1 ) ; } double xmid = ( xc0 + xc1 ) / 2 ; double ymid = ( yc0 + yc1 ) / 2 ; xc0 = ( x0 + xc0 ) / 2 ; yc0 = ( y0 + yc0 ) / 2 ; xc1 = ( xc1 + x1 ) / 2 ; yc1 = ( yc1 + y1 ) / 2 ; double xc0m = ( xc0 + xmid ) / 2 ; double yc0m = ( yc0 + ymid ) / 2 ; double xmc1 = ( xmid + xc1 ) / 2 ; double ymc1 = ( ymid + yc1 ) / 2 ; xmid = ( xc0m + xmc1 ) / 2 ; ymid = ( yc0m + ymc1 ) / 2 ; if ( Double . isNaN ( xmid ) || Double . isNaN ( ymid ) ) { return 0 ; } crossings = rectCrossingsForCubic ( crossings , rxmin , rymin , rxmax , rymax , x0 , y0 , xc0 , yc0 , xc0m , yc0m , xmid , ymid , level + 1 ) ; if ( crossings != RECT_INTERSECTS ) { crossings = rectCrossingsForCubic ( crossings , rxmin , rymin , rxmax , rymax , xmid , ymid , xmc1 , ymc1 , xc1 , yc1 , x1 , y1 , level + 1 ) ; } return crossings ; }
accumulate the number of times the cubic crosses the shadow extending to the right of the rectangle . see the comment for the rect_intersects constant for more complete details .
