Vector3: add abs

This commit is contained in:
Starbeamrainbowlabs 2021-06-26 17:06:53 +01:00
parent 88214aef57
commit 91b77c981b
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
2 changed files with 43 additions and 0 deletions

View file

@ -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)

View file

@ -245,6 +245,14 @@ function Vector3.max_component(a)
return math.max(a.x, a.y, a.z) return math.max(a.x, a.y, a.z)
end 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
-- ██████ ██████ ███████ ██████ █████ ████████ ██████ ██████ -- ██████ ██████ ███████ ██████ █████ ████████ ██████ ██████
-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ -- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██