vector = {}
-local function assert_vector(v)
- assert(type(v) == "table" and v.x and v.y and v.z, "Invalid vector")
-end
-
function vector.new(a, b, c)
if type(a) == "table" then
assert(a.x and a.y and a.z, "Invalid vector passed to vector.new()")
end
function vector.equals(a, b)
- assert_vector(a)
- assert_vector(b)
return a.x == b.x and
a.y == b.y and
a.z == b.z
end
function vector.length(v)
- assert_vector(v)
return math.hypot(v.x, math.hypot(v.y, v.z))
end
function vector.normalize(v)
- assert_vector(v)
local len = vector.length(v)
if len == 0 then
return {x=0, y=0, z=0}
end
function vector.round(v)
- assert_vector(v)
return {
x = math.floor(v.x + 0.5),
y = math.floor(v.y + 0.5),
end
function vector.distance(a, b)
- assert_vector(a)
- assert_vector(b)
local x = a.x - b.x
local y = a.y - b.y
local z = a.z - b.z
end
function vector.direction(pos1, pos2)
- assert_vector(pos1)
- assert_vector(pos2)
local x_raw = pos2.x - pos1.x
local y_raw = pos2.y - pos1.y
local z_raw = pos2.z - pos1.z
function vector.add(a, b)
- assert_vector(a)
if type(b) == "table" then
- assert_vector(b)
return {x = a.x + b.x,
y = a.y + b.y,
z = a.z + b.z}
end
function vector.subtract(a, b)
- assert_vector(a)
if type(b) == "table" then
- assert_vector(b)
return {x = a.x - b.x,
y = a.y - b.y,
z = a.z - b.z}
end
function vector.multiply(a, b)
- assert_vector(a)
if type(b) == "table" then
- assert_vector(b)
return {x = a.x * b.x,
y = a.y * b.y,
z = a.z * b.z}
end
function vector.divide(a, b)
- assert_vector(a)
if type(b) == "table" then
- assert_vector(b)
return {x = a.x / b.x,
y = a.y / b.y,
z = a.z / b.z}