Class Rect


  • public final class Rect
    extends java.lang.Object
    An immutable rectangle in cell coordinates. The take methods return a strip and the split methods return {strip, remainder}; both leave this rect unchanged.
    • Constructor Summary

      Constructors 
      Constructor Description
      Rect​(int x, int y, int width, int height)
      Creates a rectangle.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int bottom()  
      boolean contains​(int px, int py)  
      boolean equals​(java.lang.Object o)  
      int hashCode()  
      int height()  
      Rect inset​(int amount)
      Shrinks this rect on all four sides.
      Rect intersect​(Rect other)
      Returns the overlap of this rect and other; the result is empty (zero width or height) when they do not overlap.
      boolean isEmpty()  
      int right()  
      Rect[] splitBottom​(int rows)
      Splits off a bottom strip.
      Rect[] splitLeft​(int cols)
      Splits off a left strip.
      Rect[] splitRight​(int cols)
      Splits off a right strip.
      Rect[] splitTop​(int rows)
      Splits off a top strip.
      Rect takeBottom​(int rows)
      Returns the bottom strip of the given height, clamped to this rect.
      Rect takeLeft​(int cols)
      Returns the left strip of the given width, clamped to this rect.
      Rect takeRight​(int cols)
      Returns the right strip of the given width, clamped to this rect.
      Rect takeTop​(int rows)
      Returns the top strip of the given height, clamped to this rect.
      java.lang.String toString()  
      int width()  
      int x()  
      int y()  
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • Rect

        public Rect​(int x,
                    int y,
                    int width,
                    int height)
        Creates a rectangle. Negative width or height is clamped to zero.
        Parameters:
        x - left edge in cells
        y - top edge in cells
        width - width in cells
        height - height in cells
    • Method Detail

      • x

        public int x()
        Returns:
        the left edge in cells
      • y

        public int y()
        Returns:
        the top edge in cells
      • width

        public int width()
        Returns:
        the width in cells
      • height

        public int height()
        Returns:
        the height in cells
      • right

        public int right()
        Returns:
        the exclusive right edge, x + width
      • bottom

        public int bottom()
        Returns:
        the exclusive bottom edge, y + height
      • isEmpty

        public boolean isEmpty()
        Returns:
        true when the width or height is zero
      • contains

        public boolean contains​(int px,
                                int py)
        Parameters:
        px - point x in cells
        py - point y in cells
        Returns:
        true when the absolute point lies inside this rect
      • intersect

        public Rect intersect​(Rect other)
        Returns the overlap of this rect and other; the result is empty (zero width or height) when they do not overlap.
        Parameters:
        other - the rect to intersect with
        Returns:
        the overlapping rect
      • inset

        public Rect inset​(int amount)
        Shrinks this rect on all four sides.
        Parameters:
        amount - cells to remove from each side; dimensions clamp at zero
        Returns:
        a new rect inset by the amount
      • takeTop

        public Rect takeTop​(int rows)
        Returns the top strip of the given height, clamped to this rect.
        Parameters:
        rows - strip height in cells
        Returns:
        the top strip
      • takeBottom

        public Rect takeBottom​(int rows)
        Returns the bottom strip of the given height, clamped to this rect.
        Parameters:
        rows - strip height in cells
        Returns:
        the bottom strip
      • takeLeft

        public Rect takeLeft​(int cols)
        Returns the left strip of the given width, clamped to this rect.
        Parameters:
        cols - strip width in cells
        Returns:
        the left strip
      • takeRight

        public Rect takeRight​(int cols)
        Returns the right strip of the given width, clamped to this rect.
        Parameters:
        cols - strip width in cells
        Returns:
        the right strip
      • splitTop

        public Rect[] splitTop​(int rows)
        Splits off a top strip.
        Parameters:
        rows - strip height in cells
        Returns:
        a two-element array {top, rest}
      • splitBottom

        public Rect[] splitBottom​(int rows)
        Splits off a bottom strip.
        Parameters:
        rows - strip height in cells
        Returns:
        a two-element array {bottom, rest}
      • splitLeft

        public Rect[] splitLeft​(int cols)
        Splits off a left strip.
        Parameters:
        cols - strip width in cells
        Returns:
        a two-element array {left, rest}
      • splitRight

        public Rect[] splitRight​(int cols)
        Splits off a right strip.
        Parameters:
        cols - strip width in cells
        Returns:
        a two-element array {right, rest}
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object