Rect

@Serializable
data class Rect(var left: Int = 0, var top: Int = 0, var right: Int = 0, var bottom: Int = 0)

Rect holds four integer coordinates for a rectangle. The rectangle is represented by the coordinates of its 4 edges (left, top, right bottom). These fields can be accessed directly. Use width() and height() to retrieve the rectangle's width and height. Note: most methods do not check to see that the coordinates are sorted correctly (i.e. left <= right and top <= bottom).

Create a new rectangle with the specified coordinates. Note: no range checking is performed, so the caller must ensure that left <= right and top <= bottom.

Parameters

left

The X coordinate of the left side of the rectangle

top

The Y coordinate of the top of the rectangle

right

The X coordinate of the right side of the rectangle

bottom

The Y coordinate of the bottom of the rectangle

Constructors

Link copied to clipboard
constructor(r: Rect?)

Create a new rectangle, initialized with the values in the specified rectangle (which is left unmodified).

constructor(left: Int = 0, top: Int = 0, right: Int = 0, bottom: Int = 0)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
var bottom: Int
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
var left: Int
Link copied to clipboard
var right: Int
Link copied to clipboard
var top: Int

Functions

Link copied to clipboard
fun centerX(): Int
Link copied to clipboard
fun centerY(): Int
Link copied to clipboard
operator fun contains(r: Rect): Boolean

Returns true iff the specified rectangle r is inside or equal to this rectangle. An empty rectangle never contains another rectangle.

fun contains(x: Int, y: Int): Boolean

Returns true if (x,y) is inside the rectangle. The left and top are considered to be inside, while the right and bottom are not. This means that for a x,y to be contained: left <= x < right and top <= y < bottom. An empty rectangle never contains any point.

fun contains(left: Int, top: Int, right: Int, bottom: Int): Boolean

Returns true iff the 4 specified sides of a rectangle are inside or equal to this rectangle. i.e. is this rectangle a superset of the specified rectangle. An empty rectangle never contains another rectangle.

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
fun height(): Int
Link copied to clipboard
fun inset(insets: Rect)

Insets the rectangle on all sides specified by the dimensions of the insets rectangle.

fun inset(dx: Int, dy: Int)

Inset the rectangle by (dx,dy). If dx is positive, then the sides are moved inwards, making the rectangle narrower. If dx is negative, then the sides are moved outwards, making the rectangle wider. The same holds true for dy and the top and bottom.

fun inset(left: Int, top: Int, right: Int, bottom: Int)

Insets the rectangle on all sides specified by the insets.

Link copied to clipboard

If the specified rectangle intersects this rectangle, return true and set this rectangle to that intersection, otherwise return false and do not change this rectangle. No check is performed to see if either rectangle is empty. To just test for intersection, use intersects()

fun intersect(left: Int, top: Int, right: Int, bottom: Int): Boolean

If the rectangle specified by left,top,right,bottom intersects this rectangle, return true and set this rectangle to that intersection, otherwise return false and do not change this rectangle. No check is performed to see if either rectangle is empty. Note: To just test for intersection, use .intersects.

Link copied to clipboard
fun intersects(left: Int, top: Int, right: Int, bottom: Int): Boolean

Returns true if this rectangle intersects the specified rectangle. In no event is this rectangle modified. No check is performed to see if either rectangle is empty. To record the intersection, use intersect() or setIntersect().

Link copied to clipboard

If the specified rectangle intersects this rectangle, set this rectangle to that intersection, otherwise set this rectangle to the empty rectangle.

Link copied to clipboard
fun offset(dx: Int, dy: Int)

Offset the rectangle by adding dx to its left and right coordinates, and adding dy to its top and bottom coordinates.

Link copied to clipboard
fun offsetTo(newLeft: Int, newTop: Int)

Offset the rectangle to a specific (left, top) position, keeping its width and height the same.

Link copied to clipboard
fun scale(scale: Float)

Scales up the rect by the given scale.

Link copied to clipboard
operator fun set(left: Int, top: Int, right: Int, bottom: Int)

Set the rectangle's coordinates to the specified values. Note: no range checking is performed, so it is up to the caller to ensure that left <= right and top <= bottom.

Link copied to clipboard
fun setEmpty()

Set the rectangle to (0,0,0,0)

Link copied to clipboard

If rectangles a and b intersect, return true and set this rectangle to that intersection, otherwise return false and do not change this rectangle. No check is performed to see if either rectangle is empty. To just test for intersection, use intersects()

Link copied to clipboard
fun sort()

Swap top/bottom or left/right if there are flipped (i.e. left right and/or top bottom). This can be called if the edges are computed separately, and may have crossed over each other. If the edges are already correct (i.e. left <= right and top <= bottom) then nothing is done.

Link copied to clipboard
Link copied to clipboard
fun union(r: Rect)
fun union(left: Int, top: Int, right: Int, bottom: Int)

Update this Rect to enclose itself and the specified rectangle. If the specified rectangle is empty, nothing is done. If this rectangle is empty it is set to the specified rectangle.

fun union(x: Int, y: Int)

Update this Rect to enclose itself and the x,y coordinate. There is no check to see that this rectangle is non-empty.

Link copied to clipboard
fun width(): Int