Record Class Rect

java.lang.Object
java.lang.Record
nl.colorize.multimedialib.math.Rect
All Implemented Interfaces:
Shape

public record Rect(float x, float y, float width, float height) extends Record implements Shape
Immutable two-dimensional rectangle with float precision. Rectangles are specified using their top-left coordinate, width, and height. The rectangle's location permits negative coordinates, but its width and height cannot be negative.
  • Field Summary

    Fields inherited from interface nl.colorize.multimedialib.math.Shape

    EPSILON
  • Constructor Summary

    Constructors
    Constructor
    Description
    Rect(float x, float y, float width, float height)
    Creates an instance of a Rect record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    static Rect
    around(float x, float y, float width, float height)
    Returns a rectangle that has X and Y coordinates so that the specified point becomes its center.
    static Rect
    around(Point2D center, float width, float height)
    Returns a rectangle that has X and Y coordinates so that the specified point becomes its center.
    static Rect
    aroundOrigin(float width, float height)
    Returns a rectangle that has the specified width and height, with its X and Y coordinates positioned so that the origin (0, 0) ends up as the center of the rectangle.
    combine(Rect other)
    Returns a new rectangle that encompasses both this rectangle and the specified other rectangle.
    boolean
    contains(float px, float py)
    Returns true if the specified point is located within this rectangle.
    boolean
    Returns true if the specified point is located within this rectangle.
    boolean
    contains(Rect other)
    Returns true if the specified other rectangle is entirely or partially located within this rectangle.
    final boolean
    Indicates whether some other object is "equal to" this one.
    expand(float amount)
    Expands this rectangle by the specified amount, and returns the resulting new rectangle.
    static Rect
    fromPoints(float x0, float y0, float x1, float y1)
    Factory method that creates a rectangle based on the points (x0, y0) and (x1, y1).
    Returns the smallest possible rectangular bounding box that can fit this shape.
     
    float
     
    float
     
    float
     
    float
     
    final int
    Returns a hash code value for this object.
    float
    Returns the value of the height record component.
    boolean
    Returns true if the specified other retangle intersects with this rectangle.
    Returns a new Shape instance that is repositioned by the specified offset.
     
    Returns a string representation of this record class.
    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 java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface nl.colorize.multimedialib.math.Shape

    reposition
  • Constructor Details

    • Rect

      public Rect(float x, float y, float width, float height)
      Creates an instance of a Rect record class.
      Parameters:
      x - the value for the x record component
      y - the value for the y record component
      width - the value for the width record component
      height - the value for the height record component
  • Method Details

    • getEndX

      public float getEndX()
    • getEndY

      public float getEndY()
    • getCenterX

      public float getCenterX()
    • getCenterY

      public float getCenterY()
    • getCenter

      public Point2D getCenter()
      Specified by:
      getCenter in interface Shape
    • contains

      public boolean contains(Point2D p)
      Returns true if the specified point is located within this rectangle.
      Specified by:
      contains in interface Shape
    • contains

      public boolean contains(float px, float py)
      Returns true if the specified point is located within this rectangle.
    • contains

      public boolean contains(Rect other)
      Returns true if the specified other rectangle is entirely or partially located within this rectangle.
    • intersects

      public boolean intersects(Rect other)
      Returns true if the specified other retangle intersects with this rectangle.
    • combine

      public Rect combine(Rect other)
      Returns a new rectangle that encompasses both this rectangle and the specified other rectangle.
    • getBoundingBox

      public Rect getBoundingBox()
      Description copied from interface: Shape
      Returns the smallest possible rectangular bounding box that can fit this shape.
      Specified by:
      getBoundingBox in interface Shape
    • reposition

      public Rect reposition(Point2D offset)
      Description copied from interface: Shape
      Returns a new Shape instance that is repositioned by the specified offset.
      Specified by:
      reposition in interface Shape
    • expand

      public Rect expand(float amount)
      Expands this rectangle by the specified amount, and returns the resulting new rectangle. The rectangle is expanded around its center, i.e. r.getCenter().equals(r.expand(...).getCenter()). Using a negative value for amount is possible and will result in a rectangle that is smaller than the original.
    • toPolygon

      public Polygon toPolygon()
    • toString

      public 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
    • fromPoints

      public static Rect fromPoints(float x0, float y0, float x1, float y1)
      Factory method that creates a rectangle based on the points (x0, y0) and (x1, y1).
    • around

      public static Rect around(float x, float y, float width, float height)
      Returns a rectangle that has X and Y coordinates so that the specified point becomes its center.
    • around

      public static Rect around(Point2D center, float width, float height)
      Returns a rectangle that has X and Y coordinates so that the specified point becomes its center.
    • aroundOrigin

      public static Rect aroundOrigin(float width, float height)
      Returns a rectangle that has the specified width and height, with its X and Y coordinates positioned so that the origin (0, 0) ends up as the center of the rectangle.
    • 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 '=='.
      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