/**
 *  The Rectangle class represents a rectangular two-dimensional region
 *  @date: 2/2/2105
 *  @author : Harmit Minhas, Then Pepper
 *  
 */
public class Rectangle
{
    private int x,y;
    private int width, height;
    /**
     * 
     * @param x This is the x coordinate of the top left corner of the rectangle.
     * @param y This is the y coordinate of the top left corner of the rectangle.
     * @param width This is the width of the rectangle.
     * @param height This is the heigh of the rectangle
     * @exception IllegalArgumentException when width or height is <= 0
     */

    public Rectangle(int x, int y, int width, int height)
    {
        if (width <= 0 || height <= 0) 
        { throw new IllegalArgumentException("Width and height both have to be greater than 0"); }
        this.x=x;
        this.y=y;
        this.width=width;
        this.height=height;
    }

    /**
     * method that tells whether two points are inside a rectangle
     * @param otherX - the x coord of the point being tested as inside the rectangle
     * @param otherY - the y coord of the point being tested as inside the rectangle
     * @return true = the x,y passed are inside the rectangle ; false = the x, y passed are outside the rectangle; 
     *    points on the border are considered true. 
     */
    public boolean isInside(int otherX, int otherY){
        return (otherX >= this.x && otherX <= (this.x + this.width) && 
            otherY >= this.y && otherY <= this.y + this.height);
    }

    /**
     * method that returns a rectangle containing both the object's rectangle and the parameter's rectangle
     * The rectangles stay on the grid at their original positions and a new rectangle is drawn around them and returned.
     * @param other - another rectangle to be fit inside a new rectangle with the object's rectangle
     * @return a rectangle that is big enough to fit both the object's rectangle and the rectangle 
     *       passed to the method. 
     */
    public Rectangle containingRect(Rectangle other){
        int newX, newY, newH, newW;
        if (other.x < this.x) 
        { newX = other.x;}
        else { newX = this.x;}
        if (other.y < this.y) 
        { newY = other.y;}
        else { newY = this.y;}
        if (other.x + other.width < this.x + this.width) 
        { newW = this.x + this.width - newX;}
        else { newW = other.x + other.width - newX;}
        if (other.y + other.height < this.y + this.height) 
        { newH = this.y + this.height - newY;}
        else { newH = other.y + other.height - newY;}
        return new Rectangle (newX, newY, newW, newH);
    }

    /**
     * method that just gets the rectangle height
     * @return a height of the rectangle
     */
    public int getHeight()
    {
        return this.height;
    }

    /**
     * method that just gets the width of the rectangle
     * @return the width of the rectangle
     */
    public int getWidth()
    {
        return this.width;
    }

    /**
     * method that gets the top left corner x of the rectangle
     * @return the x coordinate of the top left corner of the rectangle.
     */
    public int getX()
    {
        return this.x;
    }

    /**
     * method that gets the top left corner y of the rectangle
     * @return the y coordinate of the top left corner of the rectangle.
     */
    public int getY()
    {
        return this.y;
    }

    /**
     * method that creates a string describing the object
     * @return a string the gives the coordinates of the top left corner,
     * width, and the height of the rectangle, such as 
     * "Rectangle[x=1,y=2,width=3,height=4]".
     */
    public String toString()
    {
        return "Rectangle[x="+x+",y="+y+",width="+width+",height="+height+"]";
    }
}