-
Notifications
You must be signed in to change notification settings - Fork 445
Vectors Library
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.
require('vectors')vector = V(coords, dimension)-
vectortable - The resulting vector -
coordstable - A table containing the coordinates (numerically indexed, contiguous starting from 1) -
dimensionnumber [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}.
vector = vectors.zero(dimension)-
vectortable - The resulting vector -
dimensionnumber - The length of the vector
Creates a zero-vector of dimension dimension.
vector = vectors.fill(dimension, value)-
vectortable - The resulting vector -
dimensionnumber - The dimension of the vector -
valueany - The element to fill the vector with
Creates a vector of dimension dimension with all values set to value.
vector = vectors.unit(dimension, position)-
vectortable - The resulting vector -
dimensionnumber - The dimension of the vector -
positionnumber - The position of the1element
Creates a euclidean unit vector of dimension dimension for axis position.
length = vectors.length(vector)-
lengthnumber - The resulting absolute length of the vector -
vectortable - The vector whose length is to be measured
Returns the length of vector measured from 0.
normvector = vectors.normalize(vector)-
normvectortable - The resulting normalized vector -
vectortable - The vector that is meant to be normalized
Returns a vector in the same direction as vector, normalized to length one.
dimension = vectors.dimension(vector)-
dimensionnumber - The dimension of the provided vector -
vectortable - The vector that is meant to be normalized
Returns the dimension of vector. Constant.
dotvector = vectors.dot(vector1, vector2)-
dotvectortable - The vector resulting from the operation -
vector1table - The first operand vector -
vector2table - The second operand vector
Returns the dot product between two vectors.
crossvector = vectors.cross(vector1, vector2)-
crossvectortable - The vector resulting from the operation -
vector1table - The first operand vector -
vector2table - The second operand vector
Returns the cross product of two R^3 vectors.
scaledvector = vectors.scale(vector, factor)-
scaledvectortable - The resulting scaled vector -
vectortable - The vector to scale -
factornumber - The factor to scale it with
Returns vector multiplied by factor, i.e. all elements multiplied by the same factor.
negvector = vectors.negate(vector)-
negvectortable - The resulting negated vector -
vectortable - The vector to negate
Returns the vector pointing in the opposite direction of vector with the same length.
sumvector = vectors.add(vector1, vector2)-
sumvectortable - The vector resulting from the operation -
vector1table - The first operand vector -
vector2table - The second operand vector
Returns vector1 added to vector2.
subvector = vectors.subtract(vector1, vector2)-
subvectortable - The vector resulting from the operation -
vector1table - The first operand vector -
vector2table - The second operand vector
Returns vector1 subtracted by vector2.
angle = vectors.angle(vector1, vector2)-
anglenumber - The resulting angle -
vector1table - The first operand vector -
vector2table - The second operand vector
Returns the angle described by two vectors (in radians).
vector = vectors.from_radian(angle)-
vectortable - The resulting vector -
anglenumber - 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.
radian = vectors.to_radian(vector)-
anglenumber - The resulting radian -
vectortable - The vector to compute it from
Returns the radian that describes the direction of the vector.
addvector = vector1 + vector2Calls vector.add.
subvector = vector1 - vector2Calls vector.subtract.
dotvector = vector1 * vector2Calls vector.dot.
scaledvector = vector * scalarCalls vector.scale.
negvector = -vectorCalls vector.negate.
vector = +vectorCalls V, which results in a copy of the same vector.