⚙️rect
__call
Constructor
Creates a new rectangle.
Arguments
1. Default rectangle (0, 0 position; 0, 0 size).
None.
2. Single value.
value
float
Mins XY, maxs XY value.
3. Single vector.
4. Double value.
x
float
Mins/maxs X coordinate.
y
float
Mins/maxs Y coordinate.
5. Double vector.
6. Four values.
x0
float
Mins X coordinate.
y0
float
Mins Y coordinate.
x1
float
Maxs X coordinate.
y1
float
Maxs Y coordinate.
Returns
rect
New rectangle.
Example
local my_rect = draw.rect(50, 50, 150, 150);
mins
Field
Type: vec2
Mins (top-left) vector.
maxs
Field
Type: vec2
Maxs (bottom-right) vector.
width
Method
Either returns rectangle's width, or sets a new width.
Arguments
1. Get width.
None.
2. Set width.
value
float
New width.
Returns
1. Get width.
float
Width.
2. Set width.
rect
New rectangle with changed width.
Example
local half_width = rect:width(rect:width() * 0.5);
height
Method
Either returns rectangle's height, or sets a new height.
Arguments
1. Get height.
None.
2. Set height.
value
float
New height.
Returns
1. Get height.
float
Height.
2. Set height.
rect
New rectangle with changed height.
Example
local half_height = rect:height(rect:height() * 0.5);
size
Method
Either returns rectangle's size, or sets a new size.
Arguments
1. Get size.
None.
2. Set size.
Returns
1. Get size.
Size.
2. Set size.
rect
New rectangle with changed size.
Example
local half_size = rect:size(rect:size() * draw.vec2(0.5, 0.5));
explode
Method
Explodes the rectangle by given vector (increase size from center into all directions by coordinates in the vector).
Arguments
Returns
rect
Exploded rectangle.
Example
local exp = rect:explode(draw.vec2(6, 6)); -- will subtract -3,-3 from mins and add 3,3 to maxs
half_width
Method
Returns a rectangle with half of the width of this rectangle.
Arguments
None.
Returns
rect
Rectangle with halved width.
Example
local half = rect:half_width();
translate
Method
Translates (moves) this rectangle by vector coordinates.
Arguments
Returns
rect
Translated rectangle.
Example
local rect = draw.rect(50, 50, 150, 150);
local translated = rect:translate(draw.vec2(5, 5)); -- mins/maxs will be 55,55 and 155,155
margin_left
Method
Move rectangle from the left by given amount.
Arguments
value
float
Margin amount.
Returns
rect
Moved rectangle.
Example
local new = rect:margin_left(5); -- moves to the right by 5px
margin_right
Method
Move rectangle from the right by given amount.
Arguments
value
float
Margin amount.
Returns
rect
Moved rectangle.
Example
local new = rect:margin_right(5); -- moves to the left by 5px
margin_top
Method
Move rectangle from the top by given amount.
Arguments
value
float
Margin amount.
Returns
rect
Moved rectangle.
Example
local new = rect:margin_top(5); -- moves to the bottom by 5px
margin_bottom
Method
Move rectangle from the bottom by given amount.
Arguments
value
float
Margin amount.
Returns
rect
Moved rectangle.
Example
local new = rect:margin_bottom(5); -- moves to the top by 5px
padding_left
Method
Adds the value to the left side of the rectangle.
Arguments
value
float
Padding amount.
Returns
rect
Resized rectangle.
Example
local new = rect:padding_left(5); -- adds 5 to mins x
padding_right
Method
Adds the value to the right side of the rectangle.
Arguments
value
float
Padding amount.
Returns
rect
Resized rectangle.
Example
local new = rect:padding_right(5); -- adds 5 to maxs x
padding_top
Method
Adds the value to the top side of the rectangle.
Arguments
value
float
Padding amount.
Returns
rect
Resized rectangle.
Example
local new = rect:padding_top(5); -- adds 5 to mins y
padding_bottom
Method
Adds the value to the bottom side of the rectangle.
Arguments
value
float
Padding amount.
Returns
rect
Resized rectangle.
Example
local new = rect:padding_bottom(5); -- adds 5 to maxs y
shrink
Method
Shrinks (implodes) the rectangle by given amount.
Arguments
value
float
Shrink value.
Returns
rect
Resized rectangle.
Example
local shrinked = rect:shrink(5); -- adds 5,5 to mins and subtracts 5,5 from maxs
expand
Method
Expands (explodes) the rectangle by given amount.
Arguments
value
float
Expand value.
Returns
rect
Resized rectangle.
Example
local expanded = rect:expand(5); -- subtracts 5,5 from mins and adds 5,5 to maxs
contains
Method
Returns true
if this rectangle contains either vector or another rectangle.
Arguments
1. Vector variant.
2. Rectangle variant.
other
rect
Rectangle to check against.
Returns
bool
true
if other object is in bounds of this rectangle.
Example
if rect:contains(cursor_pos) then
-- ...
end
overlaps
Method
Returns true
if the other rectangle overlaps with this rectangle.
Arguments
other
rect
Rectangle to check against.
Returns
bool
true
if other rectangle overlaps with this rectangle.
Example
if rect:overlaps(another_rect) then
-- ...
end
intersect
Method
Intersects this rectangle with another rectangle.
Arguments
other
rect
Rectangle to intersect with.
Returns
rect
Intersected rectangle.
Example
local intersected = rect:intersect(another_rect);
tl
Method
Returns top-left vector.
Arguments
None.
Returns
Top-left vector.
Example
local tl = rect:tl();
tr
Method
Returns top-right vector.
Arguments
None.
Returns
Top-right vector.
Example
local tr = rect:tr();
br
Method
Returns bottom-right vector.
Arguments
None.
Returns
Bottom-right vector.
Example
local br = rect:br();
bl
Method
Returns bottom-left vector.
Arguments
None.
Returns
Bottom-left vector.
Example
local bl = rect:bl();
center
Method
Returns center point of this rectangle.
Arguments
None.
Returns
Center point.
Example
local center = rect:center();
circle
Method
Treats this rectangle as an ellipsis and returns point on it. Note, that this "ellipsis" will be perfect with no modified curvature (basically if this rectangle is a box - you will get a point on a perfect circle).
Arguments
r
float
Radians value.
Returns
Point on the ellipsis.
Example
local point = rect:circle(rad(250)); -- returns point on the 250th degree
floor
Method
Returns floored rectangle.
Arguments
None.
Returns
rect
Floored rectangle.
Example
local floored = rect:floor();
ceil
Method
Returns ceiled rectangle.
Arguments
None.
Returns
rect
Ceiled rectangle.
Example
local ceiled = rect:ceil();
round
Method
Returns rounded rectangle.
Arguments
None.
Returns
rect
Rounded rectangle.
Example
local rounded = rect:round();
is_zero
Method
Returns true
if both mins and maxs are equal to 0.
Arguments
None.
Returns
bool
true
if this is a zero rectangle.
Example
if rect:is_zero() then
-- ...
end
Last updated