APIdia Launch Partner
Top Description Fields Constructors Methods
com.badlogic.gdx.math

public Class Rectangle

extends Object
implements Serializable, Shape2D
Class Inheritance
All Implemented Interfaces
com.badlogic.gdx.math.Shape2D, java.io.Serializable
Known Direct Subclasses
com.badlogic.gdx.scenes.scene2d.ui.Table.DebugRect
Imports
java.io.Serializable, com.badlogic.gdx.utils.GdxRuntimeException, .NumberUtils, .Scaling

Encapsulates a 2D rectangle defined by its corner point in the bottom left and its extents in x (width) and y (height).
Author
[email protected]

Field Summary

Modifier and TypeField and Description
public float
private static final long
public static final Rectangle
tmp

Static temporary rectangle.

public static final Rectangle
tmp2

Static temporary rectangle.

public float
public float
x

public float
y

Constructor Summary

AccessConstructor and Description
public
Rectangle()

Constructs a new rectangle with all values set to zero

public
Rectangle(float
The corner point x-coordinate
x
,
float
The corner point y-coordinate
y
,
float
The width
width
,
float
The height
height
)

Constructs a new rectangle with the given corner point in the bottom left and dimensions.

public
Rectangle(Rectangle
The rectangle
rect
)

Constructs a rectangle based on the given rectangle

Method Summary

Modifier and TypeMethod and Description
public float
area()

public boolean

Returns:

whether the point is contained in the rectangle
contains
(float
point x coordinate
x
,
float
point y coordinate
y
)

Implements com.badlogic.gdx.math.Shape2D.contains.

Returns whether a point with the given coordinates is contained within the shape.

public boolean

Returns:

whether the point is contained in the rectangle
contains
(Vector2
The coordinates vector
point
)

Implements com.badlogic.gdx.math.Shape2D.contains.

Returns whether the given point is contained within the shape.

public boolean
contains(Circle
the circle
circle
)

Returns: whether the circle is contained in the rectangle

public boolean
contains(Rectangle
the other Rectangle.
rectangle
)

Returns: whether the other rectangle is contained in this rectangle.

public boolean
equals(Object obj)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public Rectangle

Returns:

this rectangle for chaining
fitInside
(Rectangle
the other rectangle to fit this rectangle inside
rect
)

Fits this rectangle into another rectangle while maintaining aspect ratio.

public Rectangle

Returns:

this rectangle for chaining
fitOutside
(Rectangle
the other rectangle to fit this rectangle around
rect
)

Fits this rectangle around another rectangle while maintaining aspect ratio.

public Rectangle

Returns:

this rectangle for chaining
fromString
(String
the string.
v
)

Sets this Rectangle to the value represented by the specified string according to the format of toString().

public float

Returns:

the aspect ratio of this rectangle. Returns Float.NaN if height is 0 to avoid ArithmeticException
getAspectRatio
()

Calculates the aspect ratio ( width / height ) of this rectangle

public Vector2

Returns:

the given vector with results stored inside
getCenter
(Vector2
the Vector2 to use
vector
)

Calculates the center of the rectangle.

public float
getHeight()

Returns: the height

public Vector2
getPosition(Vector2
The Vector2
position
)

return the Vector2 with coordinates of this rectangle

public Vector2
getSize(Vector2
The Vector2
size
)

Returns: the Vector2 with size of this rectangle

public float
getWidth()

Returns: the width

public float
getX()

Returns: the x-coordinate of the bottom left corner

public float
getY()

Returns: the y-coordinate of the bottom left corner

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public Rectangle

Returns:

this rectangle for chaining
merge
(Rectangle
the other rectangle
rect
)

Merges this rectangle with the other rectangle.

public Rectangle

Returns:

this rectangle for chaining
merge
(float
the x coordinate of the point
x
,
float
the y coordinate of the point
y
)

Merges this rectangle with a point.

public Rectangle

Returns:

this rectangle for chaining
merge
(Vector2
the vector describing the point
vec
)

Merges this rectangle with a point.

public Rectangle

Returns:

this rectangle for chaining
merge
(Vector2[]
the vectors describing the points
vecs
)

Merges this rectangle with a list of points.

public boolean
overlaps(Rectangle
the other Rectangle
r
)

Returns: whether this rectangle overlaps the other rectangle.

public float
public Rectangle
set(float
bottom-left x coordinate
x
,
float
bottom-left y coordinate
y
,
float
width
width
,
float
height
height
)

Returns: this rectangle for chaining

