From 2473c1ce41751cd281d86b8891560cc937ef2b02 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Sat, 24 Sep 2022 02:33:10 +0100 Subject: [PATCH] Implement Vector3.volume () --- .tests/Vector3/volume.test.lua | 44 +++++++++++++++++++++++ worldeditadditions_core/utils/vector3.lua | 10 ++++++ 2 files changed, 54 insertions(+) create mode 100644 .tests/Vector3/volume.test.lua diff --git a/.tests/Vector3/volume.test.lua b/.tests/Vector3/volume.test.lua new file mode 100644 index 0000000..f3fc7fb --- /dev/null +++ b/.tests/Vector3/volume.test.lua @@ -0,0 +1,44 @@ +local Vector3 = require("worldeditadditions_core.utils.vector3") + +describe("Vector3.volume", function() + it("should work", function() + local a = Vector3.new(4, 4, 4) + local b = Vector3.new(8, 8, 8) + assert.are.equal( + 64, + Vector3.volume(a, b) + ) + end) + it("should work the other way around", function() + local a = Vector3.new(4, 4, 4) + local b = Vector3.new(8, 8, 8) + assert.are.equal( + 64, + Vector3.volume(b, a) + ) + end) + it("should work with negative values", function() + local a = Vector3.new(-4, -4, -4) + local b = Vector3.new(-8, -8, -8) + assert.are.equal( + 64, + Vector3.volume(a, b) + ) + end) + it("should work with different values", function() + local a = Vector3.new(5, 6, 7) + local b = Vector3.new(10, 10, 10) + assert.are.equal( + 60, + Vector3.volume(a, b) + ) + end) + it("should work with mixed values", function() + local a = Vector3.new(10, 5, 8) + local b = Vector3.new(5, 10, 2) + assert.are.equal( + 150, + Vector3.volume(a, b) + ) + end) +end) diff --git a/worldeditadditions_core/utils/vector3.lua b/worldeditadditions_core/utils/vector3.lua index fc43984..d547a2f 100644 --- a/worldeditadditions_core/utils/vector3.lua +++ b/worldeditadditions_core/utils/vector3.lua @@ -164,6 +164,16 @@ function Vector3.length(a) return math.sqrt(a:length_squared()) end +--- Calculates the volume of the region bounded by 1 points. +-- @param a Vector3 The first point bounding the target region. +-- @param b Vector3 The second point bounding the target region. +-- @returns number The volume of the defined region. +function Vector3.volume(a, b) + local pos1, pos2 = Vector3.sort(a, b) + local vol = pos2 - pos1 + return vol.x * vol.y * vol.z +end + --- Calculates the dot product of this vector and another vector. -- @param a Vector3 The first vector to operate on. -- @param a Vector3 The second vector to operate on.