com.badlogic.gdx.math
Class Rectangle

java.lang.Object
  extended by com.badlogic.gdx.math.Rectangle
All Implemented Interfaces:
Serializable

public class Rectangle
extends Object
implements Serializable

Encapsulates a 2D rectangle defined by it's bottom corner point and its extends in x (width) and y (height).

Author:
[email protected]
See Also:
Serialized Form

Field Summary
 float height
           
static Rectangle tmp
          Static temporary rectangle.
static Rectangle tmp2
          Static temporary rectangle.
 float width
           
 float x
           
 float y
           
 
Constructor Summary
Rectangle()
          Constructs a new rectangle with all values set to zero
Rectangle(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
 boolean contains(float x, float y)
           
 boolean contains(Rectangle rectangle)
           
 boolean contains(Vector2 vector)
           
 Rectangle fitInside(Rectangle rect)
          Fits this rectangle into another rectangle while maintaining aspect ratio.
 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.
 float getAspectRatio()
          Calculates the aspect ratio ( width / height ) of this rectangle
 Vector2 getCenter(Vector2 vector)
          Calculates the center of the rectangle.
 float getHeight()
           
 Vector2 getPosition(Vector2 position)
          return the Vector2 with coordinates of this rectangle
 Vector2 getSize(Vector2 size)
           
 float getWidth()
           
 float getX()
           
 float getY()
           
 Rectangle merge(Rectangle rect)
          Merges this rectangle with the other rectangle.
 boolean overlaps(Rectangle r)
           
 Rectangle set(float x, float y, float width, float height)
           
 Rectangle set(Rectangle rect)
          Sets the values of the given rectangle to this rectangle.
 Rectangle setCenter(float x, float y)
          Moves this rectangle so that its center point is located at a given position
 Rectangle setCenter(Vector2 position)
          Moves this rectangle so that its center point is located at a given position
 Rectangle setHeight(float height)
          Sets the height of this rectangle
 Rectangle setPosition(float x, float y)
          Sets the x and y-coordinates of the bottom left corner
 Rectangle setPosition(Vector2 position)
          Sets the x and y-coordinates of the bottom left corner from vector
 Rectangle setSize(float sizeXY)
          Sets the squared size of this rectangle
 Rectangle setSize(float width, float height)
          Sets the width and height of this rectangle
 Rectangle setWidth(float width)
          Sets the width of this rectangle
 Rectangle setX(float x)
          Sets the x-coordinate of the bottom left corner
 Rectangle setY(float y)
          Sets the y-coordinate of the bottom left corner
 String toString()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

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)
Parameters:
x - point x coordinate
y - point y coordinate
Returns:
whether the point is contained in the rectangle

contains

public boolean contains(Vector2 vector)
Parameters:
vector - The coordinates vector
Returns:
whether the vector 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.

Parameters:
rect - the other rectangle
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

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

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2013. All Rights Reserved.