public Rectangle

Returns:

this rectangle for chaining
set
(Rectangle
the other rectangle
rect
)

Sets the values of the given rectangle to this rectangle.

public Rectangle

Returns:

this for chaining
setCenter
(float
the position's x
x
,
float
the position's y
y
)

Moves this rectangle so that its center point is located at a given position

public Rectangle

Returns:

this for chaining
setCenter
(Vector2
the position
position
)

Moves this rectangle so that its center point is located at a given position

public Rectangle

Returns:

this rectangle for chaining
setHeight
(float
The height
height
)

Sets the height of this rectangle

public Rectangle

Returns:

this rectangle for chaining
setPosition
(Vector2
The position vector
position
)

Sets the x and y-coordinates of the bottom left corner from vector

public Rectangle

Returns:

this rectangle for chaining
setPosition
(float
The x-coordinate
x
,
float
The y-coordinate
y
)

Sets the x and y-coordinates of the bottom left corner

public Rectangle

Returns:

this rectangle for chaining
setSize
(float
The width
width
,
float
The height
height
)

Sets the width and height of this rectangle

public Rectangle

Returns:

this rectangle for chaining
setSize
(float
The size
sizeXY
)

Sets the squared size of this rectangle

public Rectangle

Returns:

this rectangle for chaining
setWidth
(float
The width
width
)

Sets the width of this rectangle

public Rectangle

Returns:

this rectangle for chaining
setX
(float
The x-coordinate
x
)

Sets the x-coordinate of the bottom left corner

public Rectangle

Returns:

this rectangle for chaining
setY
(float
The y-coordinate
y
)

Sets the y-coordinate of the bottom left corner

public String

Returns:

a string representation of this object.
toString
()

Overrides java.lang.Object.toString.

Converts this Rectangle to a string in the format [x,y,width,height].

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

heightback to summary
public float height
serialVersionUIDback to summary
private static final long serialVersionUID
tmpback to summary
public static final Rectangle tmp

Static temporary rectangle. Use with care! Use only when sure other code will not also use this.

tmp2back to summary
public static final Rectangle tmp2

Static temporary rectangle. Use with care! Use only when sure other code will not also use this.

widthback to summary
public float width
xback to summary
public float x
yback to summary
public float y

Constructor Detail

Rectangleback to summary
public Rectangle()

Constructs a new rectangle with all values set to zero

Rectangleback to summary
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:float

The corner point x-coordinate

y:float

The corner point y-coordinate

width:float

The width

height:float

The height

Rectangleback to summary
public Rectangle(Rectangle rect)

Constructs a rectangle based on the given rectangle

Parameters
rect:Rectangle

The rectangle

Method Detail

areaback to summary
public float area()
containsback to summary
public boolean contains(float x, float y)

Implements com.badlogic.gdx.math.Shape2D.contains.

Doc from com.badlogic.gdx.math.Shape2D.contains.

Returns whether a point with the given coordinates is contained within the shape.

Parameters
x:float

point x coordinate

y:float

point y coordinate

Returns:boolean

whether the point is contained in the rectangle

containsback to summary
public boolean contains(Vector2 point)

Implements com.badlogic.gdx.math.Shape2D.contains.

Doc from com.badlogic.gdx.math.Shape2D.contains.

Returns whether the given point is contained within the shape.

Parameters
point:Vector2

The coordinates vector

Returns:boolean

whether the point is contained in the rectangle

containsback to summary
public boolean contains(Circle circle)
Parameters
circle:Circle

the circle

Returns:boolean

whether the circle is contained in the rectangle

containsback to summary
public boolean contains(Rectangle rectangle)
Parameters
rectangle:Rectangle

the other Rectangle.

Returns:boolean

whether the other rectangle is contained in this rectangle.

equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

fitInsideback to summary
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:Rectangle

the other rectangle to fit this rectangle inside

Returns:Rectangle

this rectangle for chaining

See Also
Scaling
fitOutsideback to summary
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:Rectangle

the other rectangle to fit this rectangle around

Returns:Rectangle

this rectangle for chaining

See Also
Scaling
fromStringback to summary
public Rectangle fromString(String v)

Sets this Rectangle to the value represented by the specified string according to the format of toString().

Parameters
v:String

the string.

Returns:Rectangle

this rectangle for chaining

getAspectRatioback to summary
public float getAspectRatio()

Calculates the aspect ratio ( width / height ) of this rectangle

Returns:float

the aspect ratio of this rectangle. Returns Float.NaN if height is 0 to avoid ArithmeticException

getCenterback to summary
public Vector2 getCenter(Vector2 vector)

Calculates the center of the rectangle. Results are located in the given Vector2

Parameters
vector:Vector2

the Vector2 to use

Returns:Vector2

the given vector with results stored inside

getHeightback to summary
public float getHeight()
Returns:float

the height

getPositionback to summary
public Vector2 getPosition(Vector2 position)

return the Vector2 with coordinates of this rectangle

Parameters
position:Vector2

The Vector2

getSizeback to summary
public Vector2 getSize(Vector2 size)
Parameters
size:Vector2

The Vector2

Returns:Vector2

the Vector2 with size of this rectangle

getWidthback to summary
public float getWidth()
Returns:float

the width

getXback to summary
public float getX()
Returns:float

the x-coordinate of the bottom left corner

getYback to summary
public float getY()
Returns:float

the y-coordinate of the bottom left corner

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
mergeback to summary
public Rectangle merge(Rectangle rect)

Merges this rectangle with the other rectangle. The rectangle should not have negative width or negative height.

Parameters
rect:Rectangle

the other rectangle

Returns:Rectangle

this rectangle for chaining

mergeback to summary
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:float

the x coordinate of the point

y:float

the y coordinate of the point

Returns:Rectangle

this rectangle for chaining

mergeback to summary
public Rectangle merge(Vector2 vec)

Merges this rectangle with a point. The rectangle should not have negative width or negative height.

Parameters
vec:Vector2

the vector describing the point

Returns:Rectangle

this rectangle for chaining

mergeback to summary
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:Vector2[]

the vectors describing the points

Returns:Rectangle

this rectangle for chaining

overlapsback to summary
public boolean overlaps(Rectangle r)
Parameters
r:Rectangle

the other Rectangle

Returns:boolean

whether this rectangle overlaps the other rectangle.

perimeterback to summary
public float perimeter()
setback to summary
public Rectangle set(float x, float y, float width, float height)
Parameters
x:float

bottom-left x coordinate

y:float

bottom-left y coordinate

width:float

width

height:float

height

Returns:Rectangle

this rectangle for chaining

setback to summary
public Rectangle set(Rectangle rect)

Sets the values of the given rectangle to this rectangle.

Parameters
rect:Rectangle

the other rectangle

Returns:Rectangle

this rectangle for chaining

setCenterback to summary
public Rectangle setCenter(float x, float y)

Moves this rectangle so that its center point is located at a given position

Parameters
x:float

the position's x

y:float

the position's y

Returns:Rectangle

this for chaining

setCenterback to summary
public Rectangle setCenter(Vector2 position)

Moves this rectangle so that its center point is located at a given position

Parameters
position:Vector2

the position

Returns:Rectangle

this for chaining

setHeightback to summary
public Rectangle setHeight(float height)

Sets the height of this rectangle

Parameters
height:float

The height

Returns:Rectangle

this rectangle for chaining

setPositionback to summary
public Rectangle setPosition(Vector2 position)

Sets the x and y-coordinates of the bottom left corner from vector

Parameters
position:Vector2

The position vector

Returns:Rectangle

this rectangle for chaining

setPositionback to summary
public Rectangle setPosition(float x, float y)

Sets the x and y-coordinates of the bottom left corner

Parameters
x:float

The x-coordinate

y:float

The y-coordinate

Returns:Rectangle

this rectangle for chaining

setSizeback to summary
public Rectangle setSize(float width, float height)

Sets the width and height of this rectangle

Parameters
width:float

The width

height:float

The height

Returns:Rectangle

this rectangle for chaining

setSizeback to summary
public Rectangle setSize(float sizeXY)

Sets the squared size of this rectangle

Parameters
sizeXY:float

The size

Returns:Rectangle

this rectangle for chaining

setWidthback to summary
public Rectangle setWidth(float width)

Sets the width of this rectangle

Parameters
width:float

The width

Returns:Rectangle

this rectangle for chaining

setXback to summary
public Rectangle setX(float x)

Sets the x-coordinate of the bottom left corner

Parameters
x:float

The x-coordinate

Returns:Rectangle

this rectangle for chaining

setYback to summary
public Rectangle setY(float y)

Sets the y-coordinate of the bottom left corner

Parameters
y:float

The y-coordinate

Returns:Rectangle

this rectangle for chaining

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Converts this Rectangle to a string in the format [x,y,width,height].

Returns:String

a string representation of this object.