From 06e19248c701ba60aa250679d62ce441886a2bda Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Sat, 26 Jun 2021 14:19:18 +0100 Subject: [PATCH] Vector3: add floor/ceil --- .tests/Vector3/ceil.test.lua | 44 ++++++++++++++++++++++++++++ .tests/Vector3/floor.test.lua | 44 ++++++++++++++++++++++++++++ worldeditadditions/utils/vector3.lua | 14 +++++++++ 3 files changed, 102 insertions(+) create mode 100644 .tests/Vector3/ceil.test.lua create mode 100644 .tests/Vector3/floor.test.lua diff --git a/.tests/Vector3/ceil.test.lua b/.tests/Vector3/ceil.test.lua new file mode 100644 index 0000000..9c2ce99 --- /dev/null +++ b/.tests/Vector3/ceil.test.lua @@ -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.ceil", function() + it("should ceil a positive vector", function() + local a = Vector3.new(3.1, 4.2, 5.8) + assert.are.same( + a:ceil(), + Vector3.new(4, 5, 6) + ) + end) + it("should ceil a negative vector", function() + local a = Vector3.new(-3.1, -4.2, -5.3) + assert.are.same( + a:ceil(), + Vector3.new(-3, -4, -5) + ) + end) + it("should work with integers", function() + local a = Vector3.new(3, 4, 5) + assert.are.same( + a:ceil(), + 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:ceil() + assert.are.same( + result, + Vector3.new(4, 5, 6) + ) + assert.are_not.equal(result, a) + end) +end) diff --git a/.tests/Vector3/floor.test.lua b/.tests/Vector3/floor.test.lua new file mode 100644 index 0000000..61f4c7c --- /dev/null +++ b/.tests/Vector3/floor.test.lua @@ -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.floor", function() + it("should floor a positive vector", function() + local a = Vector3.new(3.1, 4.75, 5.9) + assert.are.same( + a:floor(), + Vector3.new(3, 4, 5) + ) + end) + it("should floor a negative vector", function() + local a = Vector3.new(-3.1, -4.2, -5.3) + assert.are.same( + a:floor(), + Vector3.new(-4, -5, -6) + ) + end) + it("should work with integers", function() + local a = Vector3.new(3, 4, 5) + assert.are.same( + a:floor(), + 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:floor() + assert.are.same( + result, + Vector3.new(3, 4, 5) + ) + assert.are_not.equal(result, a) + end) +end) diff --git a/worldeditadditions/utils/vector3.lua b/worldeditadditions/utils/vector3.lua index 7e59715..8db4260 100644 --- a/worldeditadditions/utils/vector3.lua +++ b/worldeditadditions/utils/vector3.lua @@ -96,6 +96,20 @@ end --- Alias for Vector3.divide. function Vector3.div(a, b) return Vector3.divide(a, b) end + +--- Rounds the components of this vector down. +-- @param a Vector3 The vector to operate on. +-- @returns Vector3 A new instance with the x/y/z components rounded down. +function Vector3.floor(a) + return Vector3.new(math.floor(a.x), math.floor(a.y), math.floor(a.z)) +end +--- Rounds the components of this vector up. +-- @param a Vector3 The vector to operate on. +-- @returns Vector3 A new instance with the x/y/z components rounded up. +function Vector3.ceil(a) + return Vector3.new(math.ceil(a.x), math.ceil(a.y), math.ceil(a.z)) +end + -- ██████ ██████ ███████ ██████ █████ ████████ ██████ ██████ -- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ -- ██ ██ ██████ █████ ██████ ███████ ██ ██ ██ ██████