Kryzeth Standard Library

area.position

Methods87

function Position.__call(_: any, ...: any) -> nil
function Position.new(pos: MapPosition) -> StdLib.Area.Position

Returns a correctly formated position object.

function Position.construct(...: number) -> StdLib.Area.Position

Creates a table representing the position from x and y.

Parameters
...number

x and y position

function Position.construct_xy(x: number, y: number) -> StdLib.Area.Position
function Position.update(pos: MapPosition.struct, x: number, y: number) -> pos MapPosition.struct

Update a position in place without returning a new position.

function Position.load(pos: MapPosition.struct) -> StdLib.Area.Position

Load the metatable into the passed position without creating a new one. Always assumes a valid {x, y} position is passed.

function Position.from_string(pos_string: string) -> StdLib.Area.Position

Converts a position string to a position.

function Position.from_key(pos_string: string) -> StdLib.Area.Position

Converts a string key position to a position.

function Position.from_chunk_position(pos: ChunkPosition) -> StdLib.Area.Position

Gets the left-top tile position of a chunk from its chunk position.

function Position.from_pixels(pos: MapPosition.struct) -> StdLib.Area.Position

Converts a position from pixels to tiles.

function Position.add(pos1: MapPosition, ...: number | MapPosition | Vector) -> StdLib.Area.Position

Addition of two positions.

function Position.subtract(pos1: MapPosition, ...: number | MapPosition | Vector) -> StdLib.Area.Position

Subtraction of two positions..

function Position.multiply(pos1: MapPosition, ...: number | MapPosition | Vector) -> StdLib.Area.Position

Multiplication of two positions.

function Position.divide(pos1: MapPosition, ...: number | MapPosition | Vector) -> StdLib.Area.Position

Division of two positions.

function Position.mod(pos1: MapPosition, ...: number | MapPosition | Vector) -> StdLib.Area.Position

Modulo of two positions.

function Position.closest(pos1: MapPosition.struct, positions: MapPosition.struct[]) -> StdLib.Area.Position

Return the closest position to the first position.

function Position.farthest(pos1: MapPosition.struct, positions: MapPosition.struct[]) -> StdLib.Area.Position

Return the farthest position from the first position.

function Position.between(pos1: MapPosition.struct, pos2: MapPosition.struct) -> StdLib.Area.Position

The middle of two positions.

function Position.projection(pos1: MapPosition.struct, pos2: MapPosition.struct) -> StdLib.Area.Position

The projection point of two positions.

function Position.reflection(pos1: MapPosition.struct, pos2: MapPosition.struct) -> StdLib.Area.Position

The reflection point or two positions.

function Position.store(pos: StdLib.Area.Position) -> StdLib.Area.Position

Stores the position for recall later, not deterministic.

function Position.recall(pos: StdLib.Area.Position) -> StdLib.Area.Position

Recalls the stored position.

function Position.normalize(pos: MapPosition.struct) -> StdLib.Area.Position

Normalizes a position by rounding it to 2 decimal places.

function Position.abs(pos: MapPosition.struct) -> StdLib.Area.Position

Abs x, y values

function Position.ceil(pos: MapPosition.struct) -> StdLib.Area.Position

Ceil x, y values.

function Position.floor(pos: MapPosition.struct) -> StdLib.Area.Position

Floor x, y values.

function Position.center(pos: MapPosition.struct) -> StdLib.Area.Position

The center position of the tile where the given position resides.

function Position.round(pos: MapPosition.struct) -> StdLib.Area.Position

Rounds a positions points to the closest integer.

function Position.perpendicular(pos: MapPosition.struct) -> StdLib.Area.Position

Perpendicular position.

function Position.swap(pos: MapPosition.struct) -> StdLib.Area.Position

Swap the x and y coordinates.

function Position.flip(pos: MapPosition.struct) -> StdLib.Area.Position

Flip the signs of the position.

function Position.unary(pos: MapPosition.struct) -> StdLib.Area.Position
function Position.flip_x(pos: MapPosition.struct) -> StdLib.Area.Position

Flip the x sign.

function Position.flip_y(pos: MapPosition.struct) -> StdLib.Area.Position

Flip the y sign.

