Vectors Library

zetaepyon edited this page Jan 9, 2018 · 1 revision

This library adds an interface for working with arbitrary-sized vectors (in the mathematical sense). It provides various functions for creating vectors, and common operators to work with them.

All the functions that return a vector create a copy and never modify the same vector. I.e. v:negate() would return the negative of the provided vector, but leave the passed in vector unchanged.

Usage

require('vectors')

Functions

V(coords, dimension)

vector = V(coords, dimension)
  • vector table - The resulting vector

  • coords table - A table containing the coordinates (numerically indexed, contiguous starting from 1)

  • dimension number [optional] - An integer describing the dimension of the vector

Constructor for vectors. Optionally provide dimension dimension, to avoid computing the dimension from the table. Can be used on a literal table like the similar one capital letter functions in other libraries. I.e. T{1,2,3}, S{1,2,3} and, in this case, V{1,2,3}.

vectors.zero(dimension)

vector = vectors.zero(dimension)
  • vector table - The resulting vector

  • dimension number - The length of the vector

Creates a zero-vector of dimension dimension.

vectors.fill(dimension, value)

vector = vectors.fill(dimension, value)
  • vector table - The resulting vector

  • dimension number - The dimension of the vector

  • value any - The element to fill the vector with

Creates a vector of dimension dimension with all values set to value.

vectors.unit(dimension, position)

vector = vectors.unit(dimension, position)
  • vector table - The resulting vector

  • dimension number - The dimension of the vector

  • position number - The position of the 1 element

Creates a euclidean unit vector of dimension dimension for axis position.

vectors.length(vector)

length = vectors.length(vector)
  • length number - The resulting absolute length of the vector

  • vector table - The vector whose length is to be measured

Returns the length of vector measured from 0.

vectors.normalize(vector)

normvector = vectors.normalize(vector)
  • normvector table - The resulting normalized vector

  • vector table - The vector that is meant to be normalized

Returns a vector in the same direction as vector, normalized to length one.

vectors.dimension(vector)

dimension = vectors.dimension(vector)
  • dimension number - The dimension of the provided vector

  • vector table - The vector that is meant to be normalized

Returns the dimension of vector. Constant.

vectors.dot(vector1, vector2)

dotvector = vectors.dot(vector1, vector2)
  • dotvector table - The vector resulting from the operation

  • vector1 table - The first operand vector

  • vector2 table - The second operand vector

Returns the dot product between two vectors.

vectors.cross(vector1, vector2)

crossvector = vectors.cross(vector1, vector2)
  • crossvector table - The vector resulting from the operation

  • vector1 table - The first operand vector

  • vector2 table - The second operand vector

Returns the cross product of two R^3 vectors.

vectors.scale(vector, factor)

scaledvector = vectors.scale(vector, factor)
  • scaledvector table - The resulting scaled vector

  • vector table - The vector to scale

  • factor number - The factor to scale it with

Returns vector multiplied by factor, i.e. all elements multiplied by the same factor.

vectors.negate(vector)

negvector = vectors.negate(vector)
  • negvector table - The resulting negated vector

  • vector table - The vector to negate

Returns the vector pointing in the opposite direction of vector with the same length.

vectors.add(vector1, vector2)

sumvector = vectors.add(vector1, vector2)
  • sumvector table - The vector resulting from the operation

  • vector1 table - The first operand vector

  • vector2 table - The second operand vector

Returns vector1 added to vector2.

vectors.subtract(vector1, vector2)

subvector = vectors.subtract(vector1, vector2)
  • subvector table - The vector resulting from the operation

  • vector1 table - The first operand vector

  • vector2 table - The second operand vector

Returns vector1 subtracted by vector2.

vectors.angle(vector1, vector2)

angle = vectors.angle(vector1, vector2)
  • angle number - The resulting angle

  • vector1 table - The first operand vector

  • vector2 table - The second operand vector

Returns the angle described by two vectors (in radians).

vectors.from_radian(angle)

vector = vectors.from_radian(angle)
  • vector table - The resulting vector

  • angle number - The radian to compute it from

Returns a 2D vector from a radian value. Note that this goes against mathematical convention, which commonly makes the radian go counter-clockwise. This function, instead, goes clockwise, i.e. it will return (0, -1) for π/2. This is done to match the game's internal representation, which has the X axis pointing east and the Y axis pointing south.

vectors.to_radian(vector)

radian = vectors.to_radian(vector)
  • angle number - The resulting radian

  • vector table - The vector to compute it from

Returns the radian that describes the direction of the vector.

Operators

addvector

addvector = vector1 + vector2

Calls vector.add.

subvector

subvector = vector1 - vector2

Calls vector.subtract.

dotvector

dotvector = vector1 * vector2

Calls vector.dot.

scaledvector

scaledvector = vector * scalar

Calls vector.scale.

negvector

negvector = -vector

Calls vector.negate.

V

vector = +vector

Calls V, which results in a copy of the same vector.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.