Package arc.math.geom

Class Rect

java.lang.Object
arc.math.geom.Rect
All Implemented Interfaces:
Shape2D
Direct Known Subclasses:
PixmapPacker.PixmapPackerRect

public class Rect extends Object implements Shape2D
Encapsulates a 2D rectangle defined by its corner point in the bottom left and its extents in x (width) and y (height).
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    float
     
    static final Rect
    Static temporary rectangle.
    static final Rect
    Static temporary rectangle.
    float
     
    float
     
    float
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new rectangle with all values set to zero
    Rect(float x, float y, float width, float height)
    Constructs a new rectangle with the given corner point in the bottom left and dimensions.
    Rect(Rect rect)
    Constructs a rectangle based on the given rectangle
  • Method Summary

    Modifier and Type
    Method
    Description
    float
     
    boolean
    contains(float x, float y)
    Returns whether a point with the given coordinates is contained within the shape.
    static boolean
    contains(float x, float y, float width, float height, float px, float py)
     
    boolean
    contains(Circle circle)
     
    boolean
    contains(Rect rect)
     
    boolean
    contains(Vec2 point)
    Returns whether the given point is contained within the shape.
    boolean
     
    Fits this rectangle into another rectangle while maintaining aspect ratio.
    Fits this rectangle around another rectangle while maintaining aspect ratio.
    Sets this Rectangle to the value represented by the specified string according to the format of toString() .
    float
    Calculates the aspect ratio ( width / height ) of this rectangle
    getCenter(Vec2 vector)
    Calculates the center of the rectangle.
    float
     
    getPosition(Vec2 position)
    return the Vec2 with coordinates of this rectangle
    getSize(Vec2 size)
     
    float
     
    float
     
    float
     
    grow(float amount)
     
    grow(float amountX, float amountY)
     
    int
     
    merge(float x, float y)
    Merges this rectangle with a point.
    merge(Rect rect)
    Merges this rectangle with the other rectangle.
    merge(Vec2 vec)
    Merges this rectangle with a point.
    merge(Vec2[] vecs)
    Merges this rectangle with a list of points.
    move(float cx, float cy)
     
    "fixes" negative size dimensions.
    boolean
    overlaps(float rx, float ry, float rwidth, float rheight)
     
    boolean
     
    float
     
    set(float x, float y, float width, float height)
     
    set(Rect rect)
    Sets the values of the given rectangle to this rectangle.
    setCenter(float x, float y)
    Moves this rectangle so that its center point is located at a given position
    setCenter(Vec2 position)
    Moves this rectangle so that its center point is located at a given position
    setCentered(float x, float y, float size)
     
    setCentered(float x, float y, float width, float height)
     
    setHeight(float height)
    Sets the height of this rectangle
    setPosition(float x, float y)
    Sets the x and y-coordinates of the bottom left corner
    setPosition(Vec2 position)
    Sets the x and y-coordinates of the bottom left corner from vector
    setSize(float sizeXY)
    Sets the squared size of this rectangle
    setSize(float width, float height)
    Sets the width and height of this rectangle
    setWidth(float width)
    Sets the width of this rectangle
    setX(float x)
    Sets the x-coordinate of the bottom left corner
    setY(float y)
    Sets the y-coordinate of the bottom left corner
    Converts this Rectangle to a string in the format [x,y,width,height].

    Methods inherited from class java.lang.Object

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

    • tmp

      public static final Rect tmp
      Static temporary rectangle. Use with care! Use only when sure other code will not also use this.
    • tmp2

      public static final Rect tmp2
      Static temporary rectangle. Use with care! Use only when sure other code will not also use this.
    • x

      public float x
    • y

      public float y
    • width

      public float width
    • height

      public float height
  • Constructor Details

    • Rect

      public Rect()
      Constructs a new rectangle with all values set to zero
    • Rect

      public Rect(float x, float y, float width, float height)
      Constructs a new rectangle with the given corner point in the bottom left and dimensions.
      Parameters:
      x - The corner point x-coordinate
      y - The corner point y-coordinate
      width - The width
      height - The height
    • Rect

      public Rect(Rect rect)
      Constructs a rectangle based on the given rectangle
      Parameters:
      rect - The rectangle
  • Method Details

    • setCentered

      public Rect setCentered(float x, float y, float size)
    • setCentered

      public Rect setCentered(float x, float y, float width, float height)
    • set

      public Rect set(float x, float y, float width, float height)
      Parameters:
      x - bottom-left x coordinate
      y - bottom-left y coordinate
      width - width
      height - height
      Returns:
      this rectangle for chaining
    • getX

      public float getX()
      Returns:
      the x-coordinate of the bottom left corner
    • setX

      public Rect setX(float x)
      Sets the x-coordinate of the bottom left corner
      Parameters:
      x - The x-coordinate
      Returns:
      this rectangle for chaining
    • getY

      public float getY()
      Returns:
      the y-coordinate of the bottom left corner
    • setY

      public Rect setY(float y)
      Sets the y-coordinate of the bottom left corner
      Parameters:
      y - The y-coordinate
      Returns:
      this rectangle for chaining
    • getWidth

      public float getWidth()
      Returns:
      the width
    • setWidth

      public Rect setWidth(float width)
      Sets the width of this rectangle
      Parameters:
      width - The width
      Returns:
      this rectangle for chaining
    • getHeight

      public float getHeight()
      Returns:
      the height
    • setHeight

      public Rect setHeight(float height)
      Sets the height of this rectangle
      Parameters:
      height - The height
      Returns:
      this rectangle for chaining
    • getPosition

      public Vec2 getPosition(Vec2 position)
      return the Vec2 with coordinates of this rectangle
      Parameters:
      position - The Vec2
    • setPosition

      public Rect setPosition(Vec2 position)
      Sets the x and y-coordinates of the bottom left corner from vector
      Parameters:
      position - The position vector
      Returns:
      this rectangle for chaining
    • setPosition

      public Rect setPosition(float x, float y)
      Sets the x and y-coordinates of the bottom left corner
      Parameters:
      x - The x-coordinate
      y - The y-coordinate
      Returns:
      this rectangle for chaining
    • move

      public Rect move(float cx, float cy)
    • setSize

      public Rect setSize(float width, float height)
      Sets the width and height of this rectangle
      Parameters:
      width - The width
      height - The height
      Returns:
      this rectangle for chaining
    • setSize

      public Rect setSize(float sizeXY)
      Sets the squared size of this rectangle
      Parameters:
      sizeXY - The size
      Returns:
      this rectangle for chaining
    • getSize

      public Vec2 getSize(Vec2 size)
      Parameters:
      size - The Vec2
      Returns:
      the Vec2 with size of this rectangle
    • contains

      public static boolean contains(float x, float y, float width, float height, float px, float py)
    • contains

      public boolean contains(float x, float y)
      Description copied from interface: Shape2D
      Returns whether a point with the given coordinates is contained within the shape.
      Specified by:
      contains in interface Shape2D
      Parameters:
      x - point x coordinate
      y - point y coordinate
      Returns:
      whether the point is contained in the rectangle
    • contains

      public boolean contains(Vec2 point)
      Description copied from interface: Shape2D
      Returns whether the given point is contained within the shape.
      Specified by:
      contains in interface Shape2D
      Parameters:
      point - The coordinates vector
      Returns:
      whether the point is contained in the rectangle
    • contains

      public boolean contains(Circle circle)
      Parameters:
      circle - the circle
      Returns:
      whether the circle is contained in the rectangle
    • contains

      public boolean contains(Rect rect)
      Parameters:
      rect - the other Rect.
      Returns:
      whether the other rectangle is contained in this rectangle.
    • overlaps

      public boolean overlaps(Rect r)
      Parameters:
      r - the other Rect
      Returns:
      whether this rectangle overlaps the other rectangle.
    • overlaps

      public boolean overlaps(float rx, float ry, float rwidth, float rheight)
      Returns:
      whether this rectangle overlaps the other rectangle.
    • set

      public Rect set(Rect rect)
      Sets the values of the given rectangle to this rectangle.
      Parameters:
      rect - the other rectangle
      Returns:
      this rectangle for chaining
    • grow

      public Rect grow(float amount)
    • grow

      public Rect grow(float amountX, float amountY)
    • merge

      public Rect merge(Rect rect)
      Merges this rectangle with the other rectangle. The rectangle should not have negative width or negative height.
      Parameters:
      rect - the other rectangle
      Returns:
      this rectangle for chaining
    • normalize

      public Rect normalize()
      "fixes" negative size dimensions.
    • merge

      public Rect merge(float x, float y)
      Merges this rectangle with a point. The rectangle should not have negative width or negative height.
      Parameters:
      x - the x coordinate of the point
      y - the y coordinate of the point
      Returns:
      this rectangle for chaining
    • merge

      public Rect merge(Vec2 vec)
      Merges this rectangle with a point. The rectangle should not have negative width or negative height.
      Parameters:
      vec - the vector describing the point
      Returns:
      this rectangle for chaining
    • merge

      public Rect merge(Vec2[] vecs)
      Merges this rectangle with a list of points. The rectangle should not have negative width or negative height.
      Parameters:
      vecs - the vectors describing the points
      Returns:
      this rectangle for chaining
    • getAspectRatio

      public float getAspectRatio()
      Calculates the aspect ratio ( width / height ) of this rectangle
      Returns:
      the aspect ratio of this rectangle. Returns Float.NaN if height is 0 to avoid ArithmeticException
    • getCenter

      public Vec2 getCenter(Vec2 vector)
      Calculates the center of the rectangle. Results are located in the given Vec2
      Parameters:
      vector - the Vec2 to use
      Returns:
      the given vector with results stored inside
    • setCenter

      public Rect setCenter(float x, float y)
      Moves this rectangle so that its center point is located at a given position
      Parameters:
      x - the position's x
      y - the position's y
      Returns:
      this for chaining
    • setCenter

      public Rect setCenter(Vec2 position)
      Moves this rectangle so that its center point is located at a given position
      Parameters:
      position - the position
      Returns:
      this for chaining
    • fitOutside

      public Rect fitOutside(Rect rect)
      Fits this rectangle around another rectangle while maintaining aspect ratio. This scales and centers the rectangle to the other rectangle (e.g. Having a camera translate and scale to show a given area)
      Parameters:
      rect - the other rectangle to fit this rectangle around
      Returns:
      this rectangle for chaining
      See Also:
    • fitInside

      public Rect fitInside(Rect rect)
      Fits this rectangle into another rectangle while maintaining aspect ratio. This scales and centers the rectangle to the other rectangle (e.g. Scaling a texture within a arbitrary cell without squeezing)
      Parameters:
      rect - the other rectangle to fit this rectangle inside
      Returns:
      this rectangle for chaining
      See Also:
    • toString

      public String toString()
      Converts this Rectangle to a string in the format [x,y,width,height].
      Overrides:
      toString in class Object
      Returns:
      a string representation of this object.
    • fromString

      public Rect fromString(String v)
      Sets this Rectangle to the value represented by the specified string according to the format of toString() .
      Parameters:
      v - the string.
      Returns:
      this rectangle for chaining
    • area

      public float area()
    • perimeter

      public float perimeter()
    • hashCode

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

      public boolean equals(Object obj)
      Overrides:
      equals in class Object