A simple vector library for lua. This was mainly made to practice using metatables.
First, download vector.lua and add it to your project or whatever.
Then, add this wherever you require your libraries:
require "vector"vector.lua is all about vector objects you create using the vector() function.
Vectors are tables consisting of a list of values, starting at 1 (one). Creating a vector looks something like this:
v = Vector(10, 254, 27)This is almost the same as
v = {10, 254, 27}but there is one key distinction, I'll demonstrate it with this block of code:
v = Vector(5, -265, 111)
print(v[1]) --> 5
print(v.x) --> 5
print(v.x == v[1]) --> true
print(v.y) --> -265You can also access values in vectors using letters. specifically x, y, z, and w, in that order, but r, g, b, and a also works the same for reasons I'll get into later.
You can perform most arithmetic operations on vectors (namely +, -, /, *, %, ^, and negation).
a = Vector(3, 10, -14)
b = Vector(-6, 22, 5)
print(unpack(a - b)) --> 9 -12 -19If there's a size mismatch, then the smaller vector will be rescaled to the bigger vector.
a = Vector(82, 11, -543)
b = Vector(6, 0)
print(unpack(a + b)) --> 88 11 -537When performing vector arithmetic with a number, the number becomes a size 1 (one) vector.
v = Vector(3, 5)
print(unpack(v * 2)) --> 6 10Creates a vector with a size of the length of ..., with each of the values in the vector corresponding to the argument of the same index.
Example:
v = Vector(644, 213, -186, 320, 123)Like Vector except that it has a set size and any missing values are automatically set to 0.
Example:
v = Vec3(1,2)
print(v.x) --> 0
print(v.y) --> 0
print(v.z) --> 0Vector but the size is set by the size argument.
Example:
v = Vec(100, 5, 8, 123)
print(v.x) --> 5
print(v[3]) --> 123
print(v[54]) --> 0Vec4 but the default value is 1 instead of 0. Mainly made for LÖVE's colo(u)r values. This is why you can also use r, g, b, and a to access vectors.
Example:
col = VecC(1,0,0)
col.b = 1
love.graphics.setColor(col)
love.graphics.print("Wow! This text is magenta.")