2018-05-20 13:19:43 +00:00
|
|
|
worldeditadditions.vector = {}
|
|
|
|
|
|
|
|
function worldeditadditions.vector.tostring(v)
|
|
|
|
return "(" .. v.x ..", " .. v.y ..", " .. v.z ..")"
|
|
|
|
end
|
|
|
|
|
2020-08-20 00:53:26 +00:00
|
|
|
-- Calculates the length squared of the given vector.
|
|
|
|
-- @param v Vector The vector to operate on
|
|
|
|
-- @return number The length of the given vector squared
|
2018-05-20 13:19:43 +00:00
|
|
|
function worldeditadditions.vector.lengthsquared(v)
|
2020-08-21 19:59:50 +00:00
|
|
|
if not v.y then return v.x*v.x + v.z*v.z end
|
2018-05-20 13:19:43 +00:00
|
|
|
return v.x*v.x + v.y*v.y + v.z*v.z
|
|
|
|
end
|
2020-06-13 13:23:14 +00:00
|
|
|
|
2020-08-20 00:53:26 +00:00
|
|
|
--- Normalises the given vector such that its length is 1.
|
|
|
|
-- Also known as calculating the unit vector.
|
|
|
|
-- This method does *not* mutate.
|
|
|
|
-- @param v Vector The vector to calculate from.
|
|
|
|
-- @return Vector A new normalised vector.
|
2020-08-21 12:27:40 +00:00
|
|
|
function worldeditadditions.vector.normalize(v)
|
|
|
|
local length = math.sqrt(worldeditadditions.vector.lengthsquared(v))
|
2020-08-21 19:59:50 +00:00
|
|
|
if not v.y then return {
|
|
|
|
x = v.x / length,
|
|
|
|
z = v.z / length
|
|
|
|
} end
|
2020-08-20 00:53:26 +00:00
|
|
|
return {
|
2020-08-21 12:27:40 +00:00
|
|
|
x = v.x / length,
|
|
|
|
y = v.y / length,
|
|
|
|
z = v.z / length
|
2020-08-20 00:53:26 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Rounds the values in a vector down.
|
|
|
|
-- Warning: This MUTATES the given vector!
|
|
|
|
-- @param v Vector The vector to operate on
|
2020-06-13 13:23:14 +00:00
|
|
|
function worldeditadditions.vector.floor(v)
|
|
|
|
v.x = math.floor(v.x)
|
2020-08-20 00:53:26 +00:00
|
|
|
-- Some vectors are 2d, but on the x / z axes
|
|
|
|
if v.y then v.y = math.floor(v.y) end
|
|
|
|
-- Some vectors are 2d
|
|
|
|
if v.z then v.z = math.floor(v.z) end
|
2020-06-13 13:23:14 +00:00
|
|
|
end
|