Tools for working with <x,y>
coordinates.
The tables passed into the Position functions are mutated in-place.
local Position = require('stdlib/area/position')
add (pos1, pos2) | Adds two positions. |
center (pos) | Gets the center position of a tile where the given position resides. |
construct (x, y) | Creates a table representing the position from x and y. |
copy (pos) | Creates a position that is a copy of the given position. |
distance (pos1, pos2) | Calculates the Euclidean distance between two positions. |
distance_squared (pos1, pos2) | Calculates the Euclidean distance squared between two positions, useful when sqrt is not needed. |
equals (pos1, pos2) | Tests whether or not the two given positions are equal. |
expand_to_area (pos, radius) | Expands a position to a square area. |
increment (pos[, inc_x=0][, inc_y=0]) | Increment a position each time it is called. |
increment_closure ([new_inc_x=0][, new_inc_y=0]) | |
manhattan_distance (pos1, pos2) | Calculates the manhatten distance between two positions. |
new (pos_arr[, copy=false]) | Returns a correctly formated position object. |
next_direction (direction[, reverse=false][, eight_way=false]) | Returns the next direction. |
offset (pos, x, y) | Creates a position that is offset by x,y coordinates. |
opposite_direction (direction) | Returns the opposite direction — adapted from Factorio util.lua. |
subtract (pos1, pos2) | Subtracts two positions. |
to_table () | Deprecated |
tostring (pos) | Converts a position to a string. |
translate (pos, direction, distance) | Translates a position in the given direction. |
Metamethods | Position tables are returned with these metamethods attached |
epsilon | Machine Epsilon |
immutable | By default position tables are mutated in place set this to true to make the tables immutable. |
Adds two positions.
Parameters: Returns:
Gets the center position of a tile where the given position resides.
Parameters:
Creates a table representing the position from x and y.
Parameters: Returns:
Creates a position that is a copy of the given position.
Parameters:
Calculates the Euclidean distance between two positions.
Parameters: Returns:
Calculates the Euclidean distance squared between two positions, useful when sqrt is not needed.
Parameters: Returns:
Tests whether or not the two given positions are equal.
Parameters: Returns:
Expands a position to a square area.
Parameters:
Increment a position each time it is called.
This can be used to increment or even decrement a position quickly.
Do not store function closures in the global object; use them in the current tick.
Parameters:local next_pos = Position.increment({0,0})
for i = 1, 5 do next_pos(0,1) -- returns {x = 0, y = 1} {x = 0, y = 2} {x = 0, y = 3} {x = 0, y = 4} {x = 0, y = 5}
local next_pos = Position.increment({0, 0}, 1)
next_pos() -- returns {1, 0}
next_pos(0, 5) -- returns {1, 5}
next_pos(nil, 5) -- returns {2, 10}
local next_pos = Position.increment({0, 0}, 0, 1)
surface.create_entity{name = 'flying-text', text = 'text', position = next_pos()}
surface.create_entity{name = 'flying-text', text = 'text', position = next_pos()} -- creates two flying text entities 1 tile apart
A closure which the increment function returns.
Parameters: Returns:Do not call this directly and do not store this in the global object.
Calculates the manhatten distance between two positions.
Parameters: Returns:
Returns a correctly formated position object.
Parameters: Returns:
Position.new({0, 0}) -- returns {x = 0, y = 0}
Returns the next direction.
Parameters:For entities that only support two directions, see opposite_direction.
Creates a position that is offset by x,y coordinates.
Parameters:
Returns the opposite direction — adapted from Factorio util.lua.
Parameters:
Subtracts two positions.
Parameters: Returns:
Deprecated
See also:
Converts a position to a string.
Parameters:
Translates a position in the given direction.
Parameters:
Position tables are returned with these metamethods attached
Fields: