mirror of
https://github.com/appgurueu/modlib.git
synced 2024-11-29 18:53:44 +01:00
Improve vector utils
This commit is contained in:
parent
cc7a22ea76
commit
2f01c5716b
24
vector.lua
24
vector.lua
@ -9,6 +9,10 @@ function from_xyzw(v)
|
|||||||
return new{v.x, v.y, v.z, v.w}
|
return new{v.x, v.y, v.z, v.w}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function from_minetest(v)
|
||||||
|
return new{v.x, v.y, v.z}
|
||||||
|
end
|
||||||
|
|
||||||
function to_xyzw(v)
|
function to_xyzw(v)
|
||||||
return {x = v[1], y = v[2], z = v[3], w = v[4]}
|
return {x = v[1], y = v[2], z = v[3], w = v[4]}
|
||||||
end
|
end
|
||||||
@ -25,10 +29,10 @@ function combine(v1, v2, f)
|
|||||||
return new(new_vector)
|
return new(new_vector)
|
||||||
end
|
end
|
||||||
|
|
||||||
function apply(v, s, f)
|
function apply(v, f, ...)
|
||||||
local new_vector = {}
|
local new_vector = {}
|
||||||
for key, value in pairs(v) do
|
for key, value in pairs(v) do
|
||||||
new_vector[key] = f(value, s)
|
new_vector[key] = f(value, ...)
|
||||||
end
|
end
|
||||||
return new(new_vector)
|
return new(new_vector)
|
||||||
end
|
end
|
||||||
@ -36,8 +40,8 @@ end
|
|||||||
function combinator(f)
|
function combinator(f)
|
||||||
return function(v1, v2)
|
return function(v1, v2)
|
||||||
return combine(v1, v2, f)
|
return combine(v1, v2, f)
|
||||||
end, function(v, s)
|
end, function(v, ...)
|
||||||
return apply(v, s, f)
|
return apply(v, f, ...)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -61,3 +65,15 @@ end
|
|||||||
function normalize(v)
|
function normalize(v)
|
||||||
return divide_scalar(v, length(v))
|
return divide_scalar(v, length(v))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function floor(v)
|
||||||
|
return apply(v, math.floor)
|
||||||
|
end
|
||||||
|
|
||||||
|
function ceil(v)
|
||||||
|
return apply(v, math.ceil)
|
||||||
|
end
|
||||||
|
|
||||||
|
function clamp(v, min, max)
|
||||||
|
return apply(apply(v, math.max, min), math.min, max)
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user