Functions for manipulating areas.
NOTE: All functions assume that BoundingBox
and Position
objects contain named keys - x
and y
for
Position
, and left_top
and right_bottom
for BoundingBox
. Attempting to use these functions with the
shorthand forms of these objects will result in a crash.
local area = require("__flib__.area")
ceil(self) | Expand an area to its outer tile edges. |
center(self) | Calculate the centerpoint of the area. |
center_on(self, center_point) | Re-center the area on the given position. |
contains_area(self, area) | Check if the given area is within the area. |
contains_position(self, position) | Check if the given position is within the area. |
corners(self) | Add left-bottom and right-top corners to the area. |
distance_to_nearest_edge(self, position) | Find the distance between a point and the nearest edge of the given area. |
expand(self, delta) | Expand the given area by the given amount. |
expand_to_contain_area(self, area) | Expand the given area to contain the other area. |
expand_to_contain_position(self, position) | Expand the given area to contain the given position. |
floor(self) | Shrink an area to its inner tile edges. |
from_position(position[, snap]) | Create a 1x1 tile area from the given position. |
from_shorthand(area) | Create a proper area from a shorthanded area. |
height(self) | Calculate the height of the area. |
iterate(self[, step=1]) | Create an iterator of positions in the area from the left-top to the right-bottom. |
load(area) | Create an area object from a plain area. |
move(self, delta) | Move the given area by the given delta. |
rotate(self) | Rotate an area 90 degrees around its center. |
strip(self) | Create a new area table from the given area, removing any extra fields and metatables. |
to_shorthand(self) | Remove keys from the area to create a shorthanded area. |
width(self) | Calculate the width of an area. |
Expand an area to its outer tile edges.
Parameters:
Calculate the centerpoint of the area.
Parameters:
Re-center the area on the given position.
Parameters:
Check if the given area is within the area.
Parameters:
area
is contained withing self
.
Check if the given position is within the area.
Parameters:
Add left-bottom and right-top corners to the area.
Parameters:
left_bottom
and right_top
subtables.
Find the distance between a point and the nearest edge of the given area.
Parameters:
Expand the given area by the given amount.
Parameters:
Expand the given area to contain the other area.
Parameters:
Expand the given area to contain the given position.
Parameters:
Shrink an area to its inner tile edges.
Parameters:
Create a 1x1 tile area from the given position.
Parameters:
Create a proper area from a shorthanded area.
Parameters:
Calculate the height of the area.
Parameters:
Create an iterator of positions in the area from the left-top to the right-bottom.
The iterator function, when called, will return a Position
that is within the area.
-- standard area
for position in area.iterate(my_area) do
log(serpent.line(position))
end
-- area object
for position in MyArea:iterate() do
log(serpent.line(position))
end
Create an area object from a plain area.
Doing this allows one to use area methods directly on an area "object" via the :
operator. The area will be passed
in as self
to each function automatically.
Metatables do not persist across save/load, so when using area objects, this function must be called on them whenever
they are retrieved from global
or during on_load
.
This function will also call area.from_shorthand if needed, to ensure that the returned area is properly defined.
Parameters:-- create the area object
local MyArea = area.load(event_data.area)
-- use module methods directly on the object
local center_position = MyArea:center()
for position in MyArea:iterate(0.5) do
log(serpent.line(position))
end
Move the given area by the given delta.
Parameters:
Rotate an area 90 degrees around its center.
Parameters:
Create a new area table from the given area, removing any extra fields and metatables.
Parameters:
Remove keys from the area to create a shorthanded area.
Parameters:
Calculate the width of an area.
Parameters: