mirror of
https://github.com/appgurueu/modlib.git
synced 2024-11-26 09:13:53 +01:00
Add vector library
This commit is contained in:
parent
cbad65e52b
commit
e1c44219d3
26
vector.lua
26
vector.lua
@ -18,17 +18,19 @@ function to_minetest(v)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function combine(v1, v2, f)
|
function combine(v1, v2, f)
|
||||||
local v = {}
|
local new_vector = {}
|
||||||
for k, c in pairs(v1) do
|
for key, value in pairs(v1) do
|
||||||
v[k] = f(c, v2[k])
|
new_vector[key] = f(value, v2[key])
|
||||||
end
|
end
|
||||||
return new(v)
|
return new(new_vector)
|
||||||
end
|
end
|
||||||
|
|
||||||
function apply(v, s, f)
|
function apply(v, s, f)
|
||||||
for i, c in pairs(v) do
|
local new_vector = {}
|
||||||
v[i] = f(c, s)
|
for key, value in pairs(v) do
|
||||||
|
new_vector[key] = f(value, s)
|
||||||
end
|
end
|
||||||
|
return new(new_vector)
|
||||||
end
|
end
|
||||||
|
|
||||||
function combinator(f)
|
function combinator(f)
|
||||||
@ -44,10 +46,18 @@ subtract, subtract_scalar = combinator(function(a, b) return a - b end)
|
|||||||
multiply, multiply_scalar = combinator(function(a, b) return a * b end)
|
multiply, multiply_scalar = combinator(function(a, b) return a * b end)
|
||||||
divide, divide_scalar = combinator(function(a, b) return a / b end)
|
divide, divide_scalar = combinator(function(a, b) return a / b end)
|
||||||
|
|
||||||
function length(v)
|
function norm(v)
|
||||||
local sum = 0
|
local sum = 0
|
||||||
for _, c in pairs(v) do
|
for _, c in pairs(v) do
|
||||||
sum = sum + c*c
|
sum = sum + c*c
|
||||||
end
|
end
|
||||||
return math.sqrt(sum)
|
return sum
|
||||||
|
end
|
||||||
|
|
||||||
|
function length(v)
|
||||||
|
return math.sqrt(norm(v))
|
||||||
|
end
|
||||||
|
|
||||||
|
function normalize(v)
|
||||||
|
return divide_scalar(v, length(v))
|
||||||
end
|
end
|
Loading…
Reference in New Issue
Block a user