Package com.badlogic.gdx.math
Class Rectangle
- java.lang.Object
-
- com.badlogic.gdx.math.Rectangle
-
- All Implemented Interfaces:
Shape2D,java.io.Serializable
- Direct Known Subclasses:
PixmapPacker.PixmapPackerRectangle,Table.DebugRect
public class Rectangle extends java.lang.Object implements java.io.Serializable, Shape2D
Encapsulates a 2D rectangle defined by its corner point in the bottom left and its extents in x (width) and y (height).- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description Rectangle()Constructs a new rectangle with all values set to zeroRectangle(float x, float y, float width, float height)Constructs a new rectangle with the given corner point in the bottom left and dimensions.Rectangle(Rectangle rect)Constructs a rectangle based on the given rectangle
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description floatarea()booleancontains(float x, float y)Returns whether a point with the given coordinates is contained within the shape.booleancontains(Circle circle)booleancontains(Rectangle rectangle)booleancontains(Vector2 point)Returns whether the given point is contained within the shape.booleanequals(java.lang.Object obj)RectanglefitInside(Rectangle rect)Fits this rectangle into another rectangle while maintaining aspect ratio.RectanglefitOutside(Rectangle rect)Fits this rectangle around another rectangle while maintaining aspect ratio.RectanglefromString(java.lang.String v)Sets thisRectangleto the value represented by the specified string according to the format oftoString().floatgetAspectRatio()Calculates the aspect ratio ( width / height ) of this rectangleVector2getCenter(Vector2 vector)Calculates the center of the rectangle.floatgetHeight()Vector2getPosition(Vector2 position)return the Vector2 with coordinates of this rectangleVector2getSize(Vector2 size)floatgetWidth()floatgetX()floatgetY()inthashCode()Rectanglemerge(float x, float y)Merges this rectangle with a point.Rectanglemerge(Rectangle rect)Merges this rectangle with the other rectangle.Rectanglemerge(Vector2 vec)Merges this rectangle with a point.Rectanglemerge(Vector2[] vecs)Merges this rectangle with a list of points.booleanoverlaps(Rectangle r)floatperimeter()Rectangleset(float x, float y, float width, float height)Rectangleset(Rectangle rect)Sets the values of the given rectangle to this rectangle.RectanglesetCenter(float x, float y)Moves this rectangle so that its center point is located at a given positionRectanglesetCenter(Vector2 position)Moves this rectangle so that its center point is located at a given positionRectanglesetHeight(float height)Sets the height of this rectangleRectanglesetPosition(float x, float y)Sets the x and y-coordinates of the bottom left cornerRectanglesetPosition(Vector2 position)Sets the x and y-coordinates of the bottom left corner from vectorRectanglesetSize(float sizeXY)Sets the squared size of this rectangleRectanglesetSize(float width, float height)Sets the width and height of this rectangleRectanglesetWidth(float width)Sets the width of this rectangleRectanglesetX(float x)Sets the x-coordinate of the bottom left cornerRectanglesetY(float y)Sets the y-coordinate of the bottom left cornerjava.lang.StringtoString()Converts thisRectangleto a string in the format[x,y,width,height].
-
-
-
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-coordinatey- The corner point y-coordinatewidth- The widthheight- 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 coordinatey- bottom-left y coordinatewidth- widthheight- 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-coordinatey- 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 widthheight- 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:Shape2DReturns whether a point with the given coordinates is contained within the shape.
-
contains
public boolean contains(Vector2 point)
Description copied from interface:Shape2DReturns whether the given point is contained within the shape.
-
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 otherRectangle.- Returns:
- whether the other rectangle is contained in this rectangle.
-
overlaps
public boolean overlaps(Rectangle r)
- Parameters:
r- the otherRectangle- 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 pointy- 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 xy- 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 thisRectangleto a string in the format[x,y,width,height].- Overrides:
toStringin classjava.lang.Object- Returns:
- a string representation of this object.
-
fromString
public Rectangle fromString(java.lang.String v)
Sets thisRectangleto the value represented by the specified string according to the format oftoString().- Parameters:
v- the string.- Returns:
- this rectangle for chaining
-
area
public float area()
-
perimeter
public float perimeter()
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
-