mirror of
https://github.com/sbrl/Minetest-WorldEditAdditions.git
synced 2024-11-22 15:33:00 +00:00
Vector3: Add round
This commit is contained in:
parent
06e19248c7
commit
1e1112c34a
2 changed files with 50 additions and 0 deletions
44
.tests/Vector3/round.test.lua
Normal file
44
.tests/Vector3/round.test.lua
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
local Vector3 = require("worldeditadditions.utils.vector3")
|
||||||
|
|
||||||
|
local function format_map(map)
|
||||||
|
local result = {}
|
||||||
|
for key, value in pairs(map) do
|
||||||
|
table.insert(result, key.."\t"..tostring(value))
|
||||||
|
end
|
||||||
|
return table.concat(result, "\n")
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
describe("Vector3.round", function()
|
||||||
|
it("should round a positive vector", function()
|
||||||
|
local a = Vector3.new(3.1, 4.75, 5.9)
|
||||||
|
assert.are.same(
|
||||||
|
a:round(),
|
||||||
|
Vector3.new(3, 5, 6)
|
||||||
|
)
|
||||||
|
end)
|
||||||
|
it("should round a negative vector", function()
|
||||||
|
local a = Vector3.new(-3.1, -4.2, -5.3)
|
||||||
|
assert.are.same(
|
||||||
|
a:round(),
|
||||||
|
Vector3.new(-3, -4, -5)
|
||||||
|
)
|
||||||
|
end)
|
||||||
|
it("should work with integers", function()
|
||||||
|
local a = Vector3.new(3, 4, 5)
|
||||||
|
assert.are.same(
|
||||||
|
a:round(),
|
||||||
|
Vector3.new(3, 4, 5)
|
||||||
|
)
|
||||||
|
end)
|
||||||
|
it("should return a new Vector3 instance", function()
|
||||||
|
local a = Vector3.new(3.1, 4.7, 5.99999)
|
||||||
|
|
||||||
|
local result = a:round()
|
||||||
|
assert.are.same(
|
||||||
|
result,
|
||||||
|
Vector3.new(3, 5, 6)
|
||||||
|
)
|
||||||
|
assert.are_not.equal(result, a)
|
||||||
|
end)
|
||||||
|
end)
|
|
@ -109,6 +109,12 @@ end
|
||||||
function Vector3.ceil(a)
|
function Vector3.ceil(a)
|
||||||
return Vector3.new(math.ceil(a.x), math.ceil(a.y), math.ceil(a.z))
|
return Vector3.new(math.ceil(a.x), math.ceil(a.y), math.ceil(a.z))
|
||||||
end
|
end
|
||||||
|
--- Rounds the components of this vector.
|
||||||
|
-- @param a Vector3 The vector to operate on.
|
||||||
|
-- @returns Vector3 A new instance with the x/y/z components rounded.
|
||||||
|
function Vector3.round(a)
|
||||||
|
return Vector3.new(math.floor(a.x+0.5), math.floor(a.y+0.5), math.floor(a.z+0.5))
|
||||||
|
end
|
||||||
|
|
||||||
-- ██████ ██████ ███████ ██████ █████ ████████ ██████ ██████
|
-- ██████ ██████ ███████ ██████ █████ ████████ ██████ ██████
|
||||||
-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
|
-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
|
||||||
|
|
Loading…
Reference in a new issue