/**
 * @author
 *  Behrooz Badii 	bb2122@columbia.edu
 *  Hanhua Feng         hanhua@cs.columbia.edu
 *  Edan Harel 	        edan@columbia.edu
 */
package CookieCutter.g6;

import java.io.Serializable;

/**
 * A rectangle whose edges are parallel with x- and y- axes.
 */
public class Rectangle implements Serializable {
    double _x0, _y0, _width, _height;

    public Rectangle( double x0, double y0, double width, double height ) {
        _x0 = x0;
        _y0 = y0;
        _width = width;
        _height = height;
    }

    public final double x0() {
        return _x0;
    }

    public final double x1() {
        return _x0 + _width;
    }

    public final double y0() {
        return _y0;
    }

    public final double y1() {
        return _y0 + _height;
    }

    public final double width() {
        return _width;
    }

    public final double height() {
        return _height;
    }

    public final boolean intersects( Rectangle r ) {
        return ( x1() >= r.x0() || x0() <= r.x1() )
            && ( y1() >= r.y0() || y0() <= r.y1() );
    }

    public Rectangle union( Rectangle r ) {
        double x0 = Math.min( x0(), r.x0() );
        double x1 = Math.max( x1(), r.x1() );
        double y0 = Math.min( y0(), r.y0() );
        double y1 = Math.max( y1(), r.y1() );
        return new Rectangle( x0, y0, x1-x0, y1-y0 );
    }

    public String toString() {
        StringBuffer sb = new StringBuffer();

        sb.append( "[(" );
        sb.append( Plane.format( _x0 ) );
        sb.append( "," );
        sb.append( Plane.format( _y0 ) );
        sb.append( ") " );
        sb.append( Plane.format( _width ) );
        sb.append( "/" );
        sb.append( Plane.format( _height ) );
        sb.append( "]" );

        return new String(sb);
    }
}
