diff --git a/.tests/Vector3/area.test.lua b/.tests/Vector3/area.test.lua new file mode 100644 index 0000000..076c180 --- /dev/null +++ b/.tests/Vector3/area.test.lua @@ -0,0 +1,34 @@ +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.area", function() + it("should work with a positive vector", function() + local a = Vector3.new(3, 3, 3) + assert.are.equal( + a:area(), + 27 + ) + end) + it("should work with a negative vector", function() + local a = Vector3.new(-4, -4, -4) + assert.are.equal( + a:area(), + -64 + ) + end) + it("should work with a mixed vector", function() + local a = Vector3.new(-3, 3, -3) + assert.are.equal( + a:area(), + 27 + ) + end) +end) diff --git a/worldeditadditions/utils/vector3.lua b/worldeditadditions/utils/vector3.lua index 8f81006..e57defa 100644 --- a/worldeditadditions/utils/vector3.lua +++ b/worldeditadditions/utils/vector3.lua @@ -116,6 +116,14 @@ 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 +--- Returns the area of this vector. +-- In other words, multiplies all the components together and returns a scalar value. +-- @param a Vector3 The vector to return the area of. +-- @returns number The area of this vector. +function Vector3.area(a) + return a.x * a.y * a.z +end + -- ██████ ██████ ███████ ██████ █████ ████████ ██████ ██████ -- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ -- ██ ██ ██████ █████ ██████ ███████ ██ ██ ██ ██████