feat(utils): add some vector utils
Will be in time replace the physics/utils
This commit is contained in:
parent
b64cf3103d
commit
737bba8541
2 changed files with 35 additions and 1 deletions
|
@ -31,5 +31,6 @@ return {
|
||||||
table = require "framework.utils.table",
|
table = require "framework.utils.table",
|
||||||
string = require "framework.utils.string",
|
string = require "framework.utils.string",
|
||||||
time = require "framework.utils.time",
|
time = require "framework.utils.time",
|
||||||
datas = require "framework.utils.datas"
|
datas = require "framework.utils.datas",
|
||||||
|
vector = require "framework.utils.vector"
|
||||||
}
|
}
|
||||||
|
|
33
framework/utils/vector.lua
Normal file
33
framework/utils/vector.lua
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
local vectors = {}
|
||||||
|
local Vector = require "framework.libs.brinevector3D"
|
||||||
|
|
||||||
|
---@class Vector
|
||||||
|
---@field x number the x coordinate of the vector
|
||||||
|
---@field y number the y coordinate of the vector
|
||||||
|
---@field z number the z coordinate of the vector
|
||||||
|
|
||||||
|
--- Apply a function to all coordinate of a vector to get a new one
|
||||||
|
---@param vector Vector the vector you want to apply to
|
||||||
|
---@param func function the function you want to apply
|
||||||
|
---@return Vector vector a new vector created from the first one and the function
|
||||||
|
function vectors.applyFunctionToCoord(vector, func)
|
||||||
|
return Vector(func(vector.x), func(vector.y), func(vector.z))
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
--- Get a vector with all coordinate turned to the largest integral value smaller than or equal to them
|
||||||
|
---@param vector Vector the vector you want to get the floor coordinate
|
||||||
|
---@return Vector vector the new vector with floor-cordinates
|
||||||
|
function vectors.floor(vector)
|
||||||
|
return vectors.applyFunctionToCoord(vector, math.floor)
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Get a vector with all coordinate turned to the smallest integral value larger than or equal to them
|
||||||
|
---@param vector Vector the vector you want to get the ceil coordinate
|
||||||
|
---@return Vector vector the new vector with ceil-cordinates
|
||||||
|
function vectors.ceil(vector)
|
||||||
|
return vectors.applyFunctionToCoord(vector, math.ceil)
|
||||||
|
end
|
||||||
|
|
||||||
|
return vectors
|
Loading…
Add table
Reference in a new issue