Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(breaking) vec: Rework vec2.to_vec3; add vec3.to_xz #81

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions modules/vec2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
-- @module vec2

local modules = (...):gsub('%.[^%.]+$', '') .. "."
local vec3 = require(modules .. "vec3")
local precond = require(modules .. "_private_precond")
local private = require(modules .. "_private_utils")
local acos = math.acos
Expand Down Expand Up @@ -368,14 +367,18 @@ function vec2.flip_y(a)
return vec2.new(a.x, -a.y)
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)
-- No to_vec3 to avoid circular dependency. Use
-- vec3.from_table(a) instead.

--- Return a simple table representation of vector.
-- @tparam vec2 a Vector to be turned into a table
-- @treturn table
function vec2.to_table(a)
return {x = a.x, y = a.y}
end

-- No from_table -- it's the same as vec2.new(a).

--- Return a formatted string.
-- @tparam vec2 a Vector to be turned into a string
-- @treturn string formatted
Expand Down
30 changes: 30 additions & 0 deletions modules/vec3.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
local modules = (...):gsub('%.[^%.]+$', '') .. "."
local precond = require(modules .. "_private_precond")
local private = require(modules .. "_private_utils")
local vec2 = require(modules .. "vec2")
local sqrt = math.sqrt
local cos = math.cos
local sin = math.sin
Expand Down Expand Up @@ -373,6 +374,35 @@ function vec3.to_string(a)
return string.format("(%+0.3f,%+0.3f,%+0.3f)", a.x, a.y, a.z)
end

--- Return the x and y components as a vec2
-- @tparam vec3 a Vector to be reduced to a vec2
-- @treturn vec2
function vec3.to_xy(a)
return vec2.new(a.x, a.y)
end

--- Return the x and z components as a vec2
-- @tparam vec3 a Vector to be reduced to a vec2
-- @treturn vec2
function vec3.to_xz(a)
return vec2.new(a.x, a.z)
end

--- Return a simple table representation of vector.
-- @tparam vec3 a Vector to be turned into a table
-- @treturn table
function vec3.to_table(a)
return {x = a.x, y = a.y, z = a.z}
end

--- Create a vec3 from a table.
-- @tparam table a table to supply the values
-- @treturn vec3
function vec3.from_table(a)
assert(a.x and a.y, "from_table: Wrong argument type for right hand operand.")
return new(a.x, a.y, a.z or 0)
end

vec3_mt.__index = vec3
vec3_mt.__tostring = vec3.to_string

Expand Down
17 changes: 13 additions & 4 deletions spec/vec2_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -264,15 +264,24 @@ describe("vec2:", function()
assert.is.equal(math.deg(d.up:angle_between(d.up)), 0.0)
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)
local b = vec3.from_table(a)
local c = vec3.from_table(vec2(3,4))
assert.is.equal(b, vec3(1,2,0))
assert.is.equal(c, vec3(1,2,3))
assert.is.equal(c, vec3(3,4,0))
assert.is.equal(c:len(), 5)
end)

it("converts a 3-vector to a 2-vector", function()
local vec3 = require "modules.vec3"
local a = vec3(1,2,3)
local b = vec3.to_xy(a)
local c = vec3.to_xz(a)
assert.is.equal(b, vec2(1,2))
assert.is.equal(c, vec2(1,3))
end)

it("converts a vec3 to vec2 using the constructor", function()
Expand Down