function Position.lerp(pos1: MapPosition.struct, pos2: MapPosition.struct, alpha: number) -> StdLib.Area.Position

Lerp position of pos1 and pos2.

Parameters
pos1MapPosition.struct
pos2MapPosition.struct
alphanumber

Value from 0 to 1

function Position.trim(pos: MapPosition.struct, max_len: number) -> StdLib.Area.Position

Trim the position to a length.

function Position.offset_along_line(pos1: MapPosition.struct, pos2: MapPosition.struct, distance_from_pos2: nil | number) -> StdLib.Area.Position

Returns the position along line between source and target, at the distance from target.

function Position.translate(pos: MapPosition.struct, direction: defines.direction | nil, distance: nil | number) -> StdLib.Area.Position

Translates a position in the given direction.

function Position.random(pos: MapPosition.struct, minimum: nil | number, maximum: nil | number, random_tile: nil | boolean) -> StdLib.Area.Position

Return a random offset of a position.

function Position.average(...: MapPosition.struct) -> StdLib.Area.Position

Return the average position of the passed positions.

Overloads
function Position.average(...: MapPosition.struct) -> StdLib.Area.Position
function Position.average(array: MapPosition.struct[]) -> StdLib.Area.Position
function Position.min(...: MapPosition.struct) -> StdLib.Area.Position

Return the minimum position of the passed positions.

Overloads
function Position.min(...: MapPosition.struct) -> StdLib.Area.Position
function Position.min(array: MapPosition.struct[]) -> StdLib.Area.Position
function Position.max(...: MapPosition.struct) -> StdLib.Area.Position

Return the maximum position of the passed positions.

Overloads
function Position.max(...: MapPosition.struct) -> StdLib.Area.Position
function Position.max(array: MapPosition.struct[]) -> StdLib.Area.Position
function Position.min_xy(...: MapPosition.struct) -> StdLib.Area.Position

Return a position created from the smallest x, y values in the passed positions.

Overloads
function Position.min_xy(...: MapPosition.struct) -> StdLib.Area.Position
function Position.min_xy(array: MapPosition.struct[]) -> StdLib.Area.Position
function Position.max_xy(...: MapPosition.struct) -> StdLib.Area.Position

Return a position created from the largest x, y values in the passed positions.

Overloads
function Position.max_xy(...: MapPosition.struct) -> StdLib.Area.Position
function Position.max_xy(array: MapPosition.struct[]) -> StdLib.Area.Position
function Position.intersection(pos1_start: MapPosition.struct, pos1_end: MapPosition.struct, pos2_start: MapPosition.struct, pos2_end: MapPosition.struct) -> StdLib.Area.Position | MapPosition.struct

The intersection of two lines defined by four positions.

function Position.normalized(pos: MapPosition.struct) -> MapPosition.struct

Normalizes a position by rounding it to 2 decimal places.

function Position.absed(pos: MapPosition.struct) -> MapPosition.struct

Abs x, y values

function Position.ceiled(pos: MapPosition.struct) -> MapPosition.struct

Ceil x, y values in place.

function Position.floored(pos: MapPosition.struct) -> MapPosition.struct

Floor x, y values.

function Position.centered(pos: MapPosition.struct) -> MapPosition.struct

The center position of the tile where the given position resides.

function Position.rounded(pos: MapPosition.struct) -> MapPosition.struct

Rounds a positions points to the closest integer.

function Position.swapped(pos: MapPosition.struct) -> MapPosition.struct

Swap the x and y coordinates.

function Position.flipped(pos: MapPosition.struct) -> MapPosition.struct

Flip the signs of the position.

function Position.to_pixels(pos: MapPosition.struct) -> StdLib.Area.Position

Converts a tile position to pixels.

function Position.to_chunk_position(pos: MapPosition.struct) -> position StdLib.Area.Position

Gets the chunk coordinates containing the specified position.

Parameters
posMapPosition.struct
Returns
positionStdLib.Area.Position

Chunk coordinates

function Position.expand_to_area(pos: MapPosition.struct, radius: nil | number) -> BoundingBox

Expands a position to a square area.

function Position.to_area(pos: MapPosition.struct, width: nil | number, height: nil | number) -> BoundingBox

Expands a position into an area by setting pos to left_top.

function Position.to_tile_area(pos: TilePosition) -> BoundingBox

Converts a tile position to the @{BoundingBox|area} of the tile it is in.

function Position.to_chunk_area(pos: MapPosition.struct) -> BoundingBox

Get the chunk area the specified position is in.

function Position.chunk_position_to_chunk_area(pos: ChunkPosition) -> BoundingBox

Get the chunk area for the specified chunk position.

function Position.len_squared(pos: MapPosition.struct) -> number

Gets the squared length of a position

function Position.len(pos: MapPosition.struct) -> number

Gets the length of a position

function Position.to_string(pos: MapPosition.struct) -> string

Converts a position to a string.

function Position.to_string_xy(pos: MapPosition.struct) -> string

Converts a position to an x, y string.

function Position.to_key(pos: MapPosition.struct) -> string

Converts a position to a string suitable for using as a table index.

function Position.unpack(pos: MapPosition.struct) -> (x number, y number)

Unpack a position into a tuple.

function Position.pack(pos: MapPosition.struct) -> number[]

Packs a position into an array.

function Position.is_zero(pos: MapPosition.struct) -> boolean

Is this position {0, 0}.

function Position.inside(pos: MapPosition.struct, area: BoundingBox) -> boolean

Is a position inside of an area.

function Position.is_simple_position(pos: any) -> boolean

Is this a simple position. {num, num}

function Position.is_complex_position(pos: any) -> boolean

Is this a complex position. {x = number, y = number}

function Position.is_Position(pos: any) -> boolean

Does the position have the class attached

function Position.is_position(pos: any) -> boolean

Is this any position

function Position.atan2(pos1: MapPosition.struct, pos2: MapPosition.struct) -> number

Return the atan2 of 2 positions.

function Position.angle(pos1: MapPosition.struct, pos2: MapPosition.struct) -> number

The angle between two positions

function Position.cross(pos1: MapPosition.struct, pos2: MapPosition.struct) -> number

Return the cross product of two positions.

function Position.dot(pos1: MapPosition.struct, pos2: MapPosition.struct) -> number

Return the dot product of two positions.

function Position.equals(pos1: MapPosition.struct, pos2: MapPosition.struct) -> boolean

Tests whether or not the two given positions are equal.

function Position.less_than(pos1: MapPosition.struct, pos2: MapPosition.struct) -> boolean

Is pos1 less than pos2.

function Position.less_than_eq(pos1: MapPosition.struct, pos2: MapPosition.struct) -> boolean

Is pos1 less than or equal to pos2.

function Position.distance_squared(pos1: MapPosition.struct, pos2: MapPosition.struct) -> number

Calculates the Euclidean distance squared between two positions, useful when sqrt is not needed.

function Position.distance(pos1: MapPosition.struct, pos2: MapPosition.struct) -> number

Calculates the Euclidean distance between two positions.

function Position.manhattan_distance(pos1: MapPosition.struct, pos2: MapPosition.struct) -> number

Calculates the manhatten distance between two positions.

function Position.direction_to(pos1: MapPosition.struct, pos2: MapPosition.struct) -> defines.direction

Returms the direction to a position using simple delta comparisons.

function Position.complex_direction_to(pos1: MapPosition.struct, pos2: MapPosition.struct, eight_way: nil | boolean) -> defines.direction

Returns the direction to a position.

function Position.orientation_to(pos1: MapPosition.struct, pos2: MapPosition.struct) -> RealOrientation

Returns the orientation from one position to another.

function Position.increment(pos: MapPosition.struct, inc_x: nil | number, inc_y: nil | number, increment_initial: nil | boolean) -> fun(new_inc_x: nil | number, new_inc_y: nil | number) -> StdLib.Area.Position

Increment a position each time it is called. This can be used to increment or even decrement a position quickly. <p>Do not store function closures in the global object; use them in the current tick. @usage 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} @usage 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} @usage local next_pos = Position.increment({0, 0}, 0, 1) surface.createentity{name = 'flying-text', text = 'text', position = nextpos()} surface.createentity{name = 'flying-text', text = 'text', position = nextpos()} -- creates two flying text entities 1 tile apart

Fields4

Position.x : number
Position.y : number
Position.__class: string = "Position"
Position.__index : unknown