aboutsummaryrefslogtreecommitdiff
path: root/builtin/common
diff options
context:
space:
mode:
authorLars Müller <34514239+appgurueu@users.noreply.github.com>2022-05-04 13:44:14 +0200
committerGitHub <noreply@github.com>2022-05-04 07:44:14 -0400
commitae7664597ed15f9ac779a9bac0595ab4125457c4 (patch)
tree2aa79c278f6ceeccbd941b5ec60357720c1141ca /builtin/common
parente9e671078c8ddfcaac30e8f04976a8c69031a9b9 (diff)
downloadminetest-ae7664597ed15f9ac779a9bac0595ab4125457c4.tar.gz
minetest-ae7664597ed15f9ac779a9bac0595ab4125457c4.tar.bz2
minetest-ae7664597ed15f9ac779a9bac0595ab4125457c4.zip
Add vector.combine (#11920)
Diffstat (limited to 'builtin/common')
-rw-r--r--builtin/common/tests/vector_spec.lua8
-rw-r--r--builtin/common/vector.lua8
2 files changed, 16 insertions, 0 deletions
diff --git a/builtin/common/tests/vector_spec.lua b/builtin/common/tests/vector_spec.lua
index 25880236b..6a0b81a89 100644
--- a/builtin/common/tests/vector_spec.lua
+++ b/builtin/common/tests/vector_spec.lua
@@ -128,6 +128,14 @@ describe("vector", function()
assert.equal(vector.new(4.1, 5.9, 5.5), a:apply(f))
end)
+ it("combine()", function()
+ local a = vector.new(1, 2, 3)
+ local b = vector.new(3, 2, 1)
+ assert.equal(vector.add(a, b), vector.combine(a, b, function(x, y) return x + y end))
+ assert.equal(vector.new(3, 2, 3), vector.combine(a, b, math.max))
+ assert.equal(vector.new(1, 2, 1), vector.combine(a, b, math.min))
+ end)
+
it("equals()", function()
local function assertE(a, b)
assert.is_true(vector.equals(a, b))
diff --git a/builtin/common/vector.lua b/builtin/common/vector.lua
index 90010f6de..a08472e32 100644
--- a/builtin/common/vector.lua
+++ b/builtin/common/vector.lua
@@ -110,6 +110,14 @@ function vector.apply(v, func)
)
end
+function vector.combine(a, b, func)
+ return fast_new(
+ func(a.x, b.x),
+ func(a.y, b.y),
+ func(a.z, b.z)
+ )
+end
+
function vector.distance(a, b)
local x = a.x - b.x
local y = a.y - b.y