Add vector.distance (required by k-d-tree)

This commit is contained in:
Lars Mueller 2021-02-07 14:01:40 +01:00
parent 4bdd79cadc
commit 4aec592fb2

@ -134,6 +134,15 @@ function length(v)
return math.sqrt(norm(v))
end
-- Minor code duplication for the sake of performance
function distance(v, other_v)
local sum = 0
for key, value in pairs(v) do
sum = sum + (value - other_v[key]) ^ 2
end
return math.sqrt(sum)
end
function normalize(v)
return divide_scalar(v, length(v))
end