vec2.to_vec3(z) creates a vec3 with the given z (or 0)

This commit is contained in:
mcc 2019-11-30 11:31:22 -05:00
parent 00126f19c9
commit 185b19d766
2 changed files with 18 additions and 1 deletions

View File

@ -321,6 +321,14 @@ function vec2.to_polar(a)
return radius, theta
end
-- Convert vec2 to vec3.
-- @tparam vec2 a Vector to convert.
-- @tparam number the new z component, or nil for 0
-- @treturn vec3 Converted vector
function vec2.to_vec3(a, z)
return vec3(a.x, a.y, z or 0)
end
--- Return a formatted string.
-- @tparam vec2 a Vector to be turned into a string
-- @treturn string formatted

View File

@ -189,7 +189,16 @@ describe("vec2:", function()
assert.is.equal("(+0.000,+0.000)", b)
end)
-- Do this last, to insulate tests from accidental state contamination
-- Do vec3 tests last, to insulate tests from accidental state contamination
it("converts a 2-vector to a 3-vector", function()
local vec3 = require "modules.vec3"
local a = vec2(1,2)
local b = a:to_vec3()
local c = a:to_vec3(3)
assert.is.equal(b, vec3(1,2,0))
assert.is.equal(c, vec3(1,2,3))
end)
it("converts a vec3 to vec2 using the constructor", function()
local vec3 = require "modules.vec3"
local a = vec2(3, 5)