Class Bounds2i

java.lang.Object
com.thecsdev.common.math.Bounds2i
All Implemented Interfaces:
Cloneable

public final class Bounds2i extends Object implements Cloneable
Immutable integer-based 2D rectangle representing a bounding-box. A bounding box is what represents the spatial area a given thing occupies.
API Note:
Primarily for use in 2D graphics and GUI frameworks.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    final int
     
    final int
     
    final int
     
    final boolean
    true if width or height is 0.
    final int
     
    final int
     
    final int
     
    static final Bounds2i
    The default "zero" value whose (x, y, width, height) values are all 0.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Bounds2i(int x, int y, int width, int height)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    final Bounds2i
    add(int x, int y, int width, int height)
    Returns a new Bounds2i instance with the arguments added to it.
    final Bounds2i
     
    final boolean
    contains(int x, int y)
    Returns true if this rectangle contains a 2D point.
    final boolean
    contains(@NotNull Bounds2i other)
    Returns true if this rectangle completely contains another Bounds2i.
    final boolean
     
    final int
     
    final boolean
    hasSamePosition(@NotNull Bounds2i other)
    Returns true if only the x and y of this and the other Bounds2i match.
    final boolean
    hasSameSize(@NotNull Bounds2i other)
    Returns true if only the width and height of this and the other Bounds2i match.
    final Bounds2i
    height(int height)
    Returns a new Bounds2i instance with the given height while retaining the current X, Y and width.
    final boolean
    intersects(@NotNull Bounds2i other)
    Returns true if this rectangle intersects with another Bounds2i.
    final Bounds2i
    position(int x, int y)
    Returns a new Bounds2i instance with the given X and Y position while retaining the current width and height.
    final Bounds2i
    size(int width, int height)
    Returns a new Bounds2i instance with the given width and height while retaining the current X and Y position.
    final Bounds2i
    sub(int x, int y, int width, int height)
    Returns a new Bounds2i instance with the arguments subtracted from it.
    final String
     
    final Bounds2i
    width(int width)
    Returns a new Bounds2i instance with the given width while retaining the current X, Y and height.
    final Bounds2i
    x(int x)
    Returns a new Bounds2i instance with the given X position while retaining the current Y, width and height.
    final Bounds2i
    y(int y)
    Returns a new Bounds2i instance with the given Y position while retaining the current X, width and height.

    Methods inherited from class Object

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

    • ZERO

      public static final Bounds2i ZERO
      The default "zero" value whose (x, y, width, height) values are all 0.
    • x

      public final int x
    • y

      public final int y
    • width

      public final int width
    • height

      public final int height
    • endX

      public final int endX
    • endY

      public final int endY
    • isEmpty

      public final boolean isEmpty
      true if width or height is 0.
  • Constructor Details

    • Bounds2i

      public Bounds2i(int x, int y, int width, int height)
  • Method Details

    • hashCode

      public final int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public final boolean equals(Object obj)
      Overrides:
      equals in class Object
    • clone

      public final Bounds2i clone()
      Overrides:
      clone in class Object
    • toString

      public final String toString()
      Overrides:
      toString in class Object
    • contains

      public final boolean contains(int x, int y)
      Returns true if this rectangle contains a 2D point.
      Parameters:
      x - The point's X position.
      y - The point's Y position.
    • contains

      public final boolean contains(@NotNull @NotNull Bounds2i other) throws NullPointerException
      Returns true if this rectangle completely contains another Bounds2i.
      Parameters:
      other - The other Bounds2i to compare to.
      Throws:
      NullPointerException - If the argument is null.
      API Note:
      This function is mathematically asymmetric.
    • intersects

      public final boolean intersects(@NotNull @NotNull Bounds2i other) throws NullPointerException
      Returns true if this rectangle intersects with another Bounds2i.
      Parameters:
      other - The other Bounds2i to compare to.
      Throws:
      NullPointerException - If the argument is null.
      API Note:
      This function is mathematically symmetric.
    • hasSamePosition

      public final boolean hasSamePosition(@NotNull @NotNull Bounds2i other)
      Returns true if only the x and y of this and the other Bounds2i match.
      Parameters:
      other - The other Bounds2i to compare to.
      Throws:
      NullPointerException - If the argument is null.
    • hasSameSize

      public final boolean hasSameSize(@NotNull @NotNull Bounds2i other)
      Returns true if only the width and height of this and the other Bounds2i match.
      Parameters:
      other - The other Bounds2i to compare to.
      Throws:
      NullPointerException - If the argument is null.
    • add

      public final Bounds2i add(int x, int y, int width, int height)
      Returns a new Bounds2i instance with the arguments added to it.
    • sub

      public final Bounds2i sub(int x, int y, int width, int height)
      Returns a new Bounds2i instance with the arguments subtracted from it.
    • position

      public final Bounds2i position(int x, int y)
      Returns a new Bounds2i instance with the given X and Y position while retaining the current width and height.
    • x

      public final Bounds2i x(int x)
      Returns a new Bounds2i instance with the given X position while retaining the current Y, width and height.
    • y

      public final Bounds2i y(int y)
      Returns a new Bounds2i instance with the given Y position while retaining the current X, width and height.
    • size

      public final Bounds2i size(int width, int height)
      Returns a new Bounds2i instance with the given width and height while retaining the current X and Y position.
    • width

      public final Bounds2i width(int width)
      Returns a new Bounds2i instance with the given width while retaining the current X, Y and height.
    • height

      public final Bounds2i height(int height)
      Returns a new Bounds2i instance with the given height while retaining the current X, Y and width.