Record Class Rect

java.lang.Object
java.lang.Record
org.inspirenxe.strata.api.geometry.Rect
Record Components:
x - the left edge
y - the top edge
width - the width; zero if negative or NaN
height - the height; zero if negative or NaN

public record Rect(float x, float y, float width, float height) extends Record
An axis-aligned rectangle in UI units.

Width and height are never negative: the constructor clamps a negative size to zero, so shrinking a rectangle by more than its size yields an empty rectangle at the shrunk position instead of an inverted one. A rectangle covers the half-open range [x, x + width) by [y, y + height).

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Rect
    The empty rectangle at the origin.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Rect(float x, float y, float width, float height)
    Creates a rectangle, making a negative or NaN width or height zero.
  • Method Summary

    Modifier and Type
    Method
    Description
    float
    Returns the bottom edge, y + height.
    boolean
    contains(float px, float py)
    Returns whether a point lies inside the rectangle.
    final boolean
    Indicates whether some other object is "equal to" this one.
    grow(Insets insets)
    Moves each edge outward by the matching inset.
    final int
    Returns a hash code value for this object.
    float
    Returns the value of the height record component.
    intersect(Rect other)
    Returns the overlap of two rectangles.
    boolean
    Returns whether two rectangles overlap.
    boolean
    Returns whether the rectangle covers no area.
    static Rect
    ofEdges(float left, float top, float right, float bottom)
    Creates a rectangle from its edges.
    float
    Returns the right edge, x + width.
    shrink(Insets insets)
    Moves each edge inward by the matching inset.
    final String
    Returns a string representation of this record class.
    translate(float dx, float dy)
    Returns the same rectangle moved by an offset.
    float
    Returns the value of the width record component.
    float
    x()
    Returns the value of the x record component.
    float
    y()
    Returns the value of the y record component.

    Methods inherited from class Object

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

    • ZERO

      public static final Rect ZERO
      The empty rectangle at the origin.
  • Constructor Details

    • Rect

      public Rect(float x, float y, float width, float height)
      Creates a rectangle, making a negative or NaN width or height zero.
  • Method Details

    • ofEdges

      public static Rect ofEdges(float left, float top, float right, float bottom)
      Creates a rectangle from its edges.
      Parameters:
      left - the left edge
      top - the top edge
      right - the right edge; a value left of left gives zero width
      bottom - the bottom edge; a value above top gives zero height
      Returns:
      the rectangle between the edges
    • right

      public float right()
      Returns the right edge, x + width. The edge itself is outside the rectangle.
      Returns:
      the right edge
    • bottom

      public float bottom()
      Returns the bottom edge, y + height. The edge itself is outside the rectangle.
      Returns:
      the bottom edge
    • isEmpty

      public boolean isEmpty()
      Returns whether the rectangle covers no area.
      Returns:
      true if the width or the height is zero
    • contains

      public boolean contains(float px, float py)
      Returns whether a point lies inside the rectangle. The left and top edges are inside; the right and bottom edges are outside, so two rectangles that share an edge never both contain a point on it.
      Parameters:
      px - the point's x
      py - the point's y
      Returns:
      true if the point is inside
    • shrink

      public Rect shrink(Insets insets)
      Moves each edge inward by the matching inset. Insets larger than the rectangle give an empty rectangle.
      Parameters:
      insets - how far to move each edge
      Returns:
      the smaller rectangle
    • grow

      public Rect grow(Insets insets)
      Moves each edge outward by the matching inset. The inverse of shrink(Insets) for rectangles large enough not to be clamped.
      Parameters:
      insets - how far to move each edge
      Returns:
      the larger rectangle
    • translate

      public Rect translate(float dx, float dy)
      Returns the same rectangle moved by an offset.
      Parameters:
      dx - the horizontal offset
      dy - the vertical offset
      Returns:
      the moved rectangle
    • intersect

      public Rect intersect(Rect other)
      Returns the overlap of two rectangles. Rectangles that do not overlap give an empty rectangle placed where the overlap would start.
      Parameters:
      other - the other rectangle
      Returns:
      the area both rectangles cover
    • intersects

      public boolean intersects(Rect other)
      Returns whether two rectangles overlap. Rectangles that only share an edge do not, and an empty rectangle overlaps nothing, since it has no area.
      Parameters:
      other - the other rectangle
      Returns:
      true if some area lies inside both
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with the compare method from their corresponding wrapper classes.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • x

      public float x()
      Returns the value of the x record component.
      Returns:
      the value of the x record component
    • y

      public float y()
      Returns the value of the y record component.
      Returns:
      the value of the y record component
    • width

      public float width()
      Returns the value of the width record component.
      Returns:
      the value of the width record component
    • height

      public float height()
      Returns the value of the height record component.
      Returns:
      the value of the height record component