diff --git a/.tests/Vector3/abs.test.lua b/.tests/Vector3/abs.test.lua new file mode 100644 index 0000000..9803c57 --- /dev/null +++ b/.tests/Vector3/abs.test.lua @@ -0,0 +1,35 @@ +local Vector3 = require("worldeditadditions.utils.vector3") + +describe("Vector3.abs", function() + it("should work with a positive vector", function() + local a = Vector3.new(16, 64, 16) + assert.are.same( + Vector3.new(16, 64, 16), + a:abs() + ) + end) + it("should abs another positive vector", function() + local a = Vector3.new(9, 16, 25) + assert.are.same( + Vector3.new(9, 16, 25), + a:abs() + ) + end) + it("should abs a negative vector", function() + local a = Vector3.new(-9, -16, -25) + assert.are.same( + Vector3.new(9, 16, 25), + a:abs() + ) + end) + it("should return a new Vector3 instance", function() + local a = Vector3.new(9, -16, 25) + + local result = a:abs() + assert.are.same( + Vector3.new(9, 16, 25), + result + ) + assert.are_not.equal(result, a) + end) +end) diff --git a/worldeditadditions/utils/vector3.lua b/worldeditadditions/utils/vector3.lua index fa6115e..2598c07 100644 --- a/worldeditadditions/utils/vector3.lua +++ b/worldeditadditions/utils/vector3.lua @@ -245,6 +245,14 @@ function Vector3.max_component(a) return math.max(a.x, a.y, a.z) end +--- Returns the absolute form of this vector. +-- In other words, it removes the minus sign from all components of the vector. +-- @param a Vector3 The vector to operate on. +-- @returns Vector3 The absolute form of the given vector. +function Vector3.abs(a) + return Vector3.new(math.abs(a.x), math.abs(a.y), math.abs(a.z)) +end + -- ██████ ██████ ███████ ██████ █████ ████████ ██████ ██████ -- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██