diff --git a/modules/vec2.lua b/modules/vec2.lua index 01a0b97..030937f 100644 --- a/modules/vec2.lua +++ b/modules/vec2.lua @@ -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 diff --git a/spec/vec2_spec.lua b/spec/vec2_spec.lua index 8b826ad..4bd0305 100644 --- a/spec/vec2_spec.lua +++ b/spec/vec2_spec.lua @@ -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)