Tools for working with <x,y> coordinates.
local Position = require('__stdlib__/stdlib/area/position')
| new(pos) | Returns a correctly formated position object. |
| construct(x, y) | Creates a table representing the position from x and y. |
| update(pos, x, y) | Update a position in place without returning a new position. |
| load(pos) | Load the metatable into the passed position without creating a new one. |
| from_string(pos_string) | Converts a position string to a position. |
| from_key(pos_string) | Converts a string key position to a position. |
| from_chunk_position(pos) | Gets the left top tile position of a chunk from the chunk position. |
| from_pixels(pos) | Convert position from pixels |
| Metamethods | Position tables are returned with these metamethods attached. |
| add(pos1, ...) | Addition of two positions. |
| subtract(pos1, ...) | Subtraction of two positions.. |
| multiply(pos1, ...) | Multiplication of two positions. |
| divide(pos1, ...) | Division of two positions. |
| mod(pos1, ...) | Modulo of two positions. |
| closest(pos1, positions) | Return the closest position to the first position. |
| farthest(pos1, positions) | Return the farthest position from the first position. |
| between(pos1, pos2) | The middle of two positions. |
| projection(pos1, pos2) | The projection point of two positions. |
| reflection(pos1, pos2) | The reflection point or two positions. |
| store(pos) | Stores the position for recall later, not deterministic. |
| recall(pos) | Recalls the stored position. |
| normalize(pos) | Normalizes a position by rounding it to 2 decimal places. |
| abs(pos) | Abs x, y values |
| ceil(pos) | Ceil x, y values. |
| floor(pos) | Floor x, y values. |
| center(pos) | The center position of the tile where the given position resides. |
| round(pos) | Rounds a positions points to the closest integer. |
| perpendicular(pos) | Perpendicular position. |
| swap(pos) | Swap the x and y coordinates. |
| flip(pos) | Flip the signs of the position. |
| flip_x(pos) | Flip the x sign. |
| flip_y(pos) | Flip the y sign. |
| lerp(pos1, pos2, alpha) | Lerp position of pos1 and pos2. |
| trim(pos, max_len) | Trim the position to a length. |
| offset_along_line(pos1, pos2, distance_from_pos2) | Returns the position along line between source and target, at the distance from target. |
| translate(pos, direction, distance) | Translates a position in the given direction. |
| random(pos[, minimum=0][, maximum=1][, random_tile=false]) | Return a random offset of a position. |
| average(positions) | Return the average position of the passed positions. |
| min(positions) | Return the minimum position of the passed positions. |
| max(positions) | Return the maximum position of the passed positions. |
| min_xy(positions) | Return a position created from the smallest x, y values in the passed positions. |
| max_xy(positions) | Return a position created from the largest x, y values in the passed positions. |
| intersection(pos1_start, pos1_end, pos2_start, pos2_end) | The intersection of 4 positions. |
| normalized(pos) | Normalizes a position by rounding it to 2 decimal places. |
| absed(pos) | Abs x, y values |
| ceiled(pos) | Ceil x, y values in place. |
| floored(pos) | Floor x, y values. |
| centered(pos) | The center position of the tile where the given position resides. |
| rounded(pos) | Rounds a positions points to the closest integer. |
| swapped(pos) | Swap the x and y coordinates. |
| flipped(pos) | Flip the signs of the position. |
| to_pixels(pos) | Convert to pixels from position |
| to_chunk_position(pos) | Gets the chunk position of a chunk where the specified position resides. |
| expand_to_area(pos, radius) | Expands a position to a square area. |
| to_area(pos, width, height) | Expands a position into an area by setting pos to left_top. |
| to_tile_area(pos) | Converts a tile position to the area of the tile it is in. |
| to_chunk_area(pos) | Get the chunk area the specified position is in. |
| chunk_position_to_chunk_area(pos) | Get the chunk area for the specified chunk position. |
| len_squared(pos) | Gets the squared length of a position |
| len(pos) | Gets the length of a position |
| to_string(pos) | Converts a position to a string. |
| to_string_xy(pos) | Converts a position to an x, y string. |
| to_key(pos) | Converts a position to a string suitable for using as a table index. |
| unpack(pos) | Unpack a position into a tuple. |
| pack(pos) | Packs a position into an array. |
| is_zero(pos) | Is this position {0, 0}. |
| inside(pos, area) | Is a position inside of an area. |
| is_simple_position(pos) | Is this a simple position. |
| is_complex_position(pos) | Is this a complex position. |
| is_Position(pos) | Does the position have the class attached |
| is_position(pos) | Is this any position |
| atan2(pos1, pos2) | Return the atan2 of 2 positions. |
| angle(pos1, pos2) | The angle between two positions |
| cross(pos1, pos2) | Return the cross product of two positions. |
| equals(pos1, pos2) | Tests whether or not the two given positions are equal. |
| less_than(pos1, pos2) | Is pos1 less than pos2. |
| less_than_eq(pos1, pos2) | Is pos1 less than or equal to pos2. |
| distance_squared(pos1[, pos2]) | Calculates the Euclidean distance squared between two positions, useful when sqrt is not needed. |
| distance(pos1[, pos2={x=0]) | Calculates the Euclidean distance between two positions. |
| manhattan_distance(pos1[, pos2]) | Calculates the manhatten distance between two positions. |
| direction_to(pos1, pos2) | Returms the direction to a position using simple delta comparisons. |
| complex_direction_to(pos1, pos2, eight_way) | Returns the direction to a position. |
| increment(pos[, inc_x=0][, inc_y=0][, increment_initial=false]) | Increment a position each time it is called. |
| increment_closure([new_inc_x=0][, new_inc_y=0]) | A closure which the increment function returns. |
Returns a correctly formated position object.
Parameters:
Position.new({0, 0}) -- returns {x = 0, y = 0}
Creates a table representing the position from x and y.
Parameters: Returns:
Update a position in place without returning a new position.
Parameters: Returns:
Load the metatable into the passed position without creating a new one.
Always assumes a valid position is passed
Parameters:Converts a position string to a position.
Parameters:
Converts a string key position to a position.
Parameters:
Gets the left top tile position of a chunk from the chunk position.
Parameters:
Convert position from pixels
Parameters:
Position tables are returned with these metamethods attached.
Methods that return a position will return a NEW position without modifying the passed positions.
Fields:Addition of two positions.
Parameters: Returns:
Subtraction of two positions..
Parameters: Returns:
Multiplication of two positions.
Parameters: Returns:
Division of two positions.
Parameters: Returns:
Modulo of two positions.
Parameters: Returns:
Return the closest position to the first position.
Parameters:
Return the farthest position from the first position.
Parameters:
The middle of two positions.
Parameters: Returns:
The projection point of two positions.
Parameters: Returns:
The reflection point or two positions.
Parameters: Returns:
Stores the position for recall later, not deterministic.
Only the last position stored is saved.
Parameters:Recalls the stored position.
Parameters:
Normalizes a position by rounding it to 2 decimal places.
Parameters:
Abs x, y values
Parameters:
Ceil x, y values.
Parameters:
Floor x, y values.
Parameters:
The center position of the tile where the given position resides.
Parameters:
Rounds a positions points to the closest integer.
Parameters:
Perpendicular position.
Parameters:
Swap the x and y coordinates.
Parameters:
Flip the signs of the position.
Parameters:
Flip the x sign.
Parameters:
Flip the y sign.
Parameters:
Lerp position of pos1 and pos2.
Parameters: Returns:
Trim the position to a length.
Parameters:
Returns the position along line between source and target, at the distance from target.
Parameters:
Translates a position in the given direction.
Parameters:
Return a random offset of a position.
Parameters:
Return the average position of the passed positions.
Parameters:
Return the minimum position of the passed positions.
Parameters:
Return the maximum position of the passed positions.
Parameters:
Return a position created from the smallest x, y values in the passed positions.
Parameters:
Return a position created from the largest x, y values in the passed positions.
Parameters:
The intersection of 4 positions.
Parameters:
Normalizes a position by rounding it to 2 decimal places.
Parameters:
Abs x, y values
Parameters:
Ceil x, y values in place.
Parameters:
Floor x, y values.
Parameters:
The center position of the tile where the given position resides.
Parameters:
Rounds a positions points to the closest integer.
Parameters:
Swap the x and y coordinates.
Parameters:
Flip the signs of the position.
Parameters:
Convert to pixels from position
Parameters:
Gets the chunk position of a chunk where the specified position resides.
Parameters:
local chunk_x = Position.chunk_position(pos).x
Expands a position to a square area.
Parameters:
Expands a position into an area by setting pos to left_top.
Parameters: Returns:
Converts a tile position to the area of the tile it is in.
Parameters:
Get the chunk area the specified position is in.
Parameters:
Get the chunk area for the specified chunk position.
Parameters:
Gets the squared length of a position
Parameters:
Gets the length of a position
Parameters:
Converts a position to a string.
Parameters:
Converts a position to an x, y string.
Parameters:
Converts a position to a string suitable for using as a table index.
Parameters:
Unpack a position into a tuple.
Parameters:
Packs a position into an array.
Parameters:
Is this position {0, 0}.
Parameters:
Is a position inside of an area.
Parameters:
Is this a simple position.
{num, num}
Parameters:Is this a complex position.
{x = number, y = number}
Parameters:Does the position have the class attached
Parameters:
Is this any position
Parameters:
Return the atan2 of 2 positions.
Parameters: Returns:
The angle between two positions
Parameters: Returns:
Return the cross product of two positions.
Parameters: Returns:
Tests whether or not the two given positions are equal.
Parameters: Returns:
Is pos1 less than pos2.
Parameters: Returns:
Is pos1 less than or equal to pos2.
Parameters: Returns:
Calculates the Euclidean distance squared between two positions, useful when sqrt is not needed.
Parameters: Returns:
Calculates the Euclidean distance between two positions.
Parameters: Returns:
Calculates the manhatten distance between two positions.
Parameters: Returns:
Returms the direction to a position using simple delta comparisons.
Parameters: Returns:
Returns the direction to a position.
Parameters: Returns:
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