Simple methods for flipping a vector on exactly 1 axis

This commit is contained in:
mcc 2019-11-30 11:25:00 -05:00
parent 00126f19c9
commit 7452cc0c6c
4 changed files with 53 additions and 0 deletions

View File

@ -321,6 +321,20 @@ function vec2.to_polar(a)
return radius, theta
end
-- Negate x axis only of vector.
-- @tparam vec2 a Vector to x-flip.
-- @treturn vec2 x-flipped vector
function vec2.flip_x(a)
return vec2.new(-a.x, a.y)
end
-- Negate y axis only of vector.
-- @tparam vec2 a Vector to y-flip.
-- @treturn vec2 y-flipped vector
function vec2.flip_y(a)
return vec2.new(a.x, -a.y)
end
--- Return a formatted string.
-- @tparam vec2 a Vector to be turned into a string
-- @treturn string formatted

View File

@ -279,6 +279,27 @@ function vec3.component_max(a, b)
return new(math.max(a.x, b.x), math.max(a.y, b.y), math.max(a.z, b.z))
end
-- Negate x axis only of vector.
-- @tparam vec2 a Vector to x-flip.
-- @treturn vec2 x-flipped vector
function vec3.flip_x(a)
return vec3.new(-a.x, a.y, a.z)
end
-- Negate y axis only of vector.
-- @tparam vec2 a Vector to y-flip.
-- @treturn vec2 y-flipped vector
function vec3.flip_y(a)
return vec3.new(a.x, -a.y, a.z)
end
-- Negate z axis only of vector.
-- @tparam vec2 a Vector to z-flip.
-- @treturn vec2 z-flipped vector
function vec3.flip_z(a)
return vec3.new(a.x, a.y, -a.z)
end
--- Return a boolean showing if a table is or is not a vec3.
-- @tparam vec3 a Vector to be tested
-- @treturn boolean is_vec3

View File

@ -189,6 +189,14 @@ describe("vec2:", function()
assert.is.equal("(+0.000,+0.000)", b)
end)
it("flips a 2-vector", function()
local a = vec2(1,2)
local temp = a:flip_x()
assert.is.equal(temp, vec2(-1, 2))
temp = temp:flip_y()
assert.is.equal(temp, vec2(-1, -2))
end)
-- Do this last, to insulate tests from accidental state contamination
it("converts a vec3 to vec2 using the constructor", function()
local vec3 = require "modules.vec3"

View File

@ -202,4 +202,14 @@ describe("vec3:", function()
local b = a:to_string()
assert.is.equal("(+0.000,+0.000,+0.000)", b)
end)
it("flips a 3-vector", function()
local a = vec3(1,2,3)
local temp = a:flip_x()
assert.is.equal(temp, vec3(-1, 2, 3))
temp = temp:flip_y()
assert.is.equal(temp, vec3(-1, -2, 3))
temp = temp:flip_z()
assert.is.equal(temp, vec3(-1, -2, -3))
end)
end)