Class Rectangle

    • Field Detail

      • tmp

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

        public static final Rectangle 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 Detail

      • Rectangle

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

        public Rectangle​(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
      • Rectangle

        public Rectangle​(Rectangle rect)
        Constructs a rectangle based on the given rectangle
        Parameters:
        rect - The rectangle
    • Method Detail

      • set

        public Rectangle 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 Rectangle 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 Rectangle 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 Rectangle 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 Rectangle setHeight​(float height)
        Sets the height of this rectangle
        Parameters:
        height - The height
        Returns:
        this rectangle for chaining
      • getPosition

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

        public Rectangle setPosition​(Vector2 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 Rectangle 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
      • setSize

        public Rectangle 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 Rectangle setSize​(float sizeXY)
        Sets the squared size of this rectangle
        Parameters:
        sizeXY - The size
        Returns:
        this rectangle for chaining
      • getSize

        public Vector2 getSize​(Vector2 size)
        Parameters:
        size - The Vector2
        Returns:
        the Vector2 with size of this rectangle
      • 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​(Vector2 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​(Rectangle rectangle)
        Parameters:
        rectangle - the other Rectangle.
        Returns:
        whether the other rectangle is contained in this rectangle.
      • overlaps

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

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

        public Rectangle merge​(Rectangle 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
      • merge

        public Rectangle 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 Rectangle merge​(Vector2 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 Rectangle merge​(Vector2[] 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 Vector2 getCenter​(Vector2 vector)
        Calculates the center of the rectangle. Results are located in the given Vector2
        Parameters:
        vector - the Vector2 to use
        Returns:
        the given vector with results stored inside
      • setCenter

        public Rectangle 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 Rectangle setCenter​(Vector2 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 Rectangle fitOutside​(Rectangle 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:
        Scaling
      • fitInside

        public Rectangle fitInside​(Rectangle 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:
        Scaling
      • toString

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

        public Rectangle fromString​(java.lang.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 java.lang.Object
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object