]> git.lizzy.rs Git - dragonfireclient.git/commitdiff
Add vector.combine (#11920)
authorLars Müller <34514239+appgurueu@users.noreply.github.com>
Wed, 4 May 2022 11:44:14 +0000 (13:44 +0200)
committerGitHub <noreply@github.com>
Wed, 4 May 2022 11:44:14 +0000 (07:44 -0400)
builtin/common/tests/vector_spec.lua
builtin/common/vector.lua
doc/client_lua_api.txt
doc/lua_api.txt

index 25880236bb62ffeccdcda313b870ce2b688898cb..6a0b81a89128de8f3e6edb7e709fa7c9e5339b36 100644 (file)
@@ -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))
index 90010f6de300f6abcbaf75032d8cb66b4cdbb415..a08472e322c7b42513f084e9f94560791e778e20 100644 (file)
@@ -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
index e3cc2dd166126d05b1b78e0cbac8edc6b13902e1..d08cd9b5efeae6f84a52343e950ddc00286a1c6f 100644 (file)
@@ -580,6 +580,7 @@ Spatial Vectors
 * `vector.floor(v)`: returns a vector, each dimension rounded down
 * `vector.round(v)`: returns a vector, each dimension rounded to nearest int
 * `vector.apply(v, func)`: returns a vector
+* `vector.combine(v, w, func)`: returns a vector
 * `vector.equals(v1, v2)`: returns a boolean
 
 For the following functions `x` can be either a vector or a number:
index 339ce8a27a351df5ab0971666bc0c176e99518ea..e95c5ced8e914bd5fc2adb6a5bf74dea41333201 100644 (file)
@@ -3409,6 +3409,9 @@ vectors are written like this: `(x, y, z)`:
 * `vector.apply(v, func)`:
     * Returns a vector where the function `func` has been applied to each
       component.
+* `vector.combine(v, w, func)`:
+       * Returns a vector where the function `func` has combined both components of `v` and `w`
+         for each component
 * `vector.equals(v1, v2)`:
     * Returns a boolean, `true` if the vectors are identical.
 * `vector.sort(v1, v2)`: