Package arc.math.geom
Class Rect
java.lang.Object
arc.math.geom.Rect
- All Implemented Interfaces:
Shape2D
- Direct Known Subclasses:
PixmapPacker.PixmapPackerRect
Encapsulates a 2D rectangle defined by its corner point in the bottom left and its extents in x (width) and y (height).
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionfloat
area()
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
boolean
boolean
Returns whether the given point is contained within the shape.boolean
Fits this rectangle into another rectangle while maintaining aspect ratio.fitOutside
(Rect rect) Fits this rectangle around another rectangle while maintaining aspect ratio.fromString
(String v) Sets thisRectangle
to the value represented by the specified string according to the format oftoString()
.float
Calculates the aspect ratio ( width / height ) of this rectangleCalculates the center of the rectangle.float
getPosition
(Vec2 position) return the Vec2 with coordinates of this rectanglefloat
getWidth()
float
getX()
float
getY()
grow
(float amount) grow
(float amountX, float amountY) int
hashCode()
merge
(float x, float y) Merges this rectangle with a point.Merges this rectangle with the other rectangle.Merges this rectangle with a point.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) 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 positionMoves this rectangle so that its center point is located at a given positionsetCentered
(float x, float y, float size) setCentered
(float x, float y, float width, float height) setHeight
(float height) Sets the height of this rectanglesetPosition
(float x, float y) Sets the x and y-coordinates of the bottom left cornersetPosition
(Vec2 position) Sets the x and y-coordinates of the bottom left corner from vectorsetSize
(float sizeXY) Sets the squared size of this rectanglesetSize
(float width, float height) Sets the width and height of this rectanglesetWidth
(float width) Sets the width of this rectanglesetX
(float x) Sets the x-coordinate of the bottom left cornersetY
(float y) Sets the y-coordinate of the bottom left cornertoString()
Converts thisRectangle
to a string in the format[x,y,width,height]
.
-
Field Details
-
tmp
Static temporary rectangle. Use with care! Use only when sure other code will not also use this. -
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-coordinatey
- The corner point y-coordinatewidth
- The widthheight
- The height
-
Rect
Constructs a rectangle based on the given rectangle- Parameters:
rect
- The rectangle
-
-
Method Details
-
setCentered
-
setCentered
-
set
- 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
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
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
Sets the width of this rectangle- Parameters:
width
- The width- Returns:
- this rectangle for chaining
-
getHeight
public float getHeight()- Returns:
- the height
-
setHeight
Sets the height of this rectangle- Parameters:
height
- The height- Returns:
- this rectangle for chaining
-
getPosition
return the Vec2 with coordinates of this rectangle- Parameters:
position
- The Vec2
-
setPosition
Sets the x and y-coordinates of the bottom left corner from vector- Parameters:
position
- The position vector- Returns:
- this rectangle for chaining
-
setPosition
Sets the x and y-coordinates of the bottom left corner- Parameters:
x
- The x-coordinatey
- The y-coordinate- Returns:
- this rectangle for chaining
-
move
-
setSize
Sets the width and height of this rectangle- Parameters:
width
- The widthheight
- The height- Returns:
- this rectangle for chaining
-
setSize
Sets the squared size of this rectangle- Parameters:
sizeXY
- The size- Returns:
- this rectangle for chaining
-
getSize
- 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. -
contains
Description copied from interface:Shape2D
Returns whether the given point is contained within the shape. -
contains
- Parameters:
circle
- the circle- Returns:
- whether the circle is contained in the rectangle
-
contains
- Parameters:
rect
- the otherRect
.- Returns:
- whether the other rectangle is contained in this rectangle.
-
overlaps
- Parameters:
r
- the otherRect
- 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
Sets the values of the given rectangle to this rectangle.- Parameters:
rect
- the other rectangle- Returns:
- this rectangle for chaining
-
grow
-
grow
-
merge
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
"fixes" negative size dimensions. -
merge
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
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
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
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
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
Moves this rectangle so that its center point is located at a given position- Parameters:
position
- the position- Returns:
- this for chaining
-
fitOutside
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
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
Converts thisRectangle
to a string in the format[x,y,width,height]
. -
fromString
Sets thisRectangle
to 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() -
equals
-