Skip to content

Commit

Permalink
Add vector.offset (#10321)
Browse files Browse the repository at this point in the history
  • Loading branch information
Desour committed Aug 29, 2020
1 parent 9976f36 commit 28e87ce
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
4 changes: 4 additions & 0 deletions builtin/common/tests/vector_spec.lua
Expand Up @@ -44,6 +44,10 @@ describe("vector", function()
assert.same({ x = 2, y = 4, z = 6 }, vector.add(vector.new(1, 2, 3), { x = 1, y = 2, z = 3 }))
end)

it("offset()", function()
assert.same({ x = 41, y = 52, z = 63 }, vector.offset(vector.new(1, 2, 3), 40, 50, 60))
end)

-- This function is needed because of floating point imprecision.
local function almost_equal(a, b)
if type(a) == "number" then
Expand Down
6 changes: 6 additions & 0 deletions builtin/common/vector.lua
Expand Up @@ -137,6 +137,12 @@ function vector.divide(a, b)
end
end

function vector.offset(v, x, y, z)
return {x = v.x + x,
y = v.y + y,
z = v.z + z}
end

function vector.sort(a, b)
return {x = math.min(a.x, b.x), y = math.min(a.y, b.y), z = math.min(a.z, b.z)},
{x = math.max(a.x, b.x), y = math.max(a.y, b.y), z = math.max(a.z, b.z)}
Expand Down
10 changes: 6 additions & 4 deletions doc/lua_api.txt
Expand Up @@ -3062,10 +3062,12 @@ For the following functions, `v`, `v1`, `v2` are vectors,
* Returns in order minp, maxp vectors of the cuboid defined by `v1`, `v2`.
* `vector.angle(v1, v2)`:
* Returns the angle between `v1` and `v2` in radians.
* `vector.dot(v1, v2)`
* Returns the dot product of `v1` and `v2`
* `vector.cross(v1, v2)`
* Returns the cross product of `v1` and `v2`
* `vector.dot(v1, v2)`:
* Returns the dot product of `v1` and `v2`.
* `vector.cross(v1, v2)`:
* Returns the cross product of `v1` and `v2`.
* `vector.offset(v, x, y, z)`:
* Returns the sum of the vectors `v` and `{x = x, y = y, z = z}`.

For the following functions `x` can be either a vector or a number:

Expand Down

0 comments on commit 28e87ce

Please sign in to comment.