2013-07-07 07:44:33 +02:00
|
|
|
|
|
|
|
vector = {}
|
|
|
|
|
|
|
|
function vector.new(a, b, c)
|
|
|
|
if type(a) == "table" then
|
2013-10-30 20:38:13 +01:00
|
|
|
assert(a.x and a.y and a.z, "Invalid vector passed to vector.new()")
|
2013-09-16 22:13:39 +02:00
|
|
|
return {x=a.x, y=a.y, z=a.z}
|
2013-10-30 20:38:13 +01:00
|
|
|
elseif a then
|
|
|
|
assert(b and c, "Invalid arguments for vector.new()")
|
2013-09-16 22:13:39 +02:00
|
|
|
return {x=a, y=b, z=c}
|
2013-07-07 07:44:33 +02:00
|
|
|
end
|
2013-09-16 22:13:39 +02:00
|
|
|
return {x=0, y=0, z=0}
|
2013-07-07 07:44:33 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function vector.equals(a, b)
|
|
|
|
return a.x == b.x and
|
|
|
|
a.y == b.y and
|
|
|
|
a.z == b.z
|
|
|
|
end
|
|
|
|
|
|
|
|
function vector.length(v)
|
|
|
|
return math.hypot(v.x, math.hypot(v.y, v.z))
|
|
|
|
end
|
|
|
|
|
|
|
|
function vector.normalize(v)
|
2013-07-07 08:02:45 +02:00
|
|
|
local len = vector.length(v)
|
|
|
|
if len == 0 then
|
2013-10-30 20:38:13 +01:00
|
|
|
return {x=0, y=0, z=0}
|
2013-07-07 08:02:45 +02:00
|
|
|
else
|
|
|
|
return vector.divide(v, len)
|
|
|
|
end
|
2013-07-07 07:44:33 +02:00
|
|
|
end
|
|
|
|
|
2016-10-17 21:36:38 +02:00
|
|
|
function vector.floor(v)
|
|
|
|
return {
|
|
|
|
x = math.floor(v.x),
|
|
|
|
y = math.floor(v.y),
|
|
|
|
z = math.floor(v.z)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-07-07 07:44:33 +02:00
|
|
|
function vector.round(v)
|
|
|
|
return {
|
|
|
|
x = math.floor(v.x + 0.5),
|
|
|
|
y = math.floor(v.y + 0.5),
|
|
|
|
z = math.floor(v.z + 0.5)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-12-05 23:18:52 +01:00
|
|
|
function vector.apply(v, func)
|
|
|
|
return {
|
|
|
|
x = func(v.x),
|
|
|
|
y = func(v.y),
|
|
|
|
z = func(v.z)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-07-07 07:44:33 +02:00
|
|
|
function vector.distance(a, b)
|
|
|
|
local x = a.x - b.x
|
|
|
|
local y = a.y - b.y
|
|
|
|
local z = a.z - b.z
|
|
|
|
return math.hypot(x, math.hypot(y, z))
|
|
|
|
end
|
|
|
|
|
|
|
|
function vector.direction(pos1, pos2)
|
2017-12-21 20:57:42 +01:00
|
|
|
return vector.normalize({
|
|
|
|
x = pos2.x - pos1.x,
|
|
|
|
y = pos2.y - pos1.y,
|
|
|
|
z = pos2.z - pos1.z
|
|
|
|
})
|
2013-07-07 07:44:33 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function vector.add(a, b)
|
|
|
|
if type(b) == "table" then
|
2013-09-16 22:13:39 +02:00
|
|
|
return {x = a.x + b.x,
|
|
|
|
y = a.y + b.y,
|
|
|
|
z = a.z + b.z}
|
2013-07-07 07:44:33 +02:00
|
|
|
else
|
2013-09-16 22:13:39 +02:00
|
|
|
return {x = a.x + b,
|
|
|
|
y = a.y + b,
|
|
|
|
z = a.z + b}
|
2013-07-07 07:44:33 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function vector.subtract(a, b)
|
|
|
|
if type(b) == "table" then
|
2013-09-16 22:13:39 +02:00
|
|
|
return {x = a.x - b.x,
|
|
|
|
y = a.y - b.y,
|
|
|
|
z = a.z - b.z}
|
2013-07-07 07:44:33 +02:00
|
|
|
else
|
2013-09-16 22:13:39 +02:00
|
|
|
return {x = a.x - b,
|
|
|
|
y = a.y - b,
|
|
|
|
z = a.z - b}
|
2013-07-07 07:44:33 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function vector.multiply(a, b)
|
|
|
|
if type(b) == "table" then
|
2013-09-16 22:13:39 +02:00
|
|
|
return {x = a.x * b.x,
|
|
|
|
y = a.y * b.y,
|
|
|
|
z = a.z * b.z}
|
2013-07-07 07:44:33 +02:00
|
|
|
else
|
2013-09-16 22:13:39 +02:00
|
|
|
return {x = a.x * b,
|
|
|
|
y = a.y * b,
|
|
|
|
z = a.z * b}
|
2013-07-07 07:44:33 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function vector.divide(a, b)
|
|
|
|
if type(b) == "table" then
|
2013-09-16 22:13:39 +02:00
|
|
|
return {x = a.x / b.x,
|
|
|
|
y = a.y / b.y,
|
|
|
|
z = a.z / b.z}
|
2013-07-07 07:44:33 +02:00
|
|
|
else
|
2013-09-16 22:13:39 +02:00
|
|
|
return {x = a.x / b,
|
|
|
|
y = a.y / b,
|
|
|
|
z = a.z / b}
|
2013-07-07 07:44:33 +02:00
|
|
|
end
|
|
|
|
end
|
2017-01-12 20:27:39 +01:00
|
|
|
|
|
|
|
function vector.sort(a, b)
|
|
|
|
return {x = math.min(a.x, b.x), y = math.min(a.y, b.y), z = math.min(a.z, b.z)},
|
|
|
|
{x = math.max(a.x, b.x), y = math.max(a.y, b.y), z = math.max(a.z, b.z)}
|
|
|
|
end
|