Class Rectangular

  • All Implemented Interfaces:
    Rect
    Direct Known Subclasses:
    RectangularZ, Separator

    public class Rectangular
    extends Object
    implements Rect
    This class represents a general rectangular area.
    Author:
    radek
    • Field Detail

      • x1

        protected int x1
      • y1

        protected int y1
      • x2

        protected int x2
      • y2

        protected int y2
    • Constructor Detail

      • Rectangular

        public Rectangular()
      • Rectangular

        public Rectangular​(int x1,
                           int y1,
                           int x2,
                           int y2)
        Creates a rectangle at the given coordinates. The x1:x2 and y1:y2 coordinates are automatically reordered so that x1 < x2 and y1 < y2. Note that this behavior does not allow creating empty rectangles using this constructor.
        Parameters:
        x1 -
        y1 -
        x2 -
        y2 -
      • Rectangular

        public Rectangular​(int x1,
                           int y1,
                           int x2,
                           int y2,
                           boolean reorder)
        Creates a rectangle at the given coordinates. The x1:x2 and y1:y2 coordinates may be automatically reordered so that x1 < x2 and y1 < y2.
        Parameters:
        x1 -
        y1 -
        x2 -
        y2 -
        reorder - Allow reordering the x and y values so that x1 < x2 and y1 < y2
      • Rectangular

        public Rectangular​(int x1,
                           int y1)
        Creates an empty rectangle at the given coordinates.
        Parameters:
        x1 -
        y1 -
    • Method Detail

      • getX1

        public int getX1()
        Specified by:
        getX1 in interface Rect
        Returns:
        the x1
      • setX1

        public void setX1​(int x1)
        Parameters:
        x1 - the x1 to set
      • getX2

        public int getX2()
        Specified by:
        getX2 in interface Rect
        Returns:
        the x2
      • setX2

        public void setX2​(int x2)
        Parameters:
        x2 - the x2 to set
      • getY1

        public int getY1()
        Specified by:
        getY1 in interface Rect
        Returns:
        the y1
      • setY1

        public void setY1​(int y1)
        Parameters:
        y1 - the y1 to set
      • getY2

        public int getY2()
        Specified by:
        getY2 in interface Rect
        Returns:
        the y2
      • setY2

        public void setY2​(int y2)
        Parameters:
        y2 - the y2 to set
      • getWidth

        public int getWidth()
        Specified by:
        getWidth in interface Rect
      • getHeight

        public int getHeight()
        Specified by:
        getHeight in interface Rect
      • midX

        public int midX()
      • midY

        public int midY()
      • move

        public void move​(int xofs,
                         int yofs)
        Changes the rectangle coordinates by adding the specified X and Y offsets
        Specified by:
        move in interface Rect
        Parameters:
        xofs - the X offset
        yofs - the Y offset
      • getArea

        public int getArea()
      • isEmpty

        public boolean isEmpty()
      • encloses

        public boolean encloses​(Rectangular other)
        Checks if this rectangle entirely contains another rectangle.
        Parameters:
        other - the other rectangle
        Returns:
        true when the other rectangle is completely contained in this one
      • enclosesX

        public boolean enclosesX​(Rectangular other)
        Checks if this rectangle entirely contains the X coordinates of another rectangle.
        Parameters:
        other - the other rectangle
        Returns:
        true when the X coordinates of the other rectangle are completely contained in this one
      • enclosesY

        public boolean enclosesY​(Rectangular other)
        Checks if this rectangle entirely contains the Y coordinates of another rectangle.
        Parameters:
        other - the other rectangle
        Returns:
        true when the Y coordinates of the other rectangle are completely contained in this one
      • contains

        public boolean contains​(int x,
                                int y)
        Checks if this rectangle contains a point.
        Parameters:
        x - the point X coordinate
        y - the point Y coordinate
        Returns:
        true when the point is contained in this one
      • intersects

        public boolean intersects​(Rectangular other)
      • intersectsX

        public boolean intersectsX​(Rectangular other)
      • intersectsY

        public boolean intersectsY​(Rectangular other)
      • intersection

        public Rectangular intersection​(Rectangular other)
        Computes the intersection of this rectangle with another one.
        Parameters:
        other - the other rectangle
        Returns:
        the resulting intersection or an empty rectangle when there is no intersection
      • union

        public Rectangular union​(Rectangular other)
        Computes the union of this rectangle with another one.
        Parameters:
        other - the other rectangle
        Returns:
        the union rectangle
      • replaceX

        public Rectangular replaceX​(Rectangular other)
        Replaces the X coordinates of the rectangle with the X coordinates of another one.
        Parameters:
        other - the rectangle whose X coordinates will be used
        Returns:
        the resulting rectangle
      • replaceY

        public Rectangular replaceY​(Rectangular other)
        Replaces the Y coordinates of the rectangle with the Y coordinates of another one.
        Parameters:
        other - the rectangle whose Y coordinates will be used
        Returns:
        the resulting rectangle
      • hsplit

        public Rectangular hsplit​(Rectangular other)
        If this rectangle intersets with the other one, splits this rectangle horizontally so that it does not intersect with the other one anymore.
        Parameters:
        other - the rectangle used to split this one
        Returns:
        if this rectangle had to be split in two parts, the second one is returned. Otherwise, null is returned.
      • vsplit

        public Rectangular vsplit​(Rectangular other)
        If this rectangle intersets with the other one, splits this rectangle horizontally so that it does not intersect with the other one anymore.
        Parameters:
        other - the rectangle used to split this one
        Returns:
        if this rectangle had to be split in two parts, the second one is returned. Otherwise, null is returned.
      • expandToEnclose

        public void expandToEnclose​(Rectangular other)