Minetest-WorldEditAdditions/worldeditadditions/utils/vector.lua

43 lines
1.2 KiB
Lua
Raw Normal View History

2018-05-20 13:19:43 +00:00
worldeditadditions.vector = {}
function worldeditadditions.vector.tostring(v)
return "(" .. v.x ..", " .. v.y ..", " .. v.z ..")"
end
-- 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)
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
--- 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.
function worldeditadditions.vector.normalize(v)
local length = math.sqrt(worldeditadditions.vector.lengthsquared(v))
if not v.y then return {
x = v.x / length,
z = v.z / length
} end
return {
x = v.x / length,
y = v.y / length,
z = v.z / length
}
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)
-- 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