import java.awt.*;



/** 
 *  The Clipper class is a static class with a method that clips one 
 *  Rectangle to another.
 */

public class Clipper {
    
    /** 
     *  The clipRect method clips one Rectangle to another.  
     *  If the Rectangles intersect, this method changes the first parameter
     *  so that it is the intersection of the two Rectangles and the function
     *  returns true.  If there is no intersection, the function returns 
     *  false and the parameters are unmodified.
     */
    
    static public boolean clipRect(Rectangle rect, Rectangle clipRect) {
        int sx1, sy1, sx2, sy2, dx1, dx2, dy1, dy2;
        
        sx1 = rect.x;
        sy1 = rect.y;
        sx2 = rect.x + rect.width;
        sy2 = rect.y + rect.height;
        dx1 = clipRect.x;
        dy1 = clipRect.y;
        dx2 = clipRect.x + clipRect.width;
        dy2 = clipRect.y + clipRect.height;
        
        if (sx2 < dx1 || sx1 >= dx2 || sy2 < dy1 || sy1 >= dy2) {
            return false;
        } else {
            if (sx1 < dx1) sx1 = dx1;
            if (sy1 < dy1) sy1 = dy1;
            if (sx2 > dx2) sx2 = dx2;
            if (sy2 > dy2) sy2 = dy2;
            
            rect.x = sx1;
            rect.y = sy1;
            rect.width = sx2 - sx1;
            rect.height = sy2 - sy1;
            
            return true;
        }
    }
}