Rect¶
Overview¶
Rects are used to represent an X and Y position with accompanying width and height dimensions.
Info
The X and Y positions are the Left and Top of the rectangle respectively.
Examples¶
-- A rectangle with the bottom left corner at 20,15 and
-- with a width of 100 and a height of 45
local rect = Rect.new(20, 15, 100, 45)
-- Another rectangle
local otherRect = Rect.new(50, 40, 20, 30)
-- Test intersections
local intersects = rect:Intersects(otherRect)
local containsPosition = rect:Contains(40, 30)
-- Modify
rect:Shrink(5)
rect:Grow(10, 15)
Constructors¶
function Rect.new() -> Rect
function Rect.new(x : Float, y : Float, width : Float, height : Float) -> Rect
Creates and returns a rectangle with the provided values, or with all elements set to 0 if no arguments were provided.
| Parameter | Type | Description |
|---|---|---|
| x | Float | Value to set the X element of the newly created rectangle to. |
| y | Float | Value to set the Y element of the newly created rectangle to. |
| width | Float | Value to set the width element of the newly created rectangle to. |
| height | Float | Value to set the height element of the newly created rectangle to. |
| Returns | Description |
|---|---|
Rect |
The newly created rectangle |
Methods¶
Translate¶
Moves the rectangle's X and Y elements by the given input values.
| Parameter | Type | Description |
|---|---|---|
| offsetX | Float | The offset applied to the X element. |
| offsetY | Float | The offset applied to the Y element. |
Grow¶
Increases each edge of the rectangle by the given horizontal and vertical values.
Info
The calculation for this method is:
| Parameter | Type | Description |
|---|---|---|
| horizontal | Float | The expansion applied to the left and right edges of the rectangle. |
| vertical | Float | The expansion applied to the top and bottom edges of the rectangle. |
Shrink¶
Decreases each edge of the rectangle by the given horizontal and vertical values.
Info
The calculation for this method is:
| Parameter | Type | Description |
|---|---|---|
| horizontal | Float | The reduction applied to the left and right edges of the rectangle. |
| vertical | Float | The reduction applied to the top and bottom edges of the rectangle. |
Scale¶
Scales the rectangle about 0,0 by the given amount.
Info
The calculation for this method is:
| Parameter | Type | Description |
|---|---|---|
| scale | Float | The scaling value to apply to all elements in the rectangle. |
Contains(x, y)¶
Returns whether the given X and Y position is within this rectangle
| Parameter | Type | Description |
|---|---|---|
| x | Float | The X position to test. |
| y | Float | The Y position to test. |
| Returns | Description |
|---|---|
| Boolean | Whether the given position is within this rectangle. |
Example
Contains(rect)¶
Returns whether the given other rectangle is entirely contained within this rectangle.
Info
This test fails if any element of the input rectangle lies outside the bounds of this rectangle.
| Parameter | Type | Description |
|---|---|---|
| rect | Rect | The rectangle to test. |
| Returns | Description |
|---|---|
| Boolean | Whether the given rectangle is within this rectangle. |
Example
Intersects¶
Returns whether the given other rectangle intersects with this rectangle.
| Parameter | Type | Description |
|---|---|---|
| rect | Rect | The rectangle to test. |
| Returns | Description |
|---|---|
| Boolean | Whether the given rectangle intersects with this rectangle. |
Example
GetIntersection¶
Returns a rectangle which covers where the given rectangle and this rectangle intersect.
If there is no intersection, then nil is returned.
| Parameter | Type | Description |
|---|---|---|
| rect | Rect | The rectangle to intersect with. |
| Returns | Description |
|---|---|
| Rect? | The intersection rectangle.Returns nil if there is no intersection. |
Example
GetUnion(rect)¶
Returns a rectangle which encompasses both the given rectangle and this rectangle.
| Parameter | Type | Description |
|---|---|---|
| rect | Rect | The rectangle to union with. |
| Returns | Description |
|---|---|
| Rect | The encompassing rectangle. |
Example
GetUnion(x, y)¶
Returns a rectangle which encompasses both the given X and Y position and this rectangle.
| Parameter | Type | Description |
|---|---|---|
| x | Float | The X position to union with. |
| y | Float | The Y position to union with. |
| Returns | Description |
|---|---|
| Rect | The encompassing rectangle. |
Example
Properties¶
| Name | Type | Writeable | Description |
|---|---|---|---|
| x | Float | The X position of the rectangle. | |
| y | Float | The Y position of the rectangle. | |
| width | Float | The width of the rectangle. | |
| height | Float | The height of the rectangle. | |
| left | Float | The left edge of the rectangle.left = x |
|
| top | Float | The upper edge of the rectangle.top = y |
|
| right | Float | The right edge of the rectangle.right = x + width |
|
| bottom | Float | The lower edge of the rectangle.bottom = y + height |
|
| centreX | Float | The X position of the centre point of the rectangle.centreX = x + (width / 2) |
|
| centreY | Float | The Y position of the centre point of the rectangle.centreY = y + (height / 2) |