Class Rectangle

    • Constructor Detail

      • Rectangle

        public Rectangle()
        Constructor for Rectangle.
      • Rectangle

        public Rectangle​(int x,
                         int y,
                         int w,
                         int h)
        Constructor for Rectangle.
    • Method Detail

      • setBounds

        public void setBounds​(int x,
                              int y,
                              int w,
                              int h)
        Description copied from interface: WritableRectangle
        Sets the bounds of the rectangle
        Specified by:
        setBounds in interface WritableRectangle
        Parameters:
        x - Position of rectangle on x axis
        y - Position of rectangle on y axis
        w - Width of rectangle
        h - Height of rectangle
      • getBounds

        public void getBounds​(WritableRectangle dest)
        Description copied from interface: ReadableRectangle
        Copy this readable rectangle's bounds into a destination Rectangle
        Specified by:
        getBounds in interface ReadableRectangle
        Parameters:
        dest - The destination Rectangle, or null, to create a new Rectangle
      • getLocation

        public void getLocation​(WritablePoint dest)
        Description copied from interface: ReadablePoint
        Copy this ReadablePoint into a destination Point
        Specified by:
        getLocation in interface ReadablePoint
        Parameters:
        dest - The destination Point, or null, to create a new Point
      • translate

        public void translate​(int x,
                              int y)
        Translate the rectangle by an amount.
        Parameters:
        x - The translation amount on the x axis
        y - The translation amount on the y axis
      • translate

        public void translate​(ReadablePoint point)
        Translate the rectangle by an amount.
        Parameters:
        point - The translation amount
      • untranslate

        public void untranslate​(ReadablePoint point)
        Un-translate the rectangle by an amount.
        Parameters:
        point - The translation amount
      • contains

        public boolean contains​(ReadablePoint p)
        Checks whether or not this Rectangle contains the specified Point.
        Parameters:
        p - the Point to test
        Returns:
        true if the Point (xy) is inside this Rectangle; false otherwise.
      • contains

        public boolean contains​(int X,
                                int Y)
        Checks whether or not this Rectangle contains the point at the specified location (xy).
        Parameters:
        X - the specified x coordinate
        Y - the specified y coordinate
        Returns:
        true if the point (xy) is inside this Rectangle; false otherwise.
      • contains

        public boolean contains​(ReadableRectangle r)
        Checks whether or not this Rectangle entirely contains the specified Rectangle.
        Parameters:
        r - the specified Rectangle
        Returns:
        true if the Rectangle is contained entirely inside this Rectangle; false otherwise.
      • contains

        public boolean contains​(int X,
                                int Y,
                                int W,
                                int H)
        Checks whether this Rectangle entirely contains the Rectangle at the specified location (XY) with the specified dimensions (WH).
        Parameters:
        X - the specified x coordinate
        Y - the specified y coordinate
        W - the width of the Rectangle
        H - the height of the Rectangle
        Returns:
        true if the Rectangle specified by (XYWH) is entirely enclosed inside this Rectangle; false otherwise.
      • intersects

        public boolean intersects​(ReadableRectangle r)
        Determines whether or not this Rectangle and the specified Rectangle intersect. Two rectangles intersect if their intersection is nonempty.
        Parameters:
        r - the specified Rectangle
        Returns:
        true if the specified Rectangle and this Rectangle intersect; false otherwise.
      • intersection

        public Rectangle intersection​(ReadableRectangle r,
                                      Rectangle dest)
        Computes the intersection of this Rectangle with the specified Rectangle. Returns a new Rectangle that represents the intersection of the two rectangles. If the two rectangles do not intersect, the result will be an empty rectangle.
        Parameters:
        r - the specified Rectangle
        Returns:
        the largest Rectangle contained in both the specified Rectangle and in this Rectangle; or if the rectangles do not intersect, an empty rectangle.
      • union

        public WritableRectangle union​(ReadableRectangle r,
                                       WritableRectangle dest)
        Computes the union of this Rectangle with the specified Rectangle. Returns a new Rectangle that represents the union of the two rectangles
        Parameters:
        r - the specified Rectangle
        Returns:
        the smallest Rectangle containing both the specified Rectangle and this Rectangle.
      • add

        public void add​(int newx,
                        int newy)
        Adds a point, specified by the integer arguments newx and newy, to this Rectangle. The resulting Rectangle is the smallest Rectangle that contains both the original Rectangle and the specified point.

        After adding a point, a call to contains with the added point as an argument does not necessarily return true. The contains method does not return true for points on the right or bottom edges of a Rectangle. Therefore, if the added point falls on the right or bottom edge of the enlarged Rectangle, contains returns false for that point.

        Parameters:
        newx - the x coordinates of the new point
        newy - the y coordinates of the new point
      • add

        public void add​(ReadablePoint pt)
        Adds the specified Point to this Rectangle. The resulting Rectangle is the smallest Rectangle that contains both the original Rectangle and the specified Point.

        After adding a Point, a call to contains with the added Point as an argument does not necessarily return true. The contains method does not return true for points on the right or bottom edges of a Rectangle. Therefore if the added Point falls on the right or bottom edge of the enlarged Rectangle, contains returns false for that Point.

        Parameters:
        pt - the new Point to add to this Rectangle
      • add

        public void add​(ReadableRectangle r)
        Adds a Rectangle to this Rectangle. The resulting Rectangle is the union of the two rectangles.
        Parameters:
        r - the specified Rectangle
      • grow

        public void grow​(int h,
                         int v)
        Resizes the Rectangle both horizontally and vertically.

        This method modifies the Rectangle so that it is h units larger on both the left and right side, and v units larger at both the top and bottom.

        The new Rectangle has (x - h, y - v) as its top-left corner, a width of width + 2h, and a height of height + 2v.

        If negative values are supplied for h and v, the size of the Rectangle decreases accordingly. The grow method does not check whether the resulting values of width and height are non-negative.

        Parameters:
        h - the horizontal expansion
        v - the vertical expansion
      • isEmpty

        public boolean isEmpty()
        Determines whether or not this Rectangle is empty. A Rectangle is empty if its width or its height is less than or equal to zero.
        Returns:
        true if this Rectangle is empty; false otherwise.
      • equals

        public boolean equals​(java.lang.Object obj)
        Checks whether two rectangles are equal.

        The result is true if and only if the argument is not null and is a Rectangle object that has the same top-left corner, width, and height as this Rectangle.

        Overrides:
        equals in class java.lang.Object
        Parameters:
        obj - the Object to compare with this Rectangle
        Returns:
        true if the objects are equal; false otherwise.
      • toString

        public java.lang.String toString()
        Debugging
        Overrides:
        toString in class java.lang.Object
        Returns:
        a String
      • getHeight

        public int getHeight()
        Gets the height.
        Specified by:
        getHeight in interface ReadableDimension
        Returns:
        Returns a int
      • setHeight

        public void setHeight​(int height)
        Sets the height.
        Specified by:
        setHeight in interface WritableDimension
        Parameters:
        height - The height to set
      • getWidth

        public int getWidth()
        Gets the width.
        Specified by:
        getWidth in interface ReadableDimension
        Returns:
        Returns a int
      • setWidth

        public void setWidth​(int width)
        Sets the width.
        Specified by:
        setWidth in interface WritableDimension
        Parameters:
        width - The width to set
      • getX

        public int getX()
        Gets the x.
        Specified by:
        getX in interface ReadablePoint
        Returns:
        Returns a int
      • setX

        public void setX​(int x)
        Sets the x.
        Specified by:
        setX in interface WritablePoint
        Parameters:
        x - The x to set
      • getY

        public int getY()
        Gets the y.
        Specified by:
        getY in interface ReadablePoint
        Returns:
        Returns a int
      • setY

        public void setY​(int y)
        Sets the y.
        Specified by:
        setY in interface WritablePoint
        Parameters:
        y - The y to set