mirror of
https://github.com/sbrl/Minetest-WorldEditAdditions.git
synced 2024-11-26 00:53:00 +00:00
Implement Vector3.volume ()
This commit is contained in:
parent
6357d590d9
commit
2473c1ce41
2 changed files with 54 additions and 0 deletions
44
.tests/Vector3/volume.test.lua
Normal file
44
.tests/Vector3/volume.test.lua
Normal file
|
@ -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)
|
|
@ -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.
|
||||
|
|
Loading…
Reference in a new issue