public static int intersectLineRectangle(Line2D l,Rectangle2D r,Point2D[] pts){
  double a1x=l.getX1(), a1y=l.getY1();
  double a2x=l.getX2(), a2y=l.getY2();
  double mxx=r.getMaxX(), mxy=r.getMaxY();
  double mnx=r.getMinX(), mny=r.getMinY();
  if (pts[0] == null)   pts[0]=new Point2D.Double();
  if (pts[1] == null)   pts[1]=new Point2D.Double();
  int i=0;
  if (intersectLineLine(mnx,mny,mxx,mny,a1x,a1y,a2x,a2y,pts[i]) > 0)   i++;
  if (intersectLineLine(mxx,mny,mxx,mxy,a1x,a1y,a2x,a2y,pts[i]) > 0)   i++;
  if (i == 2)   return i;
  if (intersectLineLine(mxx,mxy,mnx,mxy,a1x,a1y,a2x,a2y,pts[i]) > 0)   i++;
  if (i == 2)   return i;
  if (intersectLineLine(mnx,mxy,mnx,mny,a1x,a1y,a2x,a2y,pts[i]) > 0)   i++;
  return i;
}

public static int intersectLineRectangle ( Line2D l , Rectangle2D r , Point2D [ ] pts ) { double a1x = l . getX1 ( ) , a1y = l . getY1 ( ) ; double a2x = l . getX2 ( ) , a2y = l . getY2 ( ) ; double mxx = r . getMaxX ( ) , mxy = r . getMaxY ( ) ; double mnx = r . getMinX ( ) , mny = r . getMinY ( ) ; if ( pts [ 0 ] == null ) pts [ 0 ] = new Point2D . Double ( ) ; if ( pts [ 1 ] == null ) pts [ 1 ] = new Point2D . Double ( ) ; int i = 0 ; if ( intersectLineLine ( mnx , mny , mxx , mny , a1x , a1y , a2x , a2y , pts [ i ] ) > 0 ) i ++ ; if ( intersectLineLine ( mxx , mny , mxx , mxy , a1x , a1y , a2x , a2y , pts [ i ] ) > 0 ) i ++ ; if ( i == 2 ) return i ; if ( intersectLineLine ( mxx , mxy , mnx , mxy , a1x , a1y , a2x , a2y , pts [ i ] ) > 0 ) i ++ ; if ( i == 2 ) return i ; if ( intersectLineLine ( mnx , mxy , mnx , mny , a1x , a1y , a2x , a2y , pts [ i ] ) > 0 ) i ++ ; return i ; }
compute the intersection of a line and a rectangle .
