Tools for working with bounding boxes.
local Area = require('__stdlib__/stdlib/area/area')
| new(area) | Converts an area in either array or table format to an area with a metatable. |
| construct([x1=0][, y1=0][, x2=0][, y2=0]) | Creates an area from number parameters. |
| load(area) | Loads the metatable into the passed Area without creating a new one. |
| from_string(area_string) | Converts an area string to an area. |
| from_key(area_string) | Converts a string key area to an area. |
| store(area) | Stores the area for recall later, not deterministic. |
| recall(area) | Recalls the stored area. |
| normalize(area) | Normalizes the given area. |
| normalized(area) | Normalize an area in place. |
| from_pixels(area) | Convert area from pixels. |
| to_pixels(area) | Convert area to pixels. |
| round(area) | Rounds an areas points to its closest integer. |
| ceil(area) | Ceils an area by increasing the size of the area outwards |
| floor(area) | Floors an area by decreasing the size of the area inwards. |
| center_points(area) | Gets the center positions of the tiles where the given area’s two positions reside. |
| corners(area) | add left_bottom and right_top to the area |
| flip(area) | Flip an area such that the value of its width becomes its height, and the value of its height becomes its width. |
| non_zero(area, amount) | Return a non zero sized area by expanding if needed |
| to_diameter(area, diameter) | Returns the area to the diameter from left_top |
| min(area, area2) | Returns the smallest sized area. |
| max(area, area2) | Returns the largest sized area. |
| shrink(area, amount) | Shrinks the area inwards by the given amount. |
| expand(area, amount) | Expands the area outwards by the given amount. |
| adjust(area, amount) | Adjust an area by shrinking or expanding. |
| offset(area, pos) | Offsets the area by the {x, y} values. |
| translate(area, direction, distance) | Translates an area in the given direction. |
| to_surface_size(area, surface) | Set an area to the whole size of the surface. |
| shrink_to_surface_size(area, surface) | Shrinks an area to the size of the surface if it is bigger. |
| to_chunk_coords(area) | Return the chunk coordinates from an area. |
| center(area) | Calculates the center of the area and returns the position. |
| to_key(area) | Return a suitable string for using as a table key |
| to_string(area) | Converts an area to a string. |
| to_string_xy(area) | Converts an area to an ltx, lty / rbx, rby string. |
| is_zero(area) | Is this a non zero sized area |
| is_normalized(area) | Is the area normalized. |
| valid(area) | Is the area non-zero and normalized. |
| is_simple_area(area) | Is this a simple area. |
| is_complex_area(area) | Is this a complex area {left_top = {x = num, y = num}, right_bottom = {x = num, y = num}} |
| is_area(area) | Is this and area of any kind. |
| is_Area(area) | Does the area have the class attached |
| unpack(area) | Unpack an area into a tuple. |
| unpack_positions(area) | Unpack an area into a tuple of position tables. |
| pack(area) | Pack an area into an array. |
| pack_positions(area) | Pack an area into a simple bounding box array |
| size(area) | Gets the properties of the given area. |
| rectangle(area) | Return the rectangle. |
| width(area) | The width of the area. |
| height(area) | The height of an area. |
| dimensions(area) | The dimensions of an area. |
| perimeter(area) | The Perimiter of an area. |
| equals(area1, area2) | Returns true if two areas are the same. |
| less_than(area1, area2) | Is area1 smaller in size than area2 |
| collides(area1, area2) | Does either area overlap/collide with the other area. |
| contains_positions(area, positions) | Are the passed positions all located in an area. |
| contains_areas(area, areas) | Are all passed areas completly inside an area. |
| collides_areas(area, areas) | Do all passed areas collide with an area. |
| iterate(area[, as_position=false][, inside=false][, step=1]) | Iterates an area. |
| spiral_iterate(area, as_position) | Iterates the given area in a spiral as depicted below, from innermost to the outermost location. |
| Metamethods | Area tables are returned with these Metamethods attached. |
Converts an area in either array or table format to an area with a metatable.
Returns itself if it already has a metatable
Parameters:Creates an area from number parameters.
Parameters:
Loads the metatable into the passed Area without creating a new one.
Parameters:
Converts an area string to an area.
Parameters:
Converts a string key area to an area.
Parameters:
Stores the area for recall later, not deterministic.
Only the last area stored is saved.
Parameters:Recalls the stored area.
Parameters:
Normalizes the given area.
right_bottom.x & left_top.x IF right_bottom.x < left_top.x
right_bottom.y & left_top.y IF right_bottom.y < left_top.y
Normalize an area in place.
Parameters:
Convert area from pixels.
Parameters:
Convert area to pixels.
Parameters:
Rounds an areas points to its closest integer.
Parameters:
Ceils an area by increasing the size of the area outwards
Parameters:
Floors an area by decreasing the size of the area inwards.
Parameters:
Gets the center positions of the tiles where the given area’s two positions reside.
Parameters:
add left_bottom and right_top to the area
Parameters:
Flip an area such that the value of its width becomes its height, and the value of its height becomes its width.
Parameters:
Return a non zero sized area by expanding if needed
Parameters:
Returns the area to the diameter from left_top
Parameters:
Returns the smallest sized area.
Parameters:
Returns the largest sized area.
Parameters:
Shrinks the area inwards by the given amount.
The area shrinks inwards from top-left towards the bottom-right, and from bottom-right towards the top-left.
Parameters:Expands the area outwards by the given amount.
Parameters:
Adjust an area by shrinking or expanding.
Imagine pinching & holding with fingers the top-left & bottom-right corners of a 2D box and pulling outwards to expand and pushing inwards to shrink the box.
Parameters:local area = Area.adjust({{-2, -2}, {2, 2}}, {4, -1})
-- returns {left_top = {x = -6, y = -1}, right_bottom = {x = 6, y = 1}}
Offsets the area by the {x, y} values.
Parameters:
Translates an area in the given direction.
Parameters:
Set an area to the whole size of the surface.
Parameters:
Shrinks an area to the size of the surface if it is bigger.
Parameters:
Return the chunk coordinates from an area.
Parameters:
Calculates the center of the area and returns the position.
Parameters:
Return a suitable string for using as a table key
Parameters:
Converts an area to a string.
Parameters:
Converts an area to an ltx, lty / rbx, rby string.
Parameters:
Is this a non zero sized area
Parameters:
Is the area normalized.
Parameters:
Is the area non-zero and normalized.
Parameters:
Is this a simple area.
{{num, num}, {num, num}}
Parameters:Is this a complex area {left_top = {x = num, y = num}, right_bottom = {x = num, y = num}}
Parameters:
Is this and area of any kind.
Parameters:
Does the area have the class attached
Parameters:
Unpack an area into a tuple.
Parameters:
Unpack an area into a tuple of position tables.
Parameters:
Pack an area into an array.
Parameters:
Pack an area into a simple bounding box array
Parameters:
Gets the properties of the given area.
This function returns a total of four values that represent the properties of the given area.
Parameters:Return the rectangle.
Parameters:
The width of the area.
Parameters:
The height of an area.
Parameters:
The dimensions of an area.
Parameters:
The Perimiter of an area.
Parameters:
Returns true if two areas are the same.
Parameters:
Is area1 smaller in size than area2
Parameters:
Does either area overlap/collide with the other area.
Parameters:
Are the passed positions all located in an area.
Parameters:
Are all passed areas completly inside an area.
Parameters:
Do all passed areas collide with an area.
Parameters:
Iterates an area.
Parameters:
local area = {{0, -5}, {3, -3}}
for x,y in Area.iterate(area) do
-- return x, y values
end
for position in Area.iterate(area, true) do
-- returns a position object
end
-- Iterates from left_top.x to right_bottom.x then goes down y until right_bottom.y
Iterates the given area in a spiral as depicted below, from innermost to the outermost location.

for x, y in Area.spiral_iterate({{-2, -1}, {2, 1}}) do
print('(' .. x .. ', ' .. y .. ')')
end
prints: (0, 0) (1, 0) (1, 1) (0, 1) (-1, 1) (-1, 0) (-1, -1) (0, -1) (1, -1) (2, -1) (2, 0) (2, 1) (-2, 1) (-2, 0) (-2, -1)
Area tables are returned with these Metamethods attached.
Fields: