Class Rect

java.lang.Object
icyllis.modernui.graphics.Rect

public class Rect extends Object
Represents a rectangle.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    int
     
    int
     
    int
     
    int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a new Rect with all coordinates initialized to 0.
    Rect(int left, int top, int right, int bottom)
    Create a new rectangle with the specified coordinates.
    Create a new rectangle, initialized with the values in the specified rectangle (which is left unmodified).
  • Method Summary

    Modifier and Type
    Method
    Description
    final int
     
    final int
     
    boolean
    contains(int x, int y)
    Returns true if (x,y) is inside the rectangle.
    boolean
    contains(int left, int top, int right, int bottom)
    Returns true if the 4 specified sides of a rectangle are inside or equal to this rectangle.
    boolean
    Returns true if the specified rectangle r is inside or equal to this rectangle.
     
    static Rect
    Returns a copy of r if it is not null, or an empty Rect otherwise.
    boolean
     
    final float
     
    final float
     
    int
     
    final int
     
    void
    inset(int dx, int dy)
    Inset the rectangle by (dx,dy).
    void
    inset(int left, int top, int right, int bottom)
    Insets the rectangle on all sides specified by the insets.
    void
    inset(Rect insets)
    Insets the rectangle on all sides specified by the dimensions of the insets rectangle.
    boolean
    intersect(int left, int top, int right, int bottom)
    If the rectangle specified by left,top,right,bottom intersects this rectangle, return true and set this rectangle to that intersection, otherwise return false and do not change this rectangle.
    boolean
    If the specified rectangle intersects this rectangle, return true and set this rectangle to that intersection, otherwise return false and do not change this rectangle.
    boolean
    intersects(int left, int top, int right, int bottom)
    Returns true if this rectangle intersects the specified rectangle.
    static boolean
    Returns true if the two specified rectangles intersect.
    void
    If the specified rectangle intersects this rectangle, set this rectangle to that intersection, otherwise set this rectangle to the empty rectangle.
    final boolean
    Returns true if the rectangle is empty (left >= right or top >= bottom)
    final void
    join(int left, int top, int right, int bottom)
    Update this Rect to enclose itself and the specified rectangle.
    void
    offset(int dx, int dy)
    Offset the rectangle by adding dx to its left and right coordinates, and adding dy to its top and bottom coordinates.
    void
    offsetTo(int newLeft, int newTop)
    Offset the rectangle to a specific (left, top) position, keeping its width and height the same.
    void
    set(int left, int top, int right, int bottom)
    Set the rectangle's coordinates to the specified values.
    void
    set(Rect src)
    Copy the coordinates from src into this rectangle.
    void
    Set the rectangle to (0,0,0,0)
    boolean
    If rectangles a and b intersect, return true and set this rectangle to that intersection, otherwise return false and do not change this rectangle.
    void
    Swap top/bottom or left/right if there are flipped (i.e.
    Return a string representation of the rectangle in a compact form.
     
    void
    union(int x, int y)
    Update this Rect to enclose itself and the [x,y] coordinate.
    void
    union(int left, int top, int right, int bottom)
    Update this Rect to enclose itself and the specified rectangle.
    void
    Update this Rect to enclose itself and the specified rectangle.
    final int
     
    final int
    x()
    Returns the rectangle's left.
    final int
    y()
    Return the rectangle's top.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • left

      public int left
    • top

      public int top
    • bottom

      public int bottom
  • Constructor Details

    • Rect

      public Rect()
      Create a new Rect with all coordinates initialized to 0.
    • Rect

      public Rect(int left, int top, int right, int bottom)
      Create a new rectangle with the specified coordinates. Note: no range checking is performed, so the caller must ensure that left invalid input: '<'= right and top invalid input: '<'= bottom.
      Parameters:
      left - The X coordinate of the left side of the rectangle
      top - The Y coordinate of the top of the rectangle
      right - The X coordinate of the right side of the rectangle
      bottom - The Y coordinate of the bottom of the rectangle
    • Rect

      public Rect(@Nullable Rect r)
      Create a new rectangle, initialized with the values in the specified rectangle (which is left unmodified).
      Parameters:
      r - The rectangle whose coordinates are copied into the new rectangle.
  • Method Details

    • copy

      @NonNull public static Rect copy(@Nullable Rect r)
      Returns a copy of r if it is not null, or an empty Rect otherwise.
      Parameters:
      r - the rect to copy from
    • isEmpty

      public final boolean isEmpty()
      Returns true if the rectangle is empty (left >= right or top >= bottom)
    • x

      public final int x()
      Returns the rectangle's left.
    • y

      public final int y()
      Return the rectangle's top.
    • width

      public final int width()
      Returns:
      the rectangle's width. This does not check for a valid rectangle (i.e. left invalid input: '<'= right) so the result may be negative.
    • height

      public final int height()
      Returns:
      the rectangle's height. This does not check for a valid rectangle (i.e. top invalid input: '<'= bottom) so the result may be negative.
    • centerX

      public final int centerX()
      Returns:
      the horizontal center of the rectangle. If the computed value is fractional, this method returns the largest integer that is less than the computed value.
    • centerY

      public final int centerY()
      Returns:
      the vertical center of the rectangle. If the computed value is fractional, this method returns the largest integer that is less than the computed value.
    • exactCenterX

      public final float exactCenterX()
      Returns:
      the exact horizontal center of the rectangle as a float.
    • exactCenterY

      public final float exactCenterY()
      Returns:
      the exact vertical center of the rectangle as a float.
    • setEmpty

      public void setEmpty()
      Set the rectangle to (0,0,0,0)
    • set

      public void set(int left, int top, int right, int bottom)
      Set the rectangle's coordinates to the specified values. Note: no range checking is performed, so it is up to the caller to ensure that left invalid input: '<'= right and top invalid input: '<'= bottom.
      Parameters:
      left - The X coordinate of the left side of the rectangle
      top - The Y coordinate of the top of the rectangle
      right - The X coordinate of the right side of the rectangle
      bottom - The Y coordinate of the bottom of the rectangle
    • set

      public void set(@NonNull Rect src)
      Copy the coordinates from src into this rectangle.
      Parameters:
      src - The rectangle whose coordinates are copied into this rectangle.
    • offset

      public void offset(int dx, int dy)
      Offset the rectangle by adding dx to its left and right coordinates, and adding dy to its top and bottom coordinates.
      Parameters:
      dx - The amount to add to the rectangle's left and right coordinates
      dy - The amount to add to the rectangle's top and bottom coordinates
    • offsetTo

      public void offsetTo(int newLeft, int newTop)
      Offset the rectangle to a specific (left, top) position, keeping its width and height the same.
      Parameters:
      newLeft - The new "left" coordinate for the rectangle
      newTop - The new "top" coordinate for the rectangle
    • inset

      public void inset(int dx, int dy)
      Inset the rectangle by (dx,dy). If dx is positive, then the sides are moved inwards, making the rectangle narrower. If dx is negative, then the sides are moved outwards, making the rectangle wider. The same holds true for dy and the top and bottom.
      Parameters:
      dx - The amount to add(subtract) from the rectangle's left(right)
      dy - The amount to add(subtract) from the rectangle's top(bottom)
    • inset

      public void inset(@NonNull Rect insets)
      Insets the rectangle on all sides specified by the dimensions of the insets rectangle.
      Parameters:
      insets - The rectangle specifying the insets on all side.
    • inset

      public void inset(int left, int top, int right, int bottom)
      Insets the rectangle on all sides specified by the insets.
      Parameters:
      left - The amount to add from the rectangle's left
      top - The amount to add from the rectangle's top
      right - The amount to subtract from the rectangle's right
      bottom - The amount to subtract from the rectangle's bottom
    • contains

      public boolean contains(int x, int y)
      Returns true if (x,y) is inside the rectangle. The left and top are considered to be inside, while the right and bottom are not. This means that for a (x,y) to be contained: left invalid input: '<'= x invalid input: '<' right and top invalid input: '<'= y invalid input: '<' bottom. An empty rectangle never contains any point.
      Parameters:
      x - The X coordinate of the point being tested for containment
      y - The Y coordinate of the point being tested for containment
      Returns:
      true if (x,y) are contained by the rectangle, where containment means left invalid input: '<'= x invalid input: '<' right and top invalid input: '<'= y invalid input: '<' bottom
    • contains

      public boolean contains(int left, int top, int right, int bottom)
      Returns true if the 4 specified sides of a rectangle are inside or equal to this rectangle. i.e. is this rectangle a superset of the specified rectangle. An empty rectangle never contains another rectangle.
      Parameters:
      left - The left side of the rectangle being tested for containment
      top - The top of the rectangle being tested for containment
      right - The right side of the rectangle being tested for containment
      bottom - The bottom of the rectangle being tested for containment
      Returns:
      true if the 4 specified sides of a rectangle are inside or equal to this rectangle
    • contains

      public boolean contains(@NonNull Rect r)
      Returns true if the specified rectangle r is inside or equal to this rectangle. An empty rectangle never contains another rectangle.
      Parameters:
      r - The rectangle being tested for containment.
      Returns:
      true if the specified rectangle r is inside or equal to this rectangle
    • intersect

      public boolean intersect(int left, int top, int right, int bottom)
      If the rectangle specified by left,top,right,bottom intersects this rectangle, return true and set this rectangle to that intersection, otherwise return false and do not change this rectangle. No check is performed to see if either rectangle is empty. Note: To just test for intersection, use intersects(Rect, Rect).
      Parameters:
      left - The left side of the rectangle being intersected with this rectangle
      top - The top of the rectangle being intersected with this rectangle
      right - The right side of the rectangle being intersected with this rectangle.
      bottom - The bottom of the rectangle being intersected with this rectangle.
      Returns:
      true if the specified rectangle and this rectangle intersect (and this rectangle is then set to that intersection) else return false and do not change this rectangle.
    • intersect

      public boolean intersect(@NonNull Rect r)
      If the specified rectangle intersects this rectangle, return true and set this rectangle to that intersection, otherwise return false and do not change this rectangle. No check is performed to see if either rectangle is empty. To just test for intersection, use intersects()
      Parameters:
      r - The rectangle being intersected with this rectangle.
      Returns:
      true if the specified rectangle and this rectangle intersect (and this rectangle is then set to that intersection) else return false and do not change this rectangle.
    • intersectUnchecked

      public void intersectUnchecked(@NonNull Rect other)
      If the specified rectangle intersects this rectangle, set this rectangle to that intersection, otherwise set this rectangle to the empty rectangle.
      See Also:
    • setIntersect

      public boolean setIntersect(@NonNull Rect a, @NonNull Rect b)
      If rectangles a and b intersect, return true and set this rectangle to that intersection, otherwise return false and do not change this rectangle. No check is performed to see if either rectangle is empty. To just test for intersection, use intersects()
      Parameters:
      a - The first rectangle being intersected with
      b - The second rectangle being intersected with
      Returns:
      true if the two specified rectangles intersect. If they do, set this rectangle to that intersection. If they do not, return false and do not change this rectangle.
    • intersects

      public boolean intersects(int left, int top, int right, int bottom)
      Returns true if this rectangle intersects the specified rectangle. In no event is this rectangle modified. No check is performed to see if either rectangle is empty. To record the intersection, use intersect() or setIntersect().
      Parameters:
      left - The left side of the rectangle being tested for intersection
      top - The top of the rectangle being tested for intersection
      right - The right side of the rectangle being tested for intersection
      bottom - The bottom of the rectangle being tested for intersection
      Returns:
      true if the specified rectangle intersects this rectangle. In no event is this rectangle modified.
    • intersects

      public static boolean intersects(@NonNull Rect a, @NonNull Rect b)
      Returns true if the two specified rectangles intersect. In no event are either of the rectangles modified. To record the intersection, use intersect(Rect) or setIntersect(Rect, Rect).
      Parameters:
      a - The first rectangle being tested for intersection
      b - The second rectangle being tested for intersection
      Returns:
      true if the two specified rectangles intersect. In no event are either of the rectangles modified.
    • join

      public final void join(int left, int top, int right, int bottom)
      Update this Rect to enclose itself and the specified rectangle. If the specified rectangle is empty, nothing is done. If this rectangle is empty it is set to the specified rectangle.
      Parameters:
      left - the left edge being unioned with this rectangle
      top - the top edge being unioned with this rectangle
      right - the right edge being unioned with this rectangle
      bottom - the bottom edge being unioned with this rectangle
    • union

      public void union(int left, int top, int right, int bottom)
      Update this Rect to enclose itself and the specified rectangle. If the specified rectangle is empty, nothing is done. If this rectangle is empty it is set to the specified rectangle.
      Parameters:
      left - The left edge being unioned with this rectangle
      top - The top edge being unioned with this rectangle
      right - The right edge being unioned with this rectangle
      bottom - The bottom edge being unioned with this rectangle
    • union

      public void union(@NonNull Rect r)
      Update this Rect to enclose itself and the specified rectangle. If the specified rectangle is empty, nothing is done. If this rectangle is empty it is set to the specified rectangle.
      Parameters:
      r - The rectangle being unioned with this rectangle
    • union

      public void union(int x, int y)
      Update this Rect to enclose itself and the [x,y] coordinate. There is no check to see that this rectangle is non-empty.
      Parameters:
      x - The x coordinate of the point to add to the rectangle
      y - The y coordinate of the point to add to the rectangle
    • sort

      public void sort()
      Swap top/bottom or left/right if there are flipped (i.e. left > right and/or top > bottom). This can be called if the edges are computed separately, and may have crossed over each other. If the edges are already correct (i.e. left invalid input: '<'= right and top invalid input: '<'= bottom) then nothing is done.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

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

      public String toString()
      Overrides:
      toString in class Object
    • toShortString

      @NonNull public String toShortString()
      Return a string representation of the rectangle in a compact form.
    • copy

      @NonNull public Rect copy()