Vector3: add min_component, max_component

This commit is contained in:
Starbeamrainbowlabs 2021-06-26 17:02:35 +01:00
parent 0a3680aa7d
commit 88214aef57
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
3 changed files with 94 additions and 0 deletions

View file

@ -0,0 +1,39 @@
local Vector3 = require("worldeditadditions.utils.vector3")
describe("Vector3.max_component", function()
it("should work with a positive vector x", function()
local a = Vector3.new(30, 4, 5)
assert.are.equal(
30,
a:max_component()
)
end)
it("should work with a positive vector y", function()
local a = Vector3.new(3, 10, 5)
assert.are.equal(
10,
a:max_component()
)
end)
it("should work with a positive vector z", function()
local a = Vector3.new(3, 1, 50.5)
assert.are.equal(
50.5,
a:max_component()
)
end)
it("should work with a negative vector", function()
local a = Vector3.new(-4, -5, -1)
assert.are.equal(
-1,
a:max_component()
)
end)
it("should work with a mixed vector", function()
local a = Vector3.new(-30, 3, -3)
assert.are.equal(
3,
a:max_component()
)
end)
end)

View file

@ -0,0 +1,39 @@
local Vector3 = require("worldeditadditions.utils.vector3")
describe("Vector3.min_component", function()
it("should work with a positive vector x", function()
local a = Vector3.new(3, 4, 5)
assert.are.equal(
3,
a:min_component()
)
end)
it("should work with a positive vector y", function()
local a = Vector3.new(3, 1, 5)
assert.are.equal(
1,
a:min_component()
)
end)
it("should work with a positive vector z", function()
local a = Vector3.new(3, 1, 0.5)
assert.are.equal(
0.5,
a:min_component()
)
end)
it("should work with a negative vector", function()
local a = Vector3.new(-4, -5, -46)
assert.are.equal(
-46,
a:min_component()
)
end)
it("should work with a mixed vector", function()
local a = Vector3.new(-30, 3, -3)
assert.are.equal(
-30,
a:min_component()
)
end)
end)

View file

@ -229,6 +229,22 @@ function Vector3.move_towards(a, b, amount)
return a + (b - a):limit_to(amount) return a + (b - a):limit_to(amount)
end end
--- Returns the value of the minimum component of the vector.
-- Returns a scalar value.
-- @param a Vector3 The vector to operate on.
-- @returns number The value of the minimum component of the vector.
function Vector3.min_component(a)
return math.min(a.x, a.y, a.z)
end
--- Returns the value of the maximum component of the vector.
-- Returns a scalar value.
-- @param a Vector3 The vector to operate on.
-- @returns number The value of the maximum component of the vector.
function Vector3.max_component(a)
return math.max(a.x, a.y, a.z)
end
-- ██████ ██████ ███████ ██████ █████ ████████ ██████ ██████ -- ██████ ██████ ███████ ██████ █████ ████████ ██████ ██████
-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ -- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██