Vector3.min, Vector3.max: allow arguments to be numbers

This commit is contained in:
Starbeamrainbowlabs 2021-08-04 21:23:01 +01:00
parent 7c7abf4509
commit c48e9f2ab8
Signed by: sbrl
GPG Key ID: 1BE5172E637709C2
3 changed files with 52 additions and 4 deletions

View File

@ -19,6 +19,24 @@ describe("Vector3.max", function()
Vector3.max(a, b)
)
end)
it("should work with scalar numbers", function()
local a = Vector3.new(16, 1, 16)
local b = 2
assert.are.same(
Vector3.new(16, 2, 16),
Vector3.max(a, b)
)
end)
it("should work with scalar numbers both ways around", function()
local a = Vector3.new(16, 1, 16)
local b = 2
assert.are.same(
Vector3.new(16, 2, 16),
Vector3.max(b, a)
)
end)
it("should work with negative vectors", function()
local a = Vector3.new(-9, -16, -25)
local b = Vector3.new(-3, -6, -2)

View File

@ -19,6 +19,24 @@ describe("Vector3.min", function()
Vector3.min(a, b)
)
end)
it("should work with scalar numbers", function()
local a = Vector3.new(16, 1, 16)
local b = 2
assert.are.same(
Vector3.new(2, 1, 2),
Vector3.min(a, b)
)
end)
it("should work with scalar numbers both ways around", function()
local a = Vector3.new(16, 1, 16)
local b = 2
assert.are.same(
Vector3.new(2, 1, 2),
Vector3.min(b, a)
)
end)
it("should work with negative vectors", function()
local a = Vector3.new(-9, -16, -25)
local b = Vector3.new(-3, -6, -2)

View File

@ -317,8 +317,8 @@ end
--- Returns the mean (average) of 2 positions.
-- In other words, returns the centre of 2 points.
-- @param pos1 Vector3 pos1 of the defined region.
-- @param pos2 Vector3 pos2 of the defined region.
-- @param pos1 Vector3|number pos1 of the defined region.
-- @param pos2 Vector3|number pos2 of the defined region.
-- @param target Vector3 Centre coordinates.
function Vector3.mean(pos1, pos2)
return (pos1 + pos2) / 2
@ -326,10 +326,16 @@ end
--- Returns a vector of the min components of 2 vectors.
-- @param pos1 Vector3 The first vector to operate on.
-- @param pos2 Vector3 The second vector to operate on.
-- @param pos1 Vector3|number The first vector to operate on.
-- @param pos2 Vector3|number The second vector to operate on.
-- @return Vector3 The minimum values from the input vectors
function Vector3.min(pos1, pos2)
if type(pos1) == "number" then
pos1 = Vector3.new(pos1, pos1, pos1)
end
if type(pos2) == "number" then
pos2 = Vector3.new(pos2, pos2, pos2)
end
return Vector3.new(
math.min(pos1.x, pos2.x),
math.min(pos1.y, pos2.y),
@ -342,6 +348,12 @@ end
-- @param pos2 Vector3 The second vector to operate on.
-- @return Vector3 The maximum values from the input vectors.
function Vector3.max(pos1, pos2)
if type(pos1) == "number" then
pos1 = Vector3.new(pos1, pos1, pos1)
end
if type(pos2) == "number" then
pos2 = Vector3.new(pos2, pos2, pos2)
end
return Vector3.new(
math.max(pos1.x, pos2.x),
math.max(pos1.y, pos2.